2

initial array is looks like this:

$arInitial = Array(
  0 => Array(1,2,3), 
  1 => Array(3,4),
  2 => Array(5,6,7,8),
  3 => Array(9)
);

The resulting array should be:

Array(1,3,5,9,2,4,6,3,7,8);

I was thinking about while(1) loop, but nothing. Any ideas?

3
  • So this is not just sorting. You also want to merge several arrays into a single one. Commented Jun 8, 2016 at 0:06
  • @Jocelyn I agree, it's not only sorting. I need some thoughts how can i implement this Commented Jun 8, 2016 at 0:08
  • a loop is the only option i see Commented Jun 8, 2016 at 0:13

2 Answers 2

4

There are myriad of ways to get this, there's already a collection of array incantation function that does this, I just can't find the dup question yet, but another way is just to array_shift each batch:

$result = array();
$max = count($arInitial);
for($i = 0; $i < $max; $i++) {
    foreach($arInitial as &$a) {
        if(!empty($a)) {
            $e = array_shift($a);
            $result[] = $e;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should try two functions:

Probably you want something like

$finalArray = array();
foreach($arInitial as $array){
    $finalArray = array_merge($finalArray, $array);
}

var $finalArray = array_unique($finalArray , SORT_NUMERIC);

That way you should get the result you want.

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.