I am new to JSON and I want to store multi records in JSON format and then be able to loop through the records.

Here is what I am trying to do:

<?php
$json = '{"first_name":"Todd","last_name":"Cary","nick_name":"Todd"}';

$json2 = '{"first_name":"Todd","last_name":"Cary","nick_name":"Todd"},
          {"first_name":"David","last_name":"Smith","nick_name":"Dave"}';

$obj = json_decode($json);         
print_r($obj->{'nick_name'}); ?>

I do not know how to do the same with $json2 e.g. loop through each record and display "nick_name".

Any help to point me in the right direction would be appreciated.

Todd

    I would make your JSON an array of objects:

    <?php
    
    $json2 = '[
    	{"first_name":"Todd","last_name":"Cary","nick_name":"Todd"},
    	{"first_name":"David","last_name":"Smith","nick_name":"Dave"}
    ]';
    
    $data = json_decode($json2);
    foreach ($data as $record) {
    	echo $record->first_name.' ('.$record->nick_name.') '.$record->last_name.PHP_EOL;
    }
    

      If I were you, I would avoid manually typing any JSON at all. I would create arrays and objects and rely on [man]json_encode[/man] to write the JSON. Otherwise, it's very easy to make mistakes that cause your JSON to be invalid.

      On second thought, I see how manually typing JSON code might actually be a little easier than the PHP syntax -- as long as you don't have any quotes in your data that you have to escape.

        Or, if using a recent enough version of PHP, you could use it's shortened array syntax to make something almost JSON-like that is actually a PHP array:

        $data = [
        	["first_name" => "Todd", "last_name" => "Cary", "nick_name" => "Todd"],
        	["first_name" => "David", "last_name" => "Smith", "nick_name" => "Dave"]
        ];
        
        foreach ($data as $record) {
        	echo $record['first_name'].' ('.$record['nick_name'].') '.$record['last_name'].PHP_EOL;
        }
        

          Thank you...great idea. In fact, I went the way of JSON. Actually, I really need a text DB since the data will drive the creation of a report. Here is what I have:

          $layout = '{"Layout": [
            {"attr":"first_name","line":1,"x":0,"strong":1},
            {"attr":"last_name","line":1,"x":40,"strong":1}  
          ]}'; $oLayout = json_decode($layout, TRUE); // Get the "attr" values foreach ($oLayout as $layout) { foreach($layout as $key => $value) { print_r($value["attr"] . "<br>"); } }

          I still have not mastered the syntax of php arrays. I understand the above, however, is it possible to access the "attr" of, let's say, the second $oLayout (index = 1) is one line of code?

          Todd

            Unfortunately, that gives a blank result, and it is one of the many syntax's I have tried.

            Many thanks for responding.

              Oh, I missed that the JSON has the array wrapped in an object. You don't want to loop over [font=monospace]$oLayout[/font], you want to loop over [font=monospace]$oLayout['Layout'][/font].

                Weedpacket -

                This works:

                print_r($oLayout['Layout'][1]["attr"]); //!!!

                Where I am lost is in the concepts of objects and arrays, php style. When I create a class (e.g. myClass), I then do

                $myInstance = new $myClass();

                When I refer to the Object, $myInstance, I use

                $x = $myInstance->myFunction();

                I'm use to the "->" and obviously it does not get used here.

                Thank you again...

                Todd

                  The second argument to [man]json_decode[/man] forces associated arrays when set to true, instead of instances of StdClass.

                    Ahhh says this blind man....

                    Many thanks....

                    Todd

                      Write a Reply...