1

I receive a JSON stream from an iphone that contains some simple strings and numbers as well as an array of dictionaries. I would like to work with these dictionaries, in essence, storing the data in each of them in a separate MYSQL record.

To get access to the strings as well as the array from the JSON stream, I am using the following:

$jsonString = file_get_contents('php://input');
$jsonArray = json_decode($jsonString, true);
$authString = jsonArray['authstring'];
$itemsArray =  $jsonArray['itemsArray'];

This is what itemsArray looks like before being sent to the server:

itemsArray =     (
                {
            lasttouchedstr = "2018-07-09 17:24:56";
            localiid = 6;
            iid = 0;
            title = "test";
            complete = 1;
            userid = 99;
            whenaddedstr = "2018-06-21 14:10:23";
        },
                {
            lasttouchedstr = "2018-07-09 17:24:56";
            localiid = 37;
            iid = 0;
            title = "how about this";
            userid = 88;
            whenaddedstr = "2018-07-07 16:58:31";
        },
                {
            lasttouchedstr = "2018-07-09 17:24:56";
            localiid = 38;
            iid = 0;
            title = reggiano;
            userid = 1;
            whenaddedstr = "2018-07-07 17:28:55";
        }

etc.

I guess I should probably put these dictionaries into an Associative Array in order to save them.

I am struggling, however, with how to reference and get the objects. From what I can tell the following code is returning empty values in so far as $message comes back as empty.

  $anitem = $jsonArray['itemsArray'][0];
$message=$anitem;
    $title = $jsonArray['itemsArray'][0].[item];
$message.=$title;

Can anyone suggest proper syntax to grab these items and their properties?

Thanks in advance for any suggestions.

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • 1
    Tried a `foreach` loop? Or any loop? Looks like you've got the "item" `$jsonArray['itemsArray'][0]`. Its indexes would be: `$jsonArray['itemsArray'][$i]['localiid']` - Iterate on $i. – ficuscr Jul 09 '18 at 21:55
  • Possible duplicate of [Loop through an array php](https://stackoverflow.com/questions/4414623/loop-through-an-array-php) – ficuscr Jul 09 '18 at 21:58
  • the items in `$itemsArray` are `objects` not `arrays` so you would use object notation to access members rather than std array notation – Professor Abronsius Jul 09 '18 at 22:06
  • @ficuscr, I was able to use the middle example in the link you suggested to get it to work. What threw me off is that without a loop $anitem = $jsonArray['itemsArray'][0] did not produce a value. – user6631314 Jul 09 '18 at 22:19

1 Answers1

1

I find it strange that people associate things with a dictionary, while it is nothing more then a multidimensional array.

If you can read JSON, you see that the variable will have an index containing each entry.

For PHP:

foreach($jsonArray as $array){
  // note that $array is still an array:

  foreach($array as $v){
    echo "Hurray!: '$v'";
  }
}

If it really was an object (or cast to an object), the only thing you need to change is how you access the variable (as in any other language). In PHP it would be:

echo $jsonArray[0]->lasttouchedstr;

Or of it was the same loop:

foreach($jsonArray as $v){
  echo $v->lasttouchedstr;
}

Multidimensional?

echo $jsonArray['itemsArray'][0][item]; // array
echo $jsonArray->itemsArray[0][item]; // if items array is an actual array and jsonArray an object.

Most languages associate things written as a . that the left side is an object. In PHP it's written as ->.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • It was an NSArray of NSDictionary objects on the iphone (Objective-C). There is a difference in the nomenclature between the two languages. – user6631314 Jul 09 '18 at 22:13
  • Maybe, but you are talking about how it is accessed in PHP, not on the iPhone. Since you are accessing it in PHP, you have to switch gears so to speak, and think in terms of how PHP handles the data. – Sloan Thrasher Jul 09 '18 at 22:16
  • Yes, I used the term Associate Array as I realize that PHP handles it differently and perhaps more intuitively. However, I think you can call it a dictionary: https://en.wikibooks.org/wiki/A-level_Computing/AQA/Paper_1/Fundamentals_of_data_structures/Dictionaries – user6631314 Jul 09 '18 at 22:18
  • PHP isn't really intuitively. But I've added your multidimensional approach to the answer. – Xorifelse Jul 09 '18 at 22:21