1

I have 2 arrays to compare their indexes, how can i compare them by checking the empty indexes of first array and if so, then change the value of second array of same index.


it can be solved as 2D array by comparing the rows indexes.


array (size=5)
  0 => string '' (length=0)
  1 => string '10' (length=6)
  2 => string '' (length=0)
  3 => string '11' (length=3)
  4 => string '' (length=0)
array (size=5)
  0 => string '1' (length=4)
  1 => string '2' (length=11)
  2 => string '3' (length=11)
  3 => string '4' (length=11)
  4 => string '5' (length=10)

For instance, index 0 of first array is empty, so index 0 of second array incremented by 1. and so on

1 Answer 1

1

try this

 <?php
      $a=array('',10,11,'');
      $b=array(1,2,3,4,5);

      foreach($a as $k=>$v){
        if($v==''){

         $b[$k]=++$b[$k] ;
        }

        }

      print_r($b);

    ?>

output

Array ( [0] => 2 [1] => 2 [2] => 3 [3] => 5 [4] => 5 )

Sign up to request clarification or add additional context in comments.

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.