0

First let me say that I have gone through a lot of related question on here but none of them solve my issue. I have and php nested array in db and need to print the all the values. the output prints out the first top most array (which is the 'name', and values "Office , home") but does not get the nested array values which is contained in 'BB". this is my code:

function getTB($name='', $value='') {
$value    = json_decode($value);

$name = '';  $BB= ''; 
foreach ($value  as $key=>$val) {
    $name .= $val['name'].',';

JSON code:

[{"name":"Office","TB":[{"date":[false,true],"time":"8:00 AM"}]},{{"days":[false,true],"time":"6:00 PM"},
4
  • Are you printing the array out for debugging purposes? Perhaps print_r or var_dump may be of use (uk3.php.net/print_r uk3.php.net/var-dump) Commented May 27, 2014 at 15:32
  • no i need to print the information out in a doc which will be used later by a different program Commented May 27, 2014 at 15:49
  • Okay, so can you not just re-encode it in JSON format with json_encode? Commented May 27, 2014 at 16:11
  • @Pudge601 I do not understand your suggestion. My problem is that am unable to print out the entire array not with the format Commented May 27, 2014 at 16:40

1 Answer 1

1

use array_walk_recursive

for example if you have json like this (which is having a subarray)

$decoded = json_decode('{"abc": "one",
                         "def": "two",
                         "sub_array": {
                                 "ABC": "ONE"
                                 }}', true);

array_walk_recursive($decoded, function($value,$key) {
  echo $key.' :'.$value.'<br>';
});

the output will be

abc :one
def :two
ABC :ONE

so use like this instead of normal foreach

Update

use like this

$arr='[{"name":"Office","TB":[{"date":[false,true],"time":"8:00 AM"},{"days":[false,true],"time":"6:00 PM"}]}]';

$rr=json_decode($arr);

foreach ($rr[0]->TB as $key => $value) {
array_walk_recursive($value, function($k,$kk){
     echo 'Key is '.$kk.' & Value is '.$k.'<br>';   
});
}

output:

Key is 0 & Value is 

Key is 1 & Value is 1

Key is time & Value is 8:00 AM

Key is 0 & Value is 

Key is 1 & Value is 1

Key is time & Value is 6:00 PM
Sign up to request clarification or add additional context in comments.

5 Comments

i get this error when i tried your code: PHP Catchable fatal error: Object of class stdClass could not be converted to string. I will update the question with the structure of my array
try array_walk_recursive($decoded[0]->TB, functio...
can you post the json code,with out decoding it..? which looks like {abc:adad...}
you can access each subarray like this $rr[0]->TB[0] and $rr[0]->TB[1]
this does not return as well.. maybe this function does not imply in my case

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.