0

I have a multidimensional array like that:

$array = array(
    1 => array(
         "name" => 'Jon',
         "year" => '2012'
    ),
    2 => array(
         "name" => 'Jack',
         "year" => '1900'
    ),
    3 => array(
         "name" => 'Lisa',
         "year" => '1900'
    ),
    4 => array(
         "name" => 'Ygritte',
         "year" => '1929'
    ),
);

All the items that have a year of '1900' should be put to the end of the array. What is a lightweight solution?

Desired result:

$array = array(
    1 => array(
         "name" => 'Jon',
         "year" => '2012'
    ),
    2 => array(
         "name" => 'Ygritte',
         "year" => '1929'
    ),
    3 => array(
         "name" => 'Jack',
         "year" => '1900'
    ),
    4 => array(
         "name" => 'Lisa',
         "year" => '1900'
    ),
);
2
  • "without changing the order of the other items" shouldn't the order in the desired output and input be the same then? Which rules do the swaps follow? Also what have you tried, a simple loop should already be able to do the trick Commented Jul 24, 2014 at 9:51
  • 1
    It is pretty trivial, so it is time to write a clever foreach loop. Hint: you'll need two temporary arrays with all chance. Commented Jul 24, 2014 at 9:51

4 Answers 4

1

I assume you don't care about your indexes because you rearrange them. First thing, you should probably start indexes in your array from 0 and not for 1.

You can use the following code:

$array = array(
    1 => array(
         "name" => 'Jon',
         "year" => '2012'
    ),
    2 => array(
         "name" => 'Jack',
         "year" => '1900'
    ),
    3 => array(
         "name" => 'Lisa',
         "year" => '1900'
    ),
    4 => array(
         "name" => 'Ygritte',
         "year" => '1929'
    ),
);


$array = array_values($array);


for ($i=0, $c = count($array); $i<$c; ++$i) {
    if ($array[$i]['year'] == '1900') {
        $array[] = $array[$i];
        unset($array[$i]);
    }

}

$array = array_values($array);



foreach ($array as $k => $v) {
    echo $k.' '.$v['name'].' '.$v['year']."<br />";

}

Result for this is:

0 Jon 2012
1 Ygritte 1929
2 Jack 1900
3 Lisa 1900

Of course if you want, you can change your keys adding 1 to each one (starting from the last element) so you have the same output as in question but I assume this one is enough for you.

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

Comments

0

The simplest approach could be something like this: Find the element that has year 1900 (iterating the loop) then,

For example working for index 3

$tmp = $myArray['3'];
unset($myArray['3'];
$myArray['3'] = $tmp;

$tmp is a temporary variable inside the loop.

Comments

0

Try using SplHeap

class MyQueue extends SplHeap
{
    public function compare($item1, $item2)
    {
        if ($item1['year'] === $item2['year']) return 0;
        return ($item1['year'] == 1900) ? -1 : 0;
    }
}

$queue = new MyQueue();
foreach ($array as $item) {
    $queue->insert($item);
}

$queue->top();
$array = array();
while($queue->valid()){
    $array[] = $queue->current();
    $queue->next();
}

print_r($array);

Comments

0

Hope this following code will help you:

function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
    $sorter[$ii]=$va[$key];
}
rsort($sorter);
foreach ($sorter as $ii => $va) {
    $ret[$ii]=$array[$ii];
}
$array=$ret;
}

aasort($array,"year");
print_r($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.