-1

i tried to getting data array in array in array. have 3 level. if array in array i get it. but 3 level i can't. i want get attribute name and option in product id 315. i was successed get id variations 316 on product 315.

this sample data i tried to get

stdClass Object
(
[products] => Array
    (
        [0] => stdClass Object
            (
                [title] => Banner
                [id] => 315
                 [variations] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 316
                                [created_at] => 2019-04-13T09:21:55Z
                                   [attributes] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [name] => bahan
                                                [slug] => bahan
                                                [option] => Flexi 240gsm
                                            )

                                        [1] => stdClass Object
                                            (
                                                [name] => finising
                                                [slug] => finising
                                                [option] => Mata Ayam
                                            )

                                    )
                                  )
                     )
                 )
                 )
                 )

this a little my script..

<?php

 $values     = array();
 foreach ($results->products as $result) 
 {
   foreach ($result->variations as $varia) 
    {
    $values[] = "(  
                    '".$varia->id."', //ok
                    '".$result->id."', //ok
                    '".$varia->$attributes->$name."' //error
                ")
  }
}

?>

2 Answers2

1

Attributtes its an array. You need other loop

<?php

 $values     = array();
 foreach ($results->products as $result) 
 {
   foreach ($result->variations as $varia) 
    {
        foreach($varia->attributes as $attrib)
        {
              $values[] = "(  
                    '".$varia->id."', //ok
                    '".$result->id."', //ok
                    '".$attrib->name."'
                ")

        }

  }
}
lfpp
  • 167
  • 1
  • 4
1

You made two mistakes: first attributes is an array, you have to loop over it.

Second, you are trying to access the attributes and name fields with an incorrect notation (the $ in front of the field name is another functionality called dynamic variables, or variable variables)

<?php

$values = [];
foreach ($results->products as $result) {
    foreach ($result->variations as $varia) {
        foreach ($varia->attributes as $attribute) {
            $values[] = "(
                    '" . $varia->id . "', //ok
                    '" . $result->id . "', //ok
                    '" . $attribute->name . "''
                )";
        }
    }
}
Entilore
  • 170
  • 11
  • If you still has issues with it, maybe you can try to dump your outputs? You probably nows [print_r](https://www.php.net/manual/function.print-r.php), which can help you debugging this type of problems – Entilore Sep 16 '19 at 09:43
  • it works. thank you. but the output become more than one. it should just one product have 2 atribute. when use foreach more it become 2 product. – Beny Bond Banjarnahor Sep 16 '19 at 09:45
  • Well yeah, because each different attributes has a different name. – Entilore Sep 16 '19 at 09:57
  • can u share idea get 1 product and have name with two attribute in just one row in one table. because if my idea is insert product in product table and attribute in another table and when i get the data just join. – Beny Bond Banjarnahor Sep 16 '19 at 15:06