0

I have the following PHP code:

<?php
    exec("./mycode.py", $out)
    var_dump($out) 

?>

It produces the following output:

array(2) { [0]=> string(28) "{"ky2": "bar", "ky1": "foo"}" [1]=> string(30) "{"ky2": "bar2", "ky1": "foo2"}" }

How can I iterate the output above and print the result?

 Entry 0
    ky1 - foo
    ky2 - bar
 Entry 1
    ky1 - foo2
    ky2 - bar2

They Python code (mycode.py) is this:

#!/usr/bin/env python
import json
dict1 = {'ky1':'foo', 'ky2':'bar'}
dict2 = {'ky1':'foo2', 'ky2':'bar2'}
print json.dumps(dict1)
print json.dumps(dict2)

It prints this:

{"ky2": "bar", "ky1": "foo"}
{"ky2": "bar2", "ky1": "foo2"}
user1978142
  • 7,946
  • 3
  • 17
  • 20
pdubois
  • 7,640
  • 21
  • 70
  • 99
  • possible duplicate of [json\_decode to array](http://stackoverflow.com/questions/5164404/json-decode-to-array) – laurent Jun 04 '14 at 02:52

1 Answers1

5

You just need a a foreach loop in conjunction with json_decode() on this one. While inside the loop, decode them each time. Consider this example:

$out = array(
    array('{"ky2": "bar", "ky1": "foo"}'),
    array('{"ky2": "bar2", "ky1": "foo2"}'),
);

$new_out = array();
foreach($out as $key => $value) {
    $values = json_decode($value[0], true); // <-- second parameter
    // set to TRUE to force return as an array
    ksort($values);
    $new_out['Entry '.$key] = $values;
}

echo '<pre>';
print_r($new_out);
echo '</pre>';

Sample Output:

Array
(
    [Entry 0] => Array
        (
            [ky1] => foo
            [ky2] => bar
        )

    [Entry 1] => Array
        (
            [ky1] => foo2
            [ky2] => bar2
        )

)

Edit: Or just maybe you just want an echo, I dont know.

foreach($out as $key => $value) {
    $values = json_decode($value[0], true);
    ksort($values);

    // or just plain echo
    echo "Entry $key <br/>";
    foreach($values as $index => $element) {
        echo str_repeat('&nbsp;', 5) . "$index - $element <br/>";
    }
}

Output:

Entry 0 
   ky1 - foo 
   ky2 - bar 
Entry 1 
   ky1 - foo2 
   ky2 - bar2 

Sample Fiddle

user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Good answer. You should also note that `json_decode($value[0], true);` with the `true` set forces an array to be returned. The same code could work with pure objects. – Giacomo1968 Jun 04 '14 at 02:58
  • @JakeGould Thank you for pointing that out, I forgot to explain what that second parameter means. – user1978142 Jun 04 '14 at 03:01
  • @kevinabelita: Thanks so much. Could you kindly advice the construct right from `exec()` output? Coz, I tried but prints nothing. – pdubois Jun 04 '14 at 03:03
  • @pdubois can you rephrase it? i dont understand, you want construct that kind of output right after `exec()`? – user1978142 Jun 04 '14 at 03:09
  • @kevinabelita: in your example you have this `$out = array(array('{"ky2": "bar", "ky1": "foo"}'),array('{"ky2": "bar2", "ky1": "foo2"}'),); ` How can I convert `$out` of to that construct from `exec("./mycode.py", $out)` – pdubois Jun 04 '14 at 03:11
  • 1
    @pdubois you dont need to construct such value, it just created my own copy of `$out` which is also the same output of your executed python script. the one you provided should be fairly the same as the one i used on m example. – user1978142 Jun 04 '14 at 03:14
  • @kevinabelita: Thanks but why this code http://dpaste.com/17CD9YC/ prints nothing? – pdubois Jun 04 '14 at 03:21
  • 1
    @pdubois is that your real code? some are missing semicolons `;` – user1978142 Jun 04 '14 at 03:23
  • 1
    @pdubois i havent coded any python script yet, but i think youre not calling the python script correct. isn't it `python scriptfile.py`, without `./` – user1978142 Jun 04 '14 at 03:25