Search code examples
phparraysjsonvariablesundefined-index

How can i convert json data into php variables?


I have a json file which I want to get the data from and use it as php variables inside of a loop.

I keep getting Undefined index erros and it just does not work...

This is my code:

$file = file_get_contents("csv/products.json",'r');   

    for($i=0;$i<18;$i++){ 

    $datosArray = json_decode($file,true);

    //var_dump($datosArray);

   if (isset ($datosArray)){
    $id =  $datosArray["id"];
    $genderid = $datosArray["sex_id"];
    $dest =  $datosArray["destaque"];
    $cat =  $datosArray["categoria"];
    $marc =  $datosArray["Marca"];
    $name =  $datosArray["nombre"];
    $desc =  $datosArray["descripcion"];
    $pho1 =  $datosArray["photo_id1"];
    $pho2 =  $datosArray["photo_id2"];
    $pho3 =  $datosArray["photo_id3"];
    $dprice = $datosArray["D_price"];
    $oprice =  $datosArray["O_price"];
    }

    ?>

This is what part of my json looks like:

[
  {
    "id": 1,
    "sex_id": 101,
    "destaque": 1,
    "categoria": "Vestidos",
    "Marca": "Marfinno",
    "nombre": "Mono rayas",
    "descripcion": "Mono de rayas con botones y amarre  Marfinno",
    "photo_id1": "Female1.jpg",
    "photo_is2": "Female1.1.jpg",
    "photo_id3": "Female1.2.jpg",
    "D_price": "$100.00",
    "O_price": "$300.00"
  },
  {
    "id": 2,
    "sex_id": 101,
    "destaque": 0,
    "categoria": "Vestidos",
    "Marca": "Marfinno",
    "nombre": "Mono liso",
    "descripcion": "Mono liso con amarre  Marfinno",
    "photo_id1": "Female2.jpg",
    "photo_is2": "Female2.1.jpg",
    "photo_id3": "Female2.2.jpg",
    "D_price": "$100.00",
    "O_price": "$300.00"
  }, 

Thank you.


Solution

  • There is a mistake here:

    $file = file_get_contents("csv/products.json",'r'); 
    

    Simply put:

    $file = file_get_contents("csv/products.json"); 
    

    file_get_contents does not require a 'r' argument like when you open a file. See https://www.php.net/manual/en/function.file-get-contents.php


    Then from the comment about the var_dump, you have to see that you have an array of arrays.

    array(54) { 
        [0]=> array(12) { 
            ["id"]=> int(1) 
            ["sex_id"]=> int(101) 
            ["destaque"]=> int(1) 
            ["categoria"]=> string(8) "Vestidos" 
            ["Marca"]=> string(8) "Marfinno" 
            ["nombre"]=> string(10) "Mono rayas" 
            ["descripcion"]=> string(44) "Mono de rayas con botones y amarre Marfinno" 
            ["photo_id1"]=> string(11) "Female1.jpg" 
            ["photo_is2"]=> string(13) "Female1.1.jpg" 
            ["photo_id3"]=> string(13) "Female1.2.jpg" 
            ["D_price"]=> string(7) "$100.00" 
            ["O_price"]=> string(7) "$300.00" 
        } 
        [1]=> array(12) {
            ....
        }
    

    So when you want to reference an item, you have to put 2 indexes:

    ...
    $id =  $datosArray[$i]["id"];
    $genderid = $datosArray[$i]["sex_id"];
    ...
    

    This will work if you keep that portion of code inside the context of the for loop.