-1

代码:

$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" )
    // );
  }  
}

请告诉我如何增加价值?

4

1 回答 1

3

要修改您迭代的数组元素,您需要使用&来指示您希望引用由返回foreach,而不是元素的副本。另外,我建议分别用和替换"1"和。为了一个好习惯。"3"13

<?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);
于 2013-06-26T17:42:15.990 回答