如何在PHP数组中为父数组分配名称?

时间:2019-10-10 16:32:08

标签: php arrays multidimensional-array

我有一个名为$ array_products的数组,这就是现在在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
                )

        )

如何使第一个父数组使用名称而不是名称? 这就是我要实现的目标(保持多维结构):

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

3 个答案:

答案 0 :(得分:1)

以下为我工作:

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

答案 1 :(得分:0)

您好,您可以将值复制到新键:

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

答案 2 :(得分:0)

好的....所以最大的问题不是制作数组。 正在合并它。...

这解决了问题: https://stackoverflow.com/a/6417137/5240406

array_unshift()创建新密钥,无论是否为数字,as mentioned here

代替使用:

array_unshift($arr1, $arr2)

我用过:

$arr1 = $arr2 + $arr1;