0

I have an array called $array_products, this is how it looks right now in print_r:

[0] => Array
        (
            [0] => Array
                (
                    [weight] => 297
                    [height] => 40
                    [width] => 60
                    [lenght] => 540
                    [price] => 5975
                )

            [1] => Array
                (
                    [weight] => 75
                    [height] => 40
                    [width] => 60
                    [lenght] => 222
                    [price] => 3351
                )

        )

How do I make the first parent array have a name instead of one? This is what I'm trying to achieve (keeping the multidimensional structure):

[Products] => Array
        (
            [0] => Array
                (
                    [weight] => 297
                    [height] => 40
                    [width] => 60
                    [lenght] => 540
                    [price] => 5975
                )

            [1] => Array
                (
                    [weight] => 75
                    [height] => 40
                    [width] => 60
                    [lenght] => 222
                    [price] => 3351
                )

        )

Because I will use array_unshift to make this array in the top of another array. I don't know if array_map is what I'm looking for, but I haven't found a way to do that.

--Edit

By using:

$array_products['Products'] = $array_products[0];
unset($array_products[0])

As suggested by @freeek this is what I get:

(
    [1] => Array
        (
            [weight] => 75
            [height] => 40
            [width] => 60
            [lenght] => 222
            [price] => 3351
        )

    [Products] => Array
        (
            [weight] => 297
            [height] => 40
            [width] => 60
            [lenght] => 540
            [price] => 5975
        )

)

It basically removed the parent array moving the childs to the top, and renamed the first array 0 to Products. =/

--- This is the actual PHP (shortened):

// First array is created here:
foreach ( $package['contents'] as $item_id => $values ) {
            $product = $values['data'];
            $qty = $values['quantity'];

$shippingItem = new stdClass();

if ( $qty > 0 && $product->needs_shipping() ) {
        $shippingItem->peso = ceil($_weight);
        $shippingItem->altura = ceil($_height);
        $shippingItem->largura = ceil($_width);
        $shippingItem->comprimento = ceil($_length);
        $shippingItem->valor = ceil($product->get_price());
....
}

//This is the second part of the array, outside the first one:
        $dados_cotacao_array = array (
        'Origem' => array (
            'logradouro' => "",
            'numero' => "",
            'complemento' => "",
            'bairro' => "",
            'referencia' => "",
            'cep' => $cep_origem
        ),
        'Destino' => array (
            'logradouro' => "",
            'numero' => "",
            'complemento' => "",
            'bairro' => "",
            'referencia' => "",
            'cep' => $cep_destino
        ),
        'Token' => $this->token
        );


// Then I merge the first array with the second one
array_unshift($dados_cotacao_array, $array_produtos);

// And encode in json to send everything via cURL Post to an external API
$dados_cotacao_json = json_encode($dados_cotacao_array);

In the end this is what I'm trying to achieve:

    Array
        (
    [Products] => Array
            (
                [0] => Array
                    (
                        [weight] => 297
                        [height] => 40
                        [width] => 60
                        [lenght] => 540
                        [price] => 5975
                    )

                [1] => Array
                    (
                    [weight] => 75
                        [height] => 40
                        [width] => 60
                        [lenght] => 222
                        [price] => 3351
                    )

            )
    [Origem] => Array
        (
            [logradouro] => 
            [numero] => 
            [complemento] => 
            [bairro] => 
            [referencia] => 
            [cep] => 1234567
        )

    [Destino] => Array
        (
            [logradouro] => 
            [numero] => 
            [complemento] => 
            [bairro] => 
            [referencia] => 
            [cep] => 1234567
        )

    [Token] => token
)
Diego
  • 145
  • 4
  • 14
  • For your example it's `$array_products[0][0]`, provide some more code. – freeek Oct 10 '19 at 17:04
  • My example have: `$array_products[0]` which is the parent one, `$array_products[0][0]` which is the first child, and finally `$array_products[0][1]` as the last child. My wish is to rename `$array_products[0]` to `$array_products[Products]` in order to have: `$array_products[Products][0]` and `$array_products[Products][1]` which I would be able to use `array_unshift($first_array, $array_products[Products]);` – Diego Oct 10 '19 at 17:17

3 Answers3

1

Following worked for me:

$a['test'] = $a[0];
unset($a[0]);

Here is array result before and after separated by a new line: Original:

array (
  0 => 
  array (
    0 => 
    array (
      'weight' => 297,
      'height' => 40,
      'width' => 60,
      'lenght' => 540,
      'price' => 5975,
    ),
    1 => 
    array (
      'weight' => 75,
      'height' => 40,
      'width' => 60,
      'lenght' => 222,
      'price' => 3351,
    ),
  ),
)

Modified:

array (
  'test' => 
  array (
    0 => 
    array (
      'weight' => 297,
      'height' => 40,
      'width' => 60,
      'lenght' => 540,
      'price' => 5975,
    ),
    1 => 
    array (
      'weight' => 75,
      'height' => 40,
      'width' => 60,
      'lenght' => 222,
      'price' => 3351,
    ),
  ),
)
0

Hello you can just copy value to a new key:

$dados_cotacao_array['Products'] = $array_produtos[0];
freeek
  • 985
  • 7
  • 22
  • I tried that but didn't work as expected. It's changing the first 0 array with values ([weight], etc) to Products, while the Array 1 stays as 1. So basically with your code, it deletes the first hierarchical array and replaces the child one with 0 to Products. – Diego Oct 10 '19 at 16:45
  • Well.... Look at the first code I've shown. The first key is `[0] => Array`. The second code I've shown, the first key is `[Products] => Array` keeping the parent and child values of this multidimensional array. – Diego Oct 10 '19 at 16:49
  • @Diego This code should do what you want. It doesn't do anything to the nested arrays, it just changes the top-level array. – Barmar Oct 10 '19 at 16:54
  • @Diego provide exact php test data for us, please. – freeek Oct 10 '19 at 16:58
  • There it go! @freeek, take a look at the updated question – Diego Oct 10 '19 at 17:10
  • @Diego still the same, everything depends on products array creation. – freeek Oct 10 '19 at 17:37
  • Gosh.... I have no idea what to do. I think I will rewrite the code of the first array, so I don't have to face this problem. Even with your edited answer, the same error applied, the parent array is not renaming. Thanks anyway! :) – Diego Oct 10 '19 at 17:41
  • @Diego, then use without `[0]` – freeek Oct 10 '19 at 17:44
  • @freeek you aren't going to believe, but without the [0] it worked, rofl. But now I have 2 parents, one called [0] and then comes the [Products] inside [0]. So it can't resolve part of the problem, but "created" another. I have to get rid of this first [0] – Diego Oct 10 '19 at 17:49
  • @Diego something wrong with your example then. Debug both arrays you want to merge and post `var_dump`s. – freeek Oct 10 '19 at 17:52
0

OK.... so the big problem was not making the array. Was merging it....

This solved the problem: https://stackoverflow.com/a/6417137/5240406

array_unshift() creates new keys, if numeric or not, as mentioned here

Instead of using:

array_unshift($arr1, $arr2)

I used:

$arr1 = $arr2 + $arr1;
Diego
  • 145
  • 4
  • 14