【问题标题】:Print Array Key Multidimensional within a Foreach在 Foreach 中打印多维数组键
【发布时间】:2016-05-09 12:40:42
【问题描述】:

我有一个数组(从 excel 文件中获取数据),我一直在尝试“合并”依赖于 ID 的数据。我设法创建了数组(将产品分组到 ID),结果如下。

Array ( [Order10] => Array ( [Mr Name] => Array ( [45 STREET] => Array ( [AREA] => Array ( [Product1] => 1 [Product2] => 3 ) ) ) ) )

我现在正在尝试提取数据(订单、名称、街道等),但我只设法使用 key(var) 提取 ID (Order10)。这是当前代码的基本布局。

//Gets Data from excel file
foreach ($bulk as $prod)
{
    $APIOrder[] = array('Order' => $prod['ORDER'],
                         'Prod' => $prod['PROD'],
                        'Quantity' => $prod['QTY'],
                        'Recipient_Name' => $prod['RECIPIENT_NAME']);
}

$newOptions = array();
foreach ($APIOrder as $option)
{
    $Order = $option['Order'];
    $Prod = $option['Prod'];
    $Qty = $option['Quantity']; //Added
    $Recipient_Name = $option['Recipient_Name'];
    $newOptions[$Order][$Recipient_Name][$Prod] = $Qty;
}

$index4 = 0;

foreach($newOptions as $key=>$val)
{
    $SeperateOptions = (array_slice($newOptions, $index4, true));
    //Print Array to check, Echo ID, Name, Product
    $index4++;
}

如果我在 foreach 循环中没有代码,我可以提取这些数据。这样做的问题是订单的重复取决于订单上有多少产品,这使得将订单插入数据库系统变得更加困难。下面的例子:

Array ( [Order10] => Array ( [Mr Name] => Array ( [45 STREET] => Array ( [AREA] => Array ( [Product1] => 1) ) ) ) )
Array ( [Order10] => Array ( [Mr Name] => Array ( [45 STREET] => Array ( [AREA] => Array ( [Product2] => 3) ) ) ) )

正如我所说,如果我不尝试将所有产品放入数组中,我可以提取数据,但是当数据被发送到数据库时,我必须创建一个循环来添加缺失的产品.任何方向都将不胜感激。

【问题讨论】:

  • 你不能这样做。在 PHP 中,数组键应该是唯一的,并且只能是字符串或整数。你现有的数组很好。
  • @lolka_bolka 感谢您的回复,因此请保留一个数组(包含多个产品)。我将如何“提取”地址等关键名称?这是我目前的方式遇到的问题。

标签: php arrays multidimensional-array


【解决方案1】:

这不是一个真正的答案,但基于 OP 和我之间的 cmets,我尝试向他展示我是如何做到的,它太长了,这里是代码格式。关键是订单的 id,数组中的所有内容,然后是大于 1 的值。 products 也是一个数组。尽量把相关的东西封闭起来。:

$orders = [
    'Order10' => [
        'name' => 'Mr Name',
        'address' => [
            'street' => 'street here',
            'country' => 'country here',
            'zipcode' => 'zipcode here'
        ],
        'phones' => [
            'mobile' => 'mobile phone here',
            'fax' => 'fax here',
            'workplace' => 'here'
        ],
        'products' => [
            'productId1',
            'productId2',
            //and so on
        ]
    ]
];

然后您可以像这样迭代您的订单数组以显示:

foreach ($orders as $order) {
        ?>
        <div>Name: <?php echo $order['name']; ?></div>
        <div>Address: <?php echo $order['address']['zipcode']; ?>, <?php echo $order['address']['country']; ?>, <?php echo $order['address']['street']; ?>    </div>
        <div>Products: 
            <?php
            foreach ($order['products'] as $procutId) {
                $Produt = new Procut($productId);
                ?>
                <div><?php echo $Product->getName(); ?></div>
                <?php
            }
            ?>
        </div>
        <hr>
        <?php
    }
}
?>

在我的示例中,我有一个Product 类,也许你没有使用类,但是当然,你可以创建一个getProcut($id) 函数来获取id 的产品,所以它可能是

$product = getProduct($procutId);
echo $product['name'];

这是你的决定。

【讨论】:

  • 再次感谢@lolka_bolka。产品类并不是真正需要的,因为它正在从 excel 文件中读取订单(我正在使用其他类)。我将研究您的示例并尝试对其进行调整。再次感谢您的帮助!
猜你喜欢
  • 2016-12-24
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2013-10-09
  • 2018-03-20
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多