2

I'm new in php I have a json I decoded it and wrote the following

$jsonInput = '[{"b_pag_bo_id":"31","b_pag_user_id":"1","b_pag_id":"1","b_page_mark":"1","b_pag_num":"3","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"31","b_pag_user_id":"1","b_pag_id":"2","b_page_mark":"1","b_pag_num":"57","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"31","b_pag_user_id":"1","b_pag_id":"3","b_page_mark":"1","b_pag_num":"60","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"4","b_page_mark":"1","b_pag_num":"4","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"5","b_page_mark":"1","b_pag_num":"6","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"6","b_page_mark":"1","b_pag_num":"9","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"7","b_page_mark":"1","b_pag_num":"183","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"8","b_page_mark":"1","b_pag_num":"324","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"9","b_page_mark":"1","b_pag_num":"331","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"32","b_pag_user_id":"1","b_pag_id":"10","b_page_mark":"1","b_pag_num":"710","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"34","b_pag_user_id":"0","b_pag_id":"11","b_page_mark":"1","b_pag_num":"50","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"34","b_pag_user_id":"0","b_pag_id":"12","b_page_mark":"1","b_pag_num":"99","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"26","b_pag_user_id":"0","b_pag_id":"13","b_page_mark":"1","b_pag_num":"5","b_pag_note":"","b_page_stop":"1"},{"b_pag_bo_id":"26","b_pag_user_id":"0","b_pag_id":"14","b_page_mark":"1","b_pag_num":"7","b_pag_note":"","b_page_stop":"1"}]';

$decoded = json_decode($jsonInput, true);

print_r($decoded);

and the result is in the link here

I want to get each value of each key meaning I want all b_pag_bo_id & others in a separate variables. How do I do that?

1
  • 1
    Any reason you prefer separate variables over one big array? Commented May 29, 2012 at 13:50

4 Answers 4

1

You can print values similar to this:

foreach ($decoded as $key => $value) {
    echo $value['b_pag_bo_id'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer but it gives me the values without separators, how can I separate them by spaces for example?
echo $value['b_pag_bo_id'] . ", "; that 'll add comma separator.
$last_item = array_pop($decoded); echo $last_item['b_pag_bo_id'];
1

Use a foreach loop on your result and store the data in individual variables.

foreach ( $decoded AS $key => $val ) {
  ${'var'.$key} = $val;
}

Comments

0

you need to loop though array:

$b_pag_bo_id = array();
$b_pag_user_id = array();
foreach ($decoded as $item) {
   $b_pag_bo_id[] = $item['b_pag_bo_id'];
   $b_pag_user_id[] = $item['b_pag_user_id'];
}

1 Comment

You said that you want it in variable. If you want to print it you can use this echo implode(', ', $b_pag_bo_id); or echo json_encode($b_pag_bo_id)
0
foreach ($array['playerCredentials'] as $key => $value) 
{
   printf('%s => %s<br />', $key, $value);
}

Comments

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.