Show the first element of an array

3

I have a problem with an 'array, it has the following format:

$array = [{"id": 1,"nombre": "Disco Duro"}];

I currently consume it in the following way $array[0] accessing the position of the array but what I try is to use the $array without the position.

The result that I obtained is the following:

$array = {"id": 1,"nombre": "Disco Duro"}; 

Without the [] , but without accessing the element [0]

    
asked by Carlos Mendez 05.08.2018 в 03:38
source

4 answers

4

So that you can access the [0] element you can do the following. based on your example: link

Everything is OK, but if you just want to have the [0] element, do not put the [] within the foreach.

Your code:

foreach (json_decode($array, true) as $value){
    $simpleArray[] = $value;
}

New code:

foreach (json_decode($array, true) as $value){
    $simpleArray = $value;
}

With that there will be no need to use access to the [0]

element     
answered by 05.08.2018 / 04:31
source
1

Why not use array_shift() .

  

array_shift () Removes the first value from the array and returns it ...

Example:

$array = ['{"id": 1,"nombre": "Disco Duro"}']; 
$producto = json_decode(array_shift($array));

var_dump($producto); 

//acceso a datos 
echo 'Nombre: '.$producto->nombre;
echo 'id: '.$producto->id;

Result:

object(stdClass)#1 (2) { ["id"]=> int(1) ["nombre"]=> string(10) "Disco Duro" } 

Nombre: Disco Duro
id: 1
    
answered by 06.08.2018 в 12:18
0

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

echo $array = '[{"id": 1,"nombre": "Disco Duro"}]';
$nuevo = JSON_DECODE($array, true);

Now for you to see the content, use the var_dump () function; that will give you back the impression of the key and value

echo $array = '[{"id": 1,"nombre": "Disco Duro"}]';
$nuevo = JSON_DECODE($array, true);
var_dump($nuevo);

/*imprime lo siguiente
[{"id": 1,"nombre": "Disco Duro"}]array(1) { [0]=> array(2) { ["id"]=> 
int(1) ["nombre"]=> string(10) "Disco Duro" } }*/
  

Now I mention the JSON that comes in your variable $array(); you should   put it in single quotes to be valid at the time of   transform it

  

UPDATE

To remove the brackets you mentioned in the comments, it occurs to me to do the following in two steps with the function str_replace ()

$array = '[{"id": 1,"nombre": "Disco Duro"}]';

$arrayUno = str_replace("[", "", $array);

$arrayDos = str_replace("]", "", $arrayUno);

echo $arrayDos;
//{"id": 1,"nombre": "Disco Duro"}

echo "<br>";

$nuevo = JSON_DECODE($arrayDos, true);

var_dump($nuevo);
//array(2) { ["id"]=> int(1) ["nombre"]=> string(10) "Disco Duro" }
    
answered by 05.08.2018 в 03:58
0

You must use a ArrayObject(); and then convert it to JSON with json_encode();

$array = new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
$array->id = 1;
$array->nombre = "Disco Duro";
$myJSON = json_encode($array);
echo $myJSON;

this prints only the JSON

{"id":1,"nombre":"Disco Duro"} 

Here you can see Run!

    
answered by 05.08.2018 в 04:34