1

I want to compare the key values ​​in the array and if a match is found to increase the value of the element.

For this I use code:

// var_dump $all_array 
array(2) { 
    [0]=> array(6) { 
        [0]=> string(8) "art_7880"
        [1]=> string(11) "Арт.7880"
        [2]=> string(1) "1"
        [3]=> NULL
        [4]=> string(45) "png"
        [5]=> int(1372269755) 
    } 
    [1]=> array(6) { 
         [0]=> string(8) "art_7880"
         [1]=> string(11) "Арт.7880"
         [2]=> string(1) "1"
         [3]=> NULL
         [4]=> string(45) "png"
         [5]=> int(1372269874) 
    } 
}
// var_dump $count 
array(2) { [0]=> string(2) "10" [1]=> string(1) "1" }

// var_dump $product
array(2) { [0]=> string(10) "1372269755" [1]=> string(10) "1372269874" }

$count=$_POST['count'];
$product=$_POST['product'];
$count_arr_products=count($product);

for ($i=0; $i<=$count_arr_products; $i++){    
    foreach ($all_array as $keys => $elms) {
        if ($product[$i]==$elms[5]) {
            if($count[$i] > 0) {
                $elms[2] = $count[$i];
            } else {
                unset($keys);
            }
        }
    }
}

but step $elms[2] = $count[$i]; not work - in result value $elms[2] not change...

6
  • 2
    you cannot assign var_dump() output to variables. Use var_export(). Commented Jun 26, 2013 at 18:44
  • Isn't your script getting syntax errors from those bad array assignments? Commented Jun 26, 2013 at 18:46
  • He's just showing what those variable are. It's assigned by a $_POST. Commented Jun 26, 2013 at 18:47
  • i update question for better understand Commented Jun 26, 2013 at 18:52
  • Why are you doing the unset($keys) here? Commented Jun 26, 2013 at 19:04

1 Answer 1

3

You need to majke $elms a reference. By default it will be a copy of the sub-array, so assignment won't update the original array.

$all_array = array(array("art_7880", "Арт.7880", "1", NULL, "png", 1372269755),
                   array("art_7880", "Арт.7880", "1", NULL, "png", 1372269874));
$count = array("10", "1");
$product = array("1372269755", "1372269874");

$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count_arr_products = count($product);
for($i=0; $i<$count_arr_products; $i++){ // Use < not <=
  foreach ($all_array as $keys => &$elms) { // Use a reference so we can update it
    if ($product[$i]==$elms[5]){
      if ($count[$i] > 0) {
        $elms[2] = $count[$i];
      } else {
        unset($all_array[$keys]); // not unset($keys)
      }
    }
  }
}
var_dump($all_array);

Output:

array(2) {
  [0]=>
  array(6) {
    [0]=>
    string(8) "art_7880"
    [1]=>
    string(11) "Арт.7880"
    [2]=>
    string(2) "10"
    [3]=>
    NULL
    [4]=>
    string(3) "png"
    [5]=>
    int(1372269755)
  }
  [1]=>
  &array(6) {
    [0]=>
    string(8) "art_7880"
    [1]=>
    string(11) "Арт.7880"
    [2]=>
    string(1) "1"
    [3]=>
    NULL
    [4]=>
    string(3) "png"
    [5]=>
    int(1372269874)
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

<= should be < in the for loop.
unset($keys) should be unset($all_array[$keys] to remove that element from $all_array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.