Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析PHP多维数组_Php - Fatal编程技术网

解析PHP多维数组

解析PHP多维数组,php,Php,下面(给出)是我正在做的一些非常简单的php多维数组解析工作。我只是在搜索“突出显示”键,然后将一些键值对存储在另一个数组中。有没有更好的方法来实现这一点(我的意思是在性能方面),而不是使用n个foreach循环来实现您想要的 $json_O=json_decode(file_get_contents($url),true); foreach($json_O as $section1=>$items1){ if($section1==highlighting){

下面(给出)是我正在做的一些非常简单的php多维数组解析工作。我只是在搜索“突出显示”键,然后将一些键值对存储在另一个数组中。有没有更好的方法来实现这一点(我的意思是在性能方面),而不是使用n个foreach循环来实现您想要的

$json_O=json_decode(file_get_contents($url),true);
     foreach($json_O as $section1=>$items1){
        if($section1==highlighting){
            foreach($items1 as $section2=>$items2){
                    $key=$section2;
                    foreach($items2 as $section3=>$items3){
                        foreach ($items3 as $section4=>$items4){
                            $value=$items4;
                            $found[]=array('Key' => $key, 'Value' => $value);
下面是我试图解析的php对象示例:

Array
(
    [responseHeader] => Array
        (
            [status] => 0
            [QTime] => 3
            [params] => Array
                (
                    [indent] => on
                    [start] => 0
                    [q] => russian
                    [fragsize] => 40
                    [hl.fl] => Data
                    [wt] => json
                    [hl] => on
                    [rows] => 8
                )

        )

    [response] => Array
        (
            [numFound] => 71199
            [start] => 0
            [docs] => Array
......
......
    [highlighting] => Array
        (
            [114360] => Array
                (
                    [Data] => Array
                        (
                            [0] => AMEki has done it better <em>russian</em>...

....
....
数组
(
[responseHeader]=>数组
(
[状态]=>0
[QTime]=>3
[params]=>数组
(
[缩进]=>打开
[开始]=>0
[q] =>俄语
[fragsize]=>40
[hl.fl]=>数据
[wt]=>json
[hl]=>打开
[行]=>8
)
)
[响应]=>阵列
(
[numFound]=>71199
[开始]=>0
[docs]=>数组
......
......
[高亮显示]=>阵列
(
[114360]=>阵列
(
[数据]=>阵列
(
[0]=>阿梅基做得更好俄罗斯。。。
....
....
现在有两件事:1)我能做得更快吗?2)我能设计得更好吗

foreach($json_O['highlighting'] as ... ) {
   ...
}

仅仅因为它来自json并不意味着你不能作为一个普通的PHP数组访问它,因为它现在是一个普通的PHP数组。

一些基准测试显示(;;)循环的
性能比
foreach()更好。

当然,直接访问数组元素的速度要快得多

当您需要访问每个项目时,请使用foreach。如果您只想突出显示
,则直接访问即可

$higlighting = $json_0['highlighting'];
foreach($highlightis as $Key => $value) {
 //....
}
这似乎不必要

 foreach($json_O as $section1=>$items1){
    if($section1==highlighting){
        foreach($items1 as $section2=>$items2){
你可以这么做

        foreach($json_O['highlighting'] as $section2=>$items2){
简化其余部分也是可能的,尽管这还没有经过测试

$riter = new RecursiveArrayIterator($json_O['highlighting']);
$riteriter = new RecursiveIteratorIterator($riter, RecursiveIteratorIterator::LEAVES_ONLY);
$found = array();
foreach ($riteriter as $key => $value) {
    $key = $riteriter->getSubIterator($riteriter->getDepth() - 2)->key();
    $found[] = compact('key', 'value');
}

就个人而言,我只会使用嵌套的foreach循环。这很容易理解,而我创造性地使用递归迭代器却不容易理解。

idk,他在问,如果他能做得更快)那么,这是一个答案,直到他没有澄清他的问题8)