April 28, 2012

Parsing PHP Multidimensional Array

Question by avinash shah

Here (given below) is some very simple php parsing multidimensional array stuff I am doing. I am just searching for ‘highlighting’ key and then storing some key value pairs in another array. Is there any better way to achieve this (I mean with respect to performance), rather than have n foreach loops to get to what you want.

$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);

Here is a sample php object I am trying to parse:

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>...

....
....

Two things now: 1)Can I do it quicker? 2)Can I design it better?

Answer by rambo coder

this seems unneccesary

 foreach($json_O as $section1=>$items1){
    if($section1==highlighting){
        foreach($items1 as $section2=>$items2){

you could simply do

        foreach($json_O['highlighting'] as $section2=>$items2){

simplifying the rest is also possible, although this is untested

$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');
}

personally, I would just use the nested foreach loops though. It’s very easy to understand, while my creative use of recursive iterators is not.

Answer by Starx

Use foreach when you need to get to each and every item. If you want only highlighting, then just access it directly.

$higlighting = $json_0['highlighting'];
foreach($highlightis as $Key => $value) {
 //....
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!