我正在使用类似于此结构的索引 PHP 数组:
$result = Array(
[0] => Array([storepickup_id] => 3[sku] => 691294[inventory] => 33[reserve] => )
[1] => Array([storepickup_id] => 3[sku] => 692284[inventory] => 3[reserve] => )
[2] => Array([storepickup_id] => 3[sku] => 692289[inventory] => 4[reserve] => )
[3] => Array([storepickup_id] => 3[sku] => 693510[inventory] => 8[reserve] => )
[4] => Array([storepickup_id] => 3[sku] => 150893[inventory] => 2[reserve] => )
[5] => Array([storepickup_id] => 3[sku] => 150906[inventory] => 4[reserve] => )
)
我正在尝试使用预构建的 FOR EACH 语句过滤上述数组,如下所示:
<?php foreach ($_productCollection as $_product): ?>
<?php
echo "SKU: " . $_product->getSku();
echo "INVENTORY: " . array_column($result, 'inventory', $_product->getSku());
<?php endforeach; ?>
因此,如果 $_product-getSku() 产生 691294,我想从 PHP 数组的同一索引/行中检索相应的“库存”值。
FOR EACH 中的上述 array_column 不起作用。我还尝试了一个基于 array_filter 的函数,但未能产生正确的结果:
<?php foreach ($_productCollection as $_product): ?>
<?php
function my_function($item_values)
{
if ($item_values = $_product->getSku())
{
return true;
}
return false;
}
$sku = $_product->getSku();
print_r(array_filter($sku,"my_function"));
?>
<?php endforeach; ?>
使用上述多维数组时是否有更好的方法?提前致谢。