来自 json 个解码页面的预匹配

preg match from json decoded page

我有一个 json 已解码页面,我想从该页面删除一些数据。 我需要废弃这个 "value":“6fc975cd-bbd4-2daa-fc6b-1e8623f80caf|天线和滤波器产品|滤波器产品”
this is my json page

这是我的预匹配函数

    public function getcategories( $page = '' ) {
    $results = array();
    preg_match_all( '~/\[value\]\s=>\s(.*?)\s*\[~', $page, $matchall );
    debug($matchall);die;
    if ( !empty( $matchall[1] ) ) {
        foreach ( $matchall[1] as $single ) {
            if ( strlen( $single ) > 1 ) {
                $results[] = $single;
                }
            }
        }
    return $results;
}

我在这里调用这个函数

function checkpage( $page = '' ) {

    $vars_all_array = $this->getvarsallfrompage( $page );   
    $get_api_url = $this->catspostreq($page);
    $post_data   = $this->makePostData( $vars_all_array, 0, 25 );

    $jsonpage = $this->get_page( $get_api_url, array ('data' => $post_data, 'content-type'=> 'application/x-www-form-urlencoded; charset="UTF-8"; application/json' ) );
    $json_decoded = json_decode($jsonpage);
        $categories  = $this->getcategories( $json_decoded );
        debug($categories);die;
        }

但是有些东西不正常,我有这个错误:

preg_match_all() 期望参数 2 为字符串,数组给定

有人可以帮助我吗?

在您的 checkpage 函数中,您将 json_decode 的值作为参数传递给 getcategories 函数,其中 return 在大多数情况下是一个数组。而你的 getcategories 你传递 $page 参数作为 preg_match_all

的第二个参数
$json_decoded = json_decode($jsonpage);
$categories  = $this->getcategories( $json_decoded );

并在您的 getcategories 中

preg_match_all('~/\[value\]\s=>\s(.*?)\s*\[~', $page, $matchall);

这里的$page是json_decode的结果,是一个数组。这就是为什么你会收到那个错误

您不需要执行 preg_match_all 来从 $json_decoded 中获取值,因为 json_decode() 将 return (如果成功)一个完全可读的数组或对象。
因此,要获得一个特定值,您可以像这样访问它:

$value = $json_decoded['groupByResults'][0]->values[0]->value;

由于您希望将 所有 值放在一个新数组中,您可以迭代这些值并将其传递给一个新数组:

$categories = [];
foreach($json_decoded['groupByResults'][0]->values as $item) {
     $categories[] = $item['value'];
}

内置的数组函数可以在一行中执行此操作,而且速度可能更快。这是为了说明您对数据的处理。

使用这样的函数 array_column() 会产生这样的单行代码:

$categories =  array_column($json_decoded['groupByResults'][0]->values, "value");