SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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....

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 json_decode() 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...

    PHP Code:
    // 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 == || $item->value == 1)) {
            
    // Do something here
            
    echo 'Value found at array key ' $key;
        }


  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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....

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mihirpatel83 View Post
    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).

  5. #5
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    1)How did u knew that its encoded in jason ???
    I'm magic , i use JSON arrays all the time between AJAX and PHP scripts so I'm used to the array output.

    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....
    Like guido2004 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.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Could you please add this before print_r($item->extra_fields);

    PHP Code:
    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.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a solution now as it seems not to be an standard array ???

  10. #10
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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...

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


  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for the time...I will surely try that and will get back to you.....

  12. #12
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am sorry SgtLegend for trying it late.. Its not working...Gives the same warning message....

  13. #13
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Instead of doing a print_r($myArray), try doing a var_dump($myArray)
    What does it show?

  14. #14
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

  15. #15
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    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?

  16. #16
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    I tested this script:
    PHP Code:
    <?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:
    Code:
    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.

  17. #17
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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]

  18. #18
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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: [{&quot;id&quot;:&quot;2&quot;,&quot;value&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;1&quot;,&quot;value&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;3&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;4&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;5&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;6&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;7&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;8&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;9&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;10&quot;,&quot;value&quot;:&quot;1&quot;}]

    decoded: NULL

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

  19. #19
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    [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.

  20. #20
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mihirpatel83 View Post
    json string: [{&quot;id&quot;:&quot;2&quot;,&quot;value&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;1&quot;,&quot;value&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;3&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;4&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;5&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;6&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;7&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;8&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;9&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;10&quot;,&quot;value&quot;:&quot;1&quot;}]
    You see it like that in your browser? Or did you check the HTML code?

  21. #21
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OUTPUT
    --------
    json string: [{&quot;id&quot;:&quot;2&quot;,&quot;value&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;1&quot;,&quot;value&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;3&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;4&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;5&quot;,&quot;value&quot;:&quot;&quot;},{&quot;id&quot;:&quot;6&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;7&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;8&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;9&quot;,&quot;value&quot;:&quot;&lt;br \/&gt;&quot;},{&quot;id&quot;:&quot;10&quot;,&quot;value&quot;:&quot;1&quot;}]

    decoded: NULL
    [/QUOTE]

    The output is displayed like this only in mozilla and chrome as well.....

    &

    output for $item->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"}]"

  22. #22
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mihirpatel83 View Post
    The output is displayed like this only in mozilla and chrome as well.....

    &

    output for $item->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"}]"
    Ok, so it's a string.
    Beats me why mozilla and chrome show it like that. And beats me why decode works with a hardcoded string, and not with the same string coming from your $item class.

  23. #23
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes thats the thing.....Static string if given it takes and decodes it and everything works perfectly but it's not taking properly from item class.......

    i even tried to type cast it this way (string)$item->extra_fields but still not working....

    Also, this is the content of
    $item->extra_fields
    ------------------------------
    [{"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"}]

    in the value of id 6,7,8,9 is "<br \/>"........ so would that be the cause of problem ???? like when given as static string "<br \/>" changes to blank or is removed ?

  24. #24
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finally, I got it and probably was a stupid mistake and a check i didnt do.

    The string that was being fetched from database was showing properly in the brower but when i checked it by viewing the source the string was having "&quot;" instead of double quote...

    So I just used below code to replace and there got my problem solved....
    json_decode(str_replace("&quot;","\"",$item->extra_fields))

    Thank You for the time and help....

  25. #25
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •