phparraysmultidimensional-arraygroupingassociative

Create and fill an associative array of multiples dimensions


I'm currently trying to create an associative array which will have two dimensions and I think a solution for this probleme could resolve problemes for array with more dimensions.

I recover data with an API which look like that :

   {
        "item_id": "89",
        "name": "Confiture de Myrtilles",
        "product_id": "737",
        "meta_key": "vmm_warehouse_sg_10783",
        "meta_value": "0"
    },
    {
        "item_id": "89",
        "name": "Confiture de Myrtilles",
        "product_id": "737",
        "meta_key": "vmm_warehouse_sg_10782",
        "meta_value": "0"
    },

    {
        "item_id": "91",
        "name": "Poires Guyot (bio)",
        "product_id": "690",
        "meta_key": "_backorders",
        "meta_value": "no"
    },
    {
        "item_id": "91",
        "name": "Poires Guyot (bio)",
        "product_id": "690",
        "meta_key": "_sold_individually",
        "meta_value": "no"
    },

I just want to create an array like this :

            array[item_id->[meta_key->meta_value]]

So I have to recover the item_id which will have the role of the secondth array and after put in this array the meta_key and meta_value associated.

So for example I will have an array like this :

Products[89]["vmm_warehouse_sg_10783"->"0" "vmm_warehouse_sg_10782"->"0"]

And an other like this :

Products[91][........]

At the end, I will have a final array like this :

Products [ [89]->{"vmm_warehouse_sg_10783"->"0","vmm_warehouse_sg_10782"->"0"}
           [91]->{.....}]

I have already tried something but I'm just a beginner and I don't found solution for my problem.

$Products = $this->wpdb->get_results( $SQL_Deliveries );
//this line allow $Products to recover all data from the API


foreach ( $Products as $Product ) {
    $Meta_products[] = Product->item_id;
    foreach($Product as $Product_meta){
    $Meta_products[$item_id]->{Product_meta->meta_key,Product_meta
        ->meta_value);
    }

I'm sure I did mitakes in my code too, but I really don't know how to resolve this problem. Thank you for your participation !


Solution

  • It looks like you want a multidimensional object array.

    There is a little bit of fiddling involved to declare nested objects. The curly bracing is also necessary.

    Code: (Demo)

    $products = [
        (object)["item_id"    => "89",
         "name"       => "Confiture de Myrtilles",
         "product_id" => "737",
         "meta_key"   => "vmm_warehouse_sg_10783",
         "meta_value" => "0"
        ],
        (object)["item_id"    => "89",
         "name"       => "Confiture de Myrtilles",
         "product_id" => "737",
         "meta_key"   => "vmm_warehouse_sg_10782",
         "meta_value" => "0"
        ]
    ];
        $result = (object)[];
        foreach($products as $product) {
            if (!isset($result->{$product->item_id})) {
                $result->{$product->item_id} = (object)[];
            }
            $result->{$product->item_id}->{$product->meta_key} = $product->meta_value; 
        }
    
    var_export($result);
    

    Output:

    (object) array(
       '89' => 
      (object) array(
         'vmm_warehouse_sg_10783' => '0',
         'vmm_warehouse_sg_10782' => '0',
      ),
    )
    

    Alternatively, to generate the nested object structure, you could build an array of arrays, then use json_encode(), then json_decode() on the result.


    If you want an array as output, that's easiest: Code: (Demo)

    $products = [
        (object)["item_id"    => "89",
         "name"       => "Confiture de Myrtilles",
         "product_id" => "737",
         "meta_key"   => "vmm_warehouse_sg_10783",
         "meta_value" => "0"
        ],
        (object)["item_id"    => "89",
         "name"       => "Confiture de Myrtilles",
         "product_id" => "737",
         "meta_key"   => "vmm_warehouse_sg_10782",
         "meta_value" => "0"
        ],
        (object)["item_id"    => "91",
         "name"       => "Poires Guyot (bio)",
         "product_id" => "690",
         "meta_key"   => "_backorders",
         "meta_value" => "no"
        ],
        (object)["item_id"    => "91",
         "name"       => "Poires Guyot (bio)",
         "product_id" => "690",
         "meta_key"   => "_sold_individually",
         "meta_value" => "no"
        ]
    ];
    
    $result = [];
    foreach($products as $product) {
        $result[$product->item_id][$product->meta_key] = $product->meta_value; 
    }
    
    var_export($result);
    

    Output:

    array (
      89 => 
      array (
        'vmm_warehouse_sg_10783' => '0',
        'vmm_warehouse_sg_10782' => '0',
      ),
      91 => 
      array (
        '_backorders' => 'no',
        '_sold_individually' => 'no',
      ),
    )