Reputation: 531
I have this array $mergeArr
:
array (size=5)
'facebook' =>
array (size=3)
'facebook_enabled' => string '1' (length=1)
'facebook_url' => string 'https://www.facebook.com/' (length=25)
'facebook_order' => string '7' (length=1) //order element
'twitter' =>
array (size=3)
'twitter_enabled' => string '1' (length=1)
'twitter_url' => string 'https://www.twitter.com/' (length=24)
'twitter_order' => string '9' (length=1) //order element
'instagram' =>
array (size=3)
'instagram_enabled' => string '1' (length=1)
'instagram_url' => string 'https://www.instagram.com/' (length=26)
'instagram_order' => string '2' (length=1) //order element
'linkedin' =>
array (size=3)
'linkedin_enabled' => string '1' (length=1)
'linkedin_url' => string 'https://www.linkedin.com/' (length=25)
'linkedin_order' => string '5' (length=1) //order element
'pintrest' =>
array (size=3)
'pinterest_enabled' => string '1' (length=1)
'pinterest_url' => string 'https://www.pinterest.com/' (length=26)
'pinterest_order' => string '3' (length=1) //order element
I need to sort it according the *_order
element in each array.
I tried the code below:
CODE PHP:
array_multisort(array_column($mergeArr, '2'), SORT_ASC, $mergeArr);
Expected output order is: Instagram, Pinterest, Linkedin, Facebook, Twiter.
The error that I receive is the following
array_multisort(): Array sizes are inconsistent
Can you please tell me how can I sort this array so I get what I want?
Thanks in advance!
Upvotes: 0
Views: 248
Reputation: 11642
If the field to compare is always in the 3-th key you can do that with usort and array-values as:
usort($array, function($a, $b) {
$a = array_values($a);
$b = array_values($b);
return $a[2] > $b[2];
});
Live example: 3v4l
Upvotes: 3