How to fetch data from array?

I have a field called extra_fields in a table…and its content is

[{“id”:“2”,“value”:“1”},{“id”:“1”,“value”:“1”},{“id”:“3”,“value”:“”},{“id”:“4”,“value”:“”},{“id”:“5”,“value”:“”},{“id”:“6”,“value”:“<br \/>”},{“id”:“7”,“value”:“<br \/>”},{“id”:“8”,“value”:“<br \/>”},{“id”:“9”,“value”:“<br \/>”},{“id”:“10”,“value”:“1”}]

Now i want to check with if condition that if have value 1 or 0 with id :10…
I just dont know how to access that…

Because the array you have is JSON encoded and contains object arrays we need to do 2 things, the first thing is we need to decode the array so to do that we use [COLOR=RoyalBlue]json_decode/COLOR which is a built in PHP function that comes with PHP 5.2 and above which transforms the JSON array into a readable PHP array. The next thing we need to do is use a foreach loop so we can go through each result to then evaluate the values within the array item. See the example below…

// Decode the array
$myArray = json_decode('[{"id":"2","value":"1"},{"id":"1","value":"1"},{"id":"3","value":""},{"id":"4", "value":""},{"id":"5","value":""},{"id":"6","value":"<br \\/>"},{"id":"7","value":"<br \\/>"},{"id":"8","value":"<br \\/>"},{"id":"9","value":"<br \\/>"},{"id":"10","value":"1"}]');

// Loop through the array till we find the key we are
// looking for
foreach ($myArray as $key => $item) {
    if ($item->id == 10 && ($item->value == 0 || $item->value == 1)) {
        // Do something here
        echo 'Value found at array key ' . $key;
    }
}

Wow that was a prompt reply…Thanks a lot.

Have couple of questions…

1)How did u knew that its encoded in jason ???
2) Is it necessary to loop through the loop ??? Can we access a particular array element directly with id or index no incase we need specific element all the time…

If you need a specific element, there’s no need to loop through the array. Like all arrays, you can access the element using the key(s).

1)How did u knew that its encoded in jason ???
I’m magic :D, i use JSON arrays all the time between AJAX and PHP scripts so I’m used to the array output.

  1. Is it necessary to loop through the loop ??? Can we access a particular array element directly with id or index no incase we need specific element all the time…
    Like [COLOR=#ff6600][B]guido2004[/B][/COLOR] said yes you can but if your array is dynamic it makes it a lot hard as the array key will always change, the reason i used the loop is because you said the data comes from the database which usually means its going to be dynamically updated via another part of your site.

Lol… Hope i too learn that magic soon…:)…Thanks a lot of the reply guido2004 and SgtLegend but i am either i am doing some mistake or the array is not getting serialized. I am using CMS joomla for my project and working on a customization.

Code

print_r($item->extra_fields);
$myArray = json_decode($item->extra_fields);
echo “<br/>O/P 1:”.print_r($myArray);
echo “<br/>O/P 2:”.$myArray;

Output

[{“id”:“2”,“value”:“1”},{“id”:“1”,“value”:“1”},{“id”:“3”,“value”:“”},{“id”:“4”,“value”:“”},{“id”:“5”,“value”:“”},{“id”:“6”,“value”:“<br \/>”},{“id”:“7”,“value”:“<br \/>”},{“id”:“8”,“value”:“<br \/>”},{“id”:“9”,“value”:“<br \/>”},{“id”:“10”,“value”:“1”}]
O/P 1:1
O/P 2:

[ note: output for op2 is blank. ]

Also on using this code

foreach ($myArray as $key => $item) {

    echo 'Value found at array key ' . $key;
	} 

I am getting warning

Warning: Invalid argument supplied for foreach() in view.html.php on line 464

Could you please add this before [FONT=Courier New]print_rCOLOR=SeaGreen[/COLOR];

[/FONT]

var_dump($item);

Once you run your script could you please post the output of the dump as it will allow us to see if $item refers to an object array or a standard array.

Code

var_dump($item);
print_r($item->extra_fields);
$myArray = unserialize($item->extra_fields);
echo “<br/>O/P 1:”.print_r($myArray);
echo “<br/>O/P 2:”.$myArray;
foreach ($myArray as $key => $item) {
echo 'Value found at array key ’ . $key;
}

Output

