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
)
2
  • For your example it's $array_products[0][0], provide some more code. Commented Oct 10, 2019 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]); Commented Oct 10, 2019 at 17:17

3 Answers 3

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,
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

1 Comment

Isn't this the same as @freeek's answer?
0

Hello you can just copy value to a new key:

$dados_cotacao_array['Products'] = $array_produtos[0];

10 Comments

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.
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 This code should do what you want. It doesn't do anything to the nested arrays, it just changes the top-level array.
@Diego provide exact php test data for us, please.
There it go! @freeek, take a look at the updated question
|
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.