0

I have 2 arrays

$array1 = array(22,193,124);
$array2 = array(array('id'=>22, 'count'=> 1), array('id'=>124, 'count'=>2));

Now I need to search in $array2 for id from $array1 and if found to increment the count value and also add to the array the one's which are not found with a count as 1 so my resulting array would be

$arr = array(array('id'=>22, 'count'=> 2), array('id'=>124, 'count'=>3), array('id'=>193, 'count'=>1));

any help would be appreciated

The current code which I tried is

if($array2){
            foreach($array1 as $array){

                if(in_array($array, array_column($array2, 'id'))){                  
                    $wa_array['count'] += 1;
                } else {
                    $wa_array['id'] = $array;
                    $wa_array['count'] = 1;
                }
            }

        } else {
            foreach($array1 as $array){
                $wa_array['id'] = $array;
                $wa_array['count'] = 1;
            }
        }
6
  • That's not valid PHP code. Commented Aug 24, 2018 at 18:43
  • Why? It Is valid. Commented Aug 24, 2018 at 18:45
  • You either need to remove the array keywords or replace [ and ] with ( and ). Commented Aug 24, 2018 at 18:50
  • Hint: Use a for loop to iterate over $array1, Use in_array() to test if the id is in $array1, and if it is, increment the count. Commented Aug 24, 2018 at 18:53
  • @Barmar tried with for loop too but doesnt work as needed Commented Aug 24, 2018 at 18:59

4 Answers 4

1

This may be something you are looking for -

$array1 = array(22,193,124);
$array2 = array(array('id'=>22, 'count'=> 1), array('id'=>124, 'count'=>2));

foreach($array1 as $key=>$digit)
{
  $keyFound = array_search($digit, array_column($array2, 'id'));

  if($keyFound === false)
    {
      array_push($array2, ['id'=>$digit, 'count'=>1]);
    }
  else
    {
      $array2[$keyFound]['count']++;
    }
}

print_r($array2);
Sign up to request clarification or add additional context in comments.

Comments

0

The question is not so clear, so I will go with my understanding : You need to check if values inside the first array are in the second array. If yes, increment the count value of that second array, if not, create that element with the value of 1.

This code is not tested, hope this can help find the good solution.

  foreach($array1 as $value){
     searchForId($value,$array2);
  }

  function searchForId($id, $array) {
    foreach ($array as $key => $val) {
      if ($val['id'] === $id) {
       $val['count'] += 1;
      }else{
        array_push(array('id'=>$id,'count'=>1))
      }
    }
  return null;
 }

Comments

0

you should loop through $array2, not $array1. Finding the value in array_column($array2, 'id') doesn't tell you the index of the array element to increment.

foreach ($array2 as &$item) {
    if (in_array($item['id'], $array1)) {
        $item['count']++;
    }
}

Note that you have to use &$item to make this a reference to the original array element, so that modifying it will update $array2. Otherwise, $item would be a copy of the array element.

If $array1 is large, it would be better to convert it to an associative array so you can test membership more efficiently.

$array1_assoc = array_flip($array1);

Then the test in the loop becomes:

if (isset($array1_assoc[$item['id']]) {

Comments

0

Check this one. NOTE: But this has O(n^2) complexity.

$array1 = [22,193,124];

$array2 = [['id'=>22, 'count'=> 1], ['id'=>124, 'count'=>2]];

foreach ($array1 as $value) {
    $isFound = false;
    foreach ($array2 as &$item) {
        if ($value == $item['id']) {
           $item['count']++;
           $isFound = true;
           continue;
        }
    }
    if ($isFound == false) {
       $array2 [] = ['id'=>$value, 'count'=> 1];
    }
}

var_dump($array2);

Comments

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.