object(TableK2Item)#194 (39) { [“id”]=> string(2) “29” [“title”]=> string(4) “asdf” [“alias”]=> string(4) “asdf” [“catid”]=> string(1) “6” [“published”]=> string(1) “0” [“introtext”]=> string(0) “” [“fulltext”]=> string(0) “” [“image_caption”]=> string(0) “” [“image_credits”]=> string(0) “” [“video”]=> NULL [“video_caption”]=> string(0) “” [“video_credits”]=> string(0) “” [“gallery”]=> NULL [“extra_fields”]=> string(677) “[{“id”:“2”,“value”:“1”},{“id”:“1”,“value”:“1”},{“id”:“3”,“value”:”“},{“id”:“4”,“value”:”“},{“id”:“5”,“value”:”“},{“id”:“6”,“value”:”<br \/>“},{“id”:“7”,“value”:”<br \/>“},{“id”:“8”,“value”:”<br \/>“},{“id”:“9”,“value”:”<br \/>“},{“id”:“10”,“value”:“1”}]” [“extra_fields_search”]=> string(74) “Club 1 Germany <br /> <br /> <br /> <br /> Yes " [“created”]=> string(19) “2011-06-15 11:59:55” [“created_by”]=> string(2) “88” [“created_by_alias”]=> string(0) “” [“modified”]=> string(19) “2011-06-15 11:59:55” [“modified_by”]=> string(2) “88” [“publish_up”]=> string(19) “2011-06-15 00:00:00” [“publish_down”]=> string(5) “Never” [“checked_out”]=> string(2) “88” [“checked_out_time”]=> string(19) “2011-06-15 12:54:13” [“trash”]=> string(1) “0” [“access”]=> string(1) “0” [“ordering”]=> string(1) “7” [“featured”]=> string(1) “0” [“featured_ordering”]=> string(1) “0” [“hits”]=> string(1) “0” [“metadata”]=> string(0) “” [“metadesc”]=> string(0) “” [“metakey”]=> string(0) “” [“params”]=> string(0) “” [“plugins”]=> string(0) “” [”_tbl"]=> string(11) “#__k2_items” [“_tbl_key”]=> string(2) “id” [“_db”]=> &object(JDatabaseMySQL)#11 (19) { [“name”]=> string(5) “mysql” [“_nullDate”]=> string(19) “0000-00-00 00:00:00” [“_nameQuote”]=> string(1) “`” [“_sql”]=> string(93) “SELECT id AS value, name AS text FROM jos_groups WHERE id IN (0,1,2) OR id = ‘0’ ORDER BY id” [“_errorNum”]=> int(0) [“_errorMsg”]=> string(0) “” [“table_prefix"]=> string(4) "jos” [“_resource”]=> resource(24) of type (mysql link) [“_cursor”]=> resource(137) of type (Unknown) [“_debug”]=> int(0) [“_limit”]=> int(0) [“_offset”]=> int(0) [“_ticker”]=> int(0) [“_log”]=> array(0) { } [“_utf”]=> bool(true) [“_quoted”]=> array(0) { } [“_hasQuoted”]=> bool(false) [“_errors”]=> array(0) { } [“debug”]=> int(0) } [“_errors”]=> array(0) { } } [{“id”:“2”,“value”:“1”},{“id”:“1”,“value”:“1”},{“id”:“3”,“value”:“”},{“id”:“4”,“value”:“”},{“id”:“5”,“value”:“”},{“id”:“6”,“value”:“<br \/>”},{“id”:“7”,“value”:“<br \/>”},{“id”:“8”,“value”:“<br \/>”},{“id”:“9”,“value”:“<br \/>”},{“id”:“10”,“value”:“1”}]
O/P 1:1
O/P 2:
Warning: Invalid argument supplied for foreach() in /com_k2/views/item/view.html.php on line 466

Is there a solution now as it seems not to be an standard array ???

I’m about to head off to bed but as far as i can see the only thing i over sighted before was you have $item in the foreach declaration. Try changing $key => $item to $key => $v so the foreach loop should look something like this…

foreach ($myArray as $key => $v) {
    if ($v->id == 10 && ($v->value == 0 || $v->value == 1)) {
        echo 'Value found at array key ' . $key;
    }
}

Thanks a lot for the time…I will surely try that and will get back to you…

I am sorry SgtLegend for trying it late… Its not working…Gives the same warning message…

Instead of doing a print_r($myArray), try doing a var_dump($myArray)
What does it show?

Code

$myArray = json_decode($item->extra_fields);
echo “<br/>O/P 1:”.var_dump($myArray);
echo “<br/>O/P 2:”.$myArray;

Output

NULL
O/P 1:
O/P 2:

Where does that NULL in the output come from?

And these lines of code are in your script exactly in that order, without any other lines between them?

If I understand the manual correctly, json_decode should always return something. So the var_dump should show always something?

I tested this script:


