0

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!

dWinder
  • 11,597
  • 3
  • 24
  • 39
Cristi
  • 531
  • 1
  • 8
  • 21
  • The `$mergeArr` doesn't have key "2" - so it is size 0 and the original `$mergeArr` is size 5 -> that why the inconsistent – dWinder Jul 15 '19 at 07:19
  • ok and how should I write the new form of code? – Cristi Jul 15 '19 at 07:20
  • I am little bit confuse: what is you input? the first (bigger) array you shared on the one in the code example? according to what do you need the sort? – dWinder Jul 15 '19 at 07:22
  • - in the first example I present the form of array before sorting. – Cristi Jul 15 '19 at 07:25
  • Possible duplicate of [array\_multisort(): Array sizes are inconsistent](https://stackoverflow.com/questions/22247844/array-multisort-array-sizes-are-inconsistent) – MH2K9 Jul 15 '19 at 07:25
  • What I want to get is to sort the $ mergeArr array based on element order values ( where it writes a comment) – Cristi Jul 15 '19 at 07:27
  • Where does this array come from to begin with? Ask the person who came up with this why they are making thins deliberately(?) harder than they had to be, by including the platform name in the array keys, that seems to make rather little sense. – misorude Jul 15 '19 at 09:03

1 Answers1

3

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

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • ok but this example also works with more than two elements? can you please fill out the example with all 5 values? – Cristi Jul 15 '19 at 07:38