如何在PHP中为数组中的父数组指定名称?

如何在PHP中为数组中的父数组指定名称?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个名为$array\u products的数组,这是它现在在打印中的外观: [0] => Array ( [0] => Array ( [weight] => 297 [height] => 40 [width] => 60 [le

我有一个名为$array\u products的数组,这是它现在在打印中的外观:

[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
                )

        )
如何使第一个父数组具有名称而不是名称? 这就是我试图实现的保持多维结构:

[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
                )

        )
因为我将使用array_unshift使这个数组位于另一个数组的顶部。 我不知道array_map是否是我要找的,但我还没有找到一种方法

-编辑

通过使用:

$array_products['Products'] = $array_products[0];
unset($array_products[0])
根据@freeek的建议,我得到的是:

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

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

)
它基本上删除了将子数组移动到顶部的父数组,并将第一个数组0重命名为Products=/

--这是实际的PHP缩写:

// 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);
最后,这就是我想要实现的目标:

    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
)

您好,您只需将值复制到新密钥:

$dados_cotacao_阵列['Products]=$array_produtos[0];
以下几点对我很有用:

$a['test'] = $a[0];
unset($a[0]);
以下是用新行分隔前后的数组结果: 原件:

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,
    ),
  ),
)
修改:

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,
    ),
  ),
)

好的。。。。所以最大的问题不是制作阵列。 正在合并它

这就解决了问题:

创建新键(无论是否为数字键)

而不是使用:

array_unshift($arr1, $arr2)
我用过:

$arr1 = $arr2 + $arr1;

我试过了,但没有达到预期效果。它将值为[weight]的前0数组更改为Products,而数组1保持为1。因此,基本上在您的代码中,它删除了第一个层次结构数组,并将子数组替换为0到产品。。。。请看我显示的第一个代码。第一个键是[0]=>Array。我展示的第二个代码,第一个键是[Products]=>Array,它保留这个多维数组的父值和子值。@Diego这个代码应该做你想做的事情。它对嵌套数组没有任何作用,只是更改了顶级数组。@Diego请为我们提供准确的php测试数据。好了@freeek,看看更新后的问题,这与@freeek的答案不一样吗?例如$array_products[0][0],请提供更多的代码。我的示例有:$array_products[0]是父项,$array_products[0][0]是第一个子项,最后$array_products[0][1]是最后一个子项。我希望将$array_products[0]重命名为$array_products[products],以便有:$array_products[products][0]和$array_products[products][1],我可以使用array_unshift$first_array,$array_products[products];