<?php
  $json = '[{"id":"2","value":"1"},{"id":"1","value":"1"},{"id":"3","value":""},{"id":"4", "value":""},{"id":"5","value":""},{"id":"6","value":"<br \\/>"},{"id":"7","value":"<br \\/>"},{"id":"8","value":"<br \\/>"},{"id":"9","value":"<br \\/>"},{"id":"10","value":"1"}]';
  $decode = json_decode($json);

  echo "json string: " . htmlentities($json) . "<br/><br/>";
  echo "decoded: "; var_dump($decode);
?>

And it gave this result:


json string: [{"id":"2","value":"1"},{"id":"1","value":"1"},{"id":"3","value":""},{"id":"4", "value":""},{"id":"5","value":""},{"id":"6","value":"<br \\/>"},{"id":"7","value":"<br \\/>"},{"id":"8","value":"<br \\/>"},{"id":"9","value":"<br \\/>"},{"id":"10","value":"1"}]

decoded: array(10) { [0]=> object(stdClass)#1 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" } [1]=> object(stdClass)#2 (2) { ["id"]=> string(1) "1" ["value"]=> string(1) "1" } [2]=> object(stdClass)#3 (2) { ["id"]=> string(1) "3" ["value"]=> string(0) "" } [3]=> object(stdClass)#4 (2) { ["id"]=> string(1) "4" ["value"]=> string(0) "" } [4]=> object(stdClass)#5 (2) { ["id"]=> string(1) "5" ["value"]=> string(0) "" } [5]=> object(stdClass)#6 (2) { ["id"]=> string(1) "6" ["value"]=> string(6) "
" } [6]=> object(stdClass)#7 (2) { ["id"]=> string(1) "7" ["value"]=> string(6) "
" } [7]=> object(stdClass)#8 (2) { ["id"]=> string(1) "8" ["value"]=> string(6) "
" } [8]=> object(stdClass)#9 (2) { ["id"]=> string(1) "9" ["value"]=> string(6) "
" } [9]=> object(stdClass)#10 (2) { ["id"]=> string(2) "10" ["value"]=> string(1) "1" } }

So it should work.

Sorry my mistake…Now below is what you asked me to try with the exact code and output

code

print_r($item->extra_fields);
$myArray = json_decode($item->extra_fields);
echo “<br/>O/P 1:”.var_dump($myArray);
echo “<br/>O/P 2:”.$myArray;
foreach ($myArray as $key => $v) {
if ($v->id == 10 && ($v->value == 0 || $v->value == 1)) {
echo 'Value found at array key ’ . $key;
}
}

OUTPUT

[{“id”:“2”,“value”:“1”},{“id”:“1”,“value”:“1”},{“id”:“3”,“value”:“”},{“id”:“4”,“value”:“”},{“id”:“5”,“value”:“”},{“id”:“6”,“value”:“<br \/>”},{“id”:“7”,“value”:“<br \/>”},{“id”:“8”,“value”:“<br \/>”},{“id”:“9”,“value”:“<br \/>”},{“id”:“10”,“value”:“1”}]NULL
O/P 1:
O/P 2:
Warning: Invalid argument supplied for foreach() in /html/components/com_k2/views/item/view.html.php on line 465

[note: If i use print_r instead of var_dump…then NULL goes away and there is 1 in output of O/P 1]

I tried your code and instead of

$json = ‘[{“id”:“2”,“value”:“1”},{“id”:“1”,“value”:“1”},{“id”:“3”,“value”:“”},{“id”:“4”, “value”:“”},{“id”:“5”,“value”:“”},{“id”:“6”,“value”:“<br \/>”},{“id”:“7”,“value”:“<br \/>”},{“id”:“8”,“value”:“<br \/>”},{“id”:“9”,“value”:“<br \/>”},{“id”:“10”,“value”:“1”}]’;

i gave $json = $item->extra_fields;

OUTPUT

json string: [{"id":"2","value":"1"},{"id":"1","value":"1"},{"id":"3","value":""},{"id":"4","value":""},{"id":"5","value":""},{"id":"6","value":"<br \/>"},{"id":"7","value":"<br \/>"},{"id":"8","value":"<br \/>"},{"id":"9","value":"<br \/>"},{"id":"10","value":"1"}]

decoded: NULL

I guess its not taking $item->extra_fields as string…

[note: If i use print_r instead of var_dump…then NULL goes away and there is 1 in output of O/P 1]

It’s impossible that the NULL is caused by the var_dump. I see it before ‘O/P 1’, while the var_dump is done afterwards?

Anyway, if json_decode returns NULL, something’s wrong.
Please do a var_dump($item->extra_fields). I thought that was a string, but maybe I’m wrong? After all, my test with the same string works just fine.

You see it like that in your browser? Or did you check the HTML code?