我试图对每个人做一些事情" Order1"在进入" Order2"之前的数组中。有没有办法在PHP中执行此操作? 注意:它并不总是被称为" Order1"。我现在只是将它作为通用术语使用。很可能是订单#
这是原始数组:
Array
(
[100 Series] => Array
(
[Order1] => 4
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
[50 Series] => Array
(
[Order1] => 4
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
)
以下是经过一次后应该是什么样子:
Array
(
[100 Series] => Array
(
[Order1] => 0
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
[50 Series] => Array
(
[Order1] => 0
[Order2] => 4
[Order3] => 4
[Order4] => 4
)
)
这里添加了一些逻辑......它使用了一些未提及的其他数组数据。
foreach($orders[$project] as $order => $hoursA)
{
if(array_key_exists($project, $orders) && !empty($orders[$project]))
{
if($resources[$name]["Schedule"][$day] >= $hoursA && $resources[$name]["Schedule"][$day] != 0)
{
$workFlow[$day][$project][$order][$name] = $hoursA;
$resources[$name]["Schedule"][$day] = (($resources[$name]["Schedule"][$day] - $hoursA < 0.000000001) ? 0 : $resources[$name]["Schedule"][$day] - $hoursA);
$orders[$project][$order] = 0;
} else if($resources[$name]["Schedule"][$day] < $hoursA)
{
$workFlow[$day][$project][$order][$name] = $resources[$name]["Schedule"][$day];
$orders[$project][$order] = $orders[$project][$order] - $resources[$name]["Schedule"][$day];
$resources[$name]["Schedule"][$day] = $resources[$name]["Schedule"][$day] - $resources[$name]["Schedule"][$day];
}
}
}
答案 0 :(得分:0)
$searchFlag = 'Order1';
foreach($arrays as $array)
{
foreach($array as $key => $val)
{
if($key == $searchFlag) {
//do something to make 0
$arrays[$array][$key] = 0;
}
}
}
答案 1 :(得分:0)
如果它始终是您需要设置为零的特定键:
array_walk_recursive(
$data,
function(&$value, $key) {
if ($key == 'Order1') $value = 0;
}
);
答案 2 :(得分:0)
这样的事情:
foreach ($arr as $key => &$value) {
$_key = array_search(reset($value), $value);
$value[$_key] = 0;
}
答案 3 :(得分:0)
建立Mark Baker的解决方案 - 但使其更适用于任意数量的任意键名:
function setOrderToZeroForKey( $orderKey ) {
array_walk_recursive( $data, function( &$value, $key ) {
if( $key === $orderKey ) $value = 0;
}
}
array_walk( array( 'Order1', 'Order2', 'Order3', 'Order4' ), setOrderToZeroForKey );
(可能无法很好地扩展 - 如果没有,请考虑更有效的数据结构)