4

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?

0

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Just because it came from json doesn't mean you CAN'T access it as a normal PHP array, because it IS a normal php array now.

Comments

1

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) {
 //....
}

Comments

0

some benchmarks shows better performance of for(;;) loop rather than foreach(). and of course direct access to array element is quite faster

2 Comments

idk, he is asking, if he can do quicker)
well, it's an answer then, until he not clarified his question 8)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.