Displaying a single array item from JSON in PHP (NODE.JS and EXPRESS API) Displaying a single array item from JSON in PHP (NODE.JS and EXPRESS API) express express

Displaying a single array item from JSON in PHP (NODE.JS and EXPRESS API)


If i understand correctly , i think the most easy way is use the index to get the element in array

res.json(with_operatorCode[0]);


Of course, the [foo] denotes that it is an array.

You could address the item as [0], but that is more likely to cause an error, if no item exists in the filter:

On the php side:

$operator=json_decode($operatorJSON);echo $operator[0]->PPM->text;

On the Node side:

res.json(with_operatorCode[0]);

The smarter thing to do would be to handle it as an array:

$operator=json_decode($operatorJSON);if (is_array($operator) && count($operator)){   echo $operator[0]->PPM->text;}

Or, if filter may give you more than one:

$operator=json_decode($operatorJSON);foreach ($operator as $op){   echo $op->PPM->text;}


Try for this:

 function toObject(arr) {  var obj = {};  for (var i = 0; i < arr.length; ++i)    obj[i] = arr[i];  obj rv;}