Reputation: 1437
I have the following array and it is changeable from time to time.
Array
(
[For Sale] => For Sale
[Sold] => Sold
[To Let] => To Let
[Let] => Let
[Under Offer] => Under Offer
[Exchanged] => Exchanged
[Withdrawn] => Withdrawn
[Acquired] => Acquired
)
No matter what's the initial sequence was but when the page loads it should look like
Array
(
[For Sale] => For Sale
[Under Offer] => Under Offer
[Exchanged] => Exchanged
[Withdrawn] => Withdrawn
[Acquired] => Acquired
[Sold] => Sold
[To Let] => To Let
[Let] => Let
)
Basically these three elements should stay at the bottom of the array.
[Sold] => Sold
[To Let] => To Let
[Let] => Let
Any help is highly appreciated. Thanks in advance.
Upvotes: 1
Views: 2464
Reputation: 2995
Here I've used in_array()
to match the key with the given keys 'Sold','To Let','Let'
, then unset that key from $input
array and push that key value to the array.
<?php
$input = array(
'For Sale' => 'For Sale',
'Sold' => 'Sold',
'To Let' => ' To Let',
'Let' => 'Let',
'Under Offe' => 'Under Offer',
'Exchanged' => 'Exchanged',
'Withdrawn' => 'Withdrawn',
'Acquired' => 'Acquired'
);
foreach ($input as $key => $val) {
if (in_array($key, array('Sold', 'To Let', 'Let'))) {
unset($input[$key]);
$input[$key] = $val;
}
}
echo "<pre>";
print_r($input);
?>
This will Output :
Array
(
[For Sale] => For Sale
[Under Offe] => Under Offer
[Exchanged] => Exchanged
[Withdrawn] => Withdrawn
[Acquired] => Acquired
[Sold] => Sold
[To Let] => To Let
[Let] => Let
)
Upvotes: 1
Reputation: 92854
Short and effective solution using array_multisort
function with "custom order"(few dimensions sorting):
// $arr is your initial array
$custom_order = ['F'=> 0,'S' => 5,'T' => 6,'L' =>7,'U' => 1,'E' => 2,'W' => 3,'A' => 4];
array_multisort($custom_order, SORT_ASC, $arr);
print_r($arr);
The output:
Array
(
[For Sale] => For Sale
[Under Offer] => Under Offer
[Exchanged] => Exchanged
[Withdrawn] => Withdrawn
[Acquired] => Acquired
[Sold] => Sold
[To Let] => To Let
[Let] => Let
)
Or another(even better) approach using uksort
and array_search
functions:
$custom_order = ['For Sale','Under Offer','Exchanged','Withdrawn','Acquired','Sold','To Let','Let'];
uksort($arr, function($a, $b) use($custom_order){
return array_search($a,$custom_order) - array_search($b,$custom_order);
});
http://php.net/manual/en/function.uksort.php
Upvotes: -1
Reputation: 3407
You must format your data using foreach.
$data = [
'For Sale' => For Sale
'Sold' => Sold
'To Let' => To Let
'Let' => Let
'Under Offer' => Under Offer
'Exchanged' => Exchanged
'Withdrawn' => Withdrawn
'Acquired' => Acquired
];
$newData = [];
foreach($data as $key => $val) {
$newData[] = [
'For Sale' => $val['For Sale'],
'Under Offer' => $val['Under Offer'],
'Exchanged' => $val['Exchanged'],
'Withdrawn' => $val['Withdrawn'],
'Acquired' => $val['Acquired'],
'Sold' => $val['Sold'],
'To Let' => $val['To Let'],
'Let' => $val['Let']
]
}
print_r($newData);
Upvotes: 1
Reputation: 9583
Let you array name is $arr
.
First store the values in a variable and then unset them, after that use array_push
to store them at the last of the array.
$sold = $arr['Sold'];
unset($arr['Sold']);
$to_let = $arr['To Let'];
unset($arr['To Let']);
$let = $arr['Let'];
unset($arr['Let']);
array_push($arr, $sold, $to_let, $let);
Upvotes: 1