1

I have two arrays.

$array1 :

Array (
  [0] => Array (
      [time] => 100
      [text] => Hello
  )
  [1] => Array (
      [time] => 200
      [text] => World!
  )
  [3] => Array (
      [time] => 300
      [text] => Array1's There
  )
)

and more...

$array2 :

Array (
  [0] => Array (
      [time] => 50
      [text] => Hello
  )
  [1] => Array (
      [time] => 150
      [text] => World!
  )
  [3] => Array (
      [time] => 300
      [text] => Array2's There
  )
)

and more ...

$desiredResult :

Array (
  [0] => Array (
      [time] => 50
      [text] => Hello
  )
  [1] => Array (
      [time] => 100
      [text] => Hello
  )
  [2] => Array (
      [time] => 150
      [text] => World
  )
  [3] => Array (
      [time] => 200
      [text] => World
  )
  [4] => Array (
      [time] => 300
      [text] => Array1's There
  )
  [5] => Array (
      [time] => 300
      [text] => Array2's There
  )
)

I need to merge two array by time's numeric value, and if time's value the same, Array1's data first.

1 Answer 1

1

With below code you will get the result you want.

$finalArray = array();
if(count($array1) == count($array2)){
  for ($icount = 0; $icount < count($array1); $icount++) {
    if( ($array1[$icount] < $array2[$icount]) || ($array1[$icount] == $array2[$icount])){
      $finalArray[] = $array1[$icount];
      $finalArray[] = $array2[$icount];
    } else if($array1[$icount] > $array2[$icount]){
      $finalArray[] = $array2[$icount];
      $finalArray[] = $array1[$icount];
    }
  }
}

Also i have created function which can gives you result if anyone array contains more value then other array.

function mapArray($array1, $array2, $minCount, $maxCount, $maxCountFrom = ''){
  for ($icount = 0; $icount < $minCount; $icount++) {
    if( ($array1[$icount] < $array2[$icount]) || ($array1[$icount] == $array2[$icount])){
      $finalArray[] = $array1[$icount];
      $finalArray[] = $array2[$icount];
    } else if($array1[$icount] > $array2[$icount]){
      $finalArray[] = $array2[$icount];
      $finalArray[] = $array1[$icount];
    }
  }

  if(!empty($maxCountFrom)){
    if($maxCountFrom == '1'){
      for ($jcount = $icount; $jcount < $maxCount; $jcount++) {
        $finalArray[] = $array1[$jcount];
      }
    } else if($maxCountFrom == '2'){
      for ($jcount = $icount; $jcount < $maxCount; $jcount++) {
        $finalArray[] = $array2[$jcount];
      }
    }
  }
  return $finalArray;
}

$array1Count = count($array1);
$array2Count = count($array2);
if($array1Count > $array2Count){
  $result = mapArray($array1, $array2, $array2Count, $array1Count, '1');
} elseif($array1Count < $array2Count){
  $result = mapArray($array1, $array2, $array1Count, $array2Count, '2');
} elseif($array1Count == $array2Count){
  $result = mapArray($array1, $array2, $array2Count, $array2Count);
}

In this function where you can pass 2 array, count of the both array by minimum count and maximum count, and last parameter is to define which array has maximum value and from that array fetch all the remaining values to the final array.

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

3 Comments

Thank you very much for your answer. [time]'s value are irregular and I want merge two array by key [time]'s value in ascending order.
Now that I think about it, I just put it together and sort merged array by time's value in ascending order. Thanks @Yogendrasinh
Or you can add change in code by this line as $array1[$icount]['time'] when comparing in if condition.

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.