【问题标题】:How to assign a name for parent array in the array in PHP?如何在PHP中为数组中的父数组分配名称?
【发布时间】:2019-10-10 16:32:08
【问题描述】:

我有一个名为 $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
)

【问题讨论】:

  • 你的例子是$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]);

标签: php arrays multidimensional-array


【解决方案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,
    ),
  ),
)

【讨论】:

  • 这和@freeek 的回答不一样吗?
【解决方案2】:

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

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

【讨论】:

  • 我试过了,但没有按预期工作。它将带有值([重量]等)的第一个 0 数组更改为 Products,而 Array 1 保持为 1。所以基本上使用您的代码,它会删除第一个分层数组并将子数组替换为 0 到 Products。跨度>
  • 嗯.... 看看我展示的第一个代码。第一个键是[0] => Array。我展示的第二个代码,第一个键是 [Products] => Array 保持这个多维数组的父值和子值。
  • @Diego 这个代码应该做你想做的。它不会对嵌套数组做任何事情,它只是改变顶层数组。
  • @Diego 请为我们提供准确的 php 测试数据。
  • 好了! @freeek,看看更新后的问题
【解决方案3】:

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

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

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

而不是使用:

array_unshift($arr1, $arr2)

我用过:

$arr1 = $arr2 + $arr1;

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多