I have a problem with an `array, it has the following format:
$array = [{"id": 1,"nombre": "Disco Duro"}];
Currently I consume it in the following way $array[0]
by accessing the position of the array but what I am trying to do is use the $array
without the position.
The result that edseo obtain is the following:
$array = {"id": 1,"nombre": "Disco Duro"};
Without the []
, but without accessing the element[0]
So that you can access the element
[0]
you can do the following. based on your example: http://sandbox.onlinephpfunctions.com/code/f8abc47768f1c86ddb773b23a222108f5d931a5fEverything is OK, but if you just want to have the element
[0]
no longer put them[]
inside the foreach.Your code:
New code:
With that there will be no need to use access element
[0]
Why not use
array_shift()
.Example:
Result:
To pass the JSON to an associative array, you should use the PHP function called JSON_DECODE; I leave you the example of how it should be
Now so you can see the content, use the var_dump(); which will return the impression of the key and value
To remove the brackets that you mentioned in the comments, I can think of doing it as follows in two steps with the str_replace() function
You must use a
ArrayObject();
and then convert it to JSON format withjson_encode();
this prints only the JSON
Here you can see it Run!