Reputation: 2265
i have two arrays for example Array1:
Array(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
Array2:
Array
(
[0] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[1] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[5] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[6] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
)
the array 1 will be any one sub array from array 2,
now i want that in array 2 the first array should be that one which is array 1, after that those sub arrays which have venue id same as array 1 venue id should exist and then all other arrays. how to do this sorting? sorry if any english error i m not good in that.
expected output:
Array
(
[6] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[5] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[0] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[1] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
)
Upvotes: 4
Views: 142
Reputation: 7111
You can unset matching element from second array and than use function array_unshift($array, $element);
to preppend array.
$needle = array(
'id' => 50575,
'name' => 'Funbox - Pirates and Princesses',
'venue_id' => 3248
);
$stack = array(
[0] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[1] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[5] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[6] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
);
foreach ($stack as $k => $v) {
if ($v['id'] == $needle['id']) {
unset($stack[$k]);
array_unshift($stack, $needle);
break;
} else {
//echo 'No matching element in existing array.';
}
}
print_r($testArray);
output will be:
Array
(
[0] => Array
(
[id] => 50575
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[1] => Array
(
[id] => 53295
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[2] => Array
(
[id] => 53323
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[3] => Array
(
[id] => 53391
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[4] => Array
(
[id] => 53402
[name] => Funbox - Pirates and Princesses
[venue_id] => 2954
)
[5] => Array
(
[id] => 57130
[name] => Funbox - Pirates and Princesses
[venue_id] => 2980
)
[6] => Array
(
[id] => 57142
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[7] => Array
(
[id] => 50601
[name] => Funbox - Pirates and Princesses
[venue_id] => 3248
)
[8] => Array
(
[id] => 56113
[name] => Funbox - Pirates and Princesses
[venue_id] => 2882
)
)
Upvotes: 1
Reputation: 13544
This is a custom function should do what do you want:
function sortArr($haystack, $needle){
$out = array();
foreach ($haystack as $index => $arr){
if ($arr == $needle){
$out["$index"] = $arr;
unset ($haystack[$index]);
break;
}
}
foreach ($haystack as $index => $arr){
if ($arr['venue_id'] == $needle['venue_id']){
$out["$index"] = $arr;
unset($haystack[$index]);
}
}
if (is_array($haystack)){
return array_merge($out,$haystack);
}
}
The following is a DEMO LINK
Upvotes: 2
Reputation: 760
You can sort it to your example order using usort.
function cmp($a, $b) {
return $b["venue_id"] - $a["venue_id"];
}
usort($array2,"cmp");
All this does is sort descending by venue_id. So all arrays with the same venue_id will be grouped, with the highest venue_id at the top. It is a bit unclear from your answer if you wanted something else.
Edit: I have given this a bit more thought and come up with a function that should do what I think you're asking for. It's not perfect, but it should work.
function customSort($array1,$array2) {
global $primaryVenue_id;
$primaryVenue_id = $array1["venue_id"];
function cmp($a, $b) {
global $primaryVenue_id;
if ($a["venue_id"] == $primaryVenue_id) return -1;
elseif ($b["venue_id"] == $primaryVenue_id) return 1;
else return $b["venue_id"] - $a["venue_id"];
}
usort($array2, "cmp");
unset($primaryVenue_id);
return $array2;
}
Basically if you call the function customSort($array1,$array2) then it will return $array2, with all subarrays containing the same venue_id as $array1 first, followed by the rest descending order of id.
Upvotes: 1