代码:
$record=array('test','test1','test2');
$product = $record[0];
$arr = array(
array( "test", "1", "test5" ),
array( "test5", "3", "test5" )
);
foreach ($arr as $keys => $elms) {
if($product==$elms[0]){
// here need to increase the value of the $elms[1] on one;
// for ex. if $product==$elms[0] in result need get next array:
// $arr = array(
// array( "test", "2", "test5" ),
// array( "test5", "3", "test5" )
// );
}
}
请告诉我如何增加价值?
答案 0 :(得分:3)
要修改迭代的数组元素,您需要使用&
表示您希望引用由foreach
返回,而不是复制。另外,我建议分别用"1"
和"3"
替换1
和3
。为了一个好习惯。
<?php
$record=array('test','test1','test2');
$product = $record[0];
$arr = array(
array( "test", "1", "test5" ),
array( "test5", "3", "test5" )
);
foreach ($arr as $keys => &$elms) { // here, the & is the key...
if($product==$elms[0]){
$elms[1]++;
}
}
print_r($arr);