2

I have this an array with this form

CODE PHP:

$mergeArr = array_merge($facebookURL, $twitterURL, $instagramURL, $linkedinURL, $pinterestURL); 

    array (size=15)
      'fbenabled' => string '1' (length=1)
      'fburl' => string 'https://www.facebook.com' (length=24)
      'fborder' => string '4' (length=1)         // order element
      'twenabled' => string '1' (length=1)
      'twurl' => string 'https://www.twiiter.com' (length=23)
      'tworder' => string '7' (length=1)         // order element
      'instaenabled' => string '1' (length=1)
      'instaurl' => string 'https://www.instagram.com' (length=25)
      'instaorder' => string '9' (length=1)      // order element
      'linkenabled' => string '1' (length=1)
      'linkurl' => string 'https://www.linkedin.com' (length=24)
      'linkorder' => string '2' (length=1)       // order element
      'pinenabled' => string '1' (length=1)
      'pinurl' => string 'https://www.pinterest.com' (length=25)
      'pinkorder' => string '1' (length=1)       // order element

I want to sort this array according to the "order" value of each element.

The new form of the array should be:

   array (size=15)
          'pinenabled' => string '1' (length=1)
          'pinurl' => string 'https://www.pinterest.com' (length=25)
          'pinkorder' => string '1' (length=1)       // order element
          'linkenabled' => string '1' (length=1)
          'linkurl' => string 'https://www.linkedin.com' (length=24)
          'linkorder' => string '2' (length=1)       // order element
          'fbenabled' => string '1' (length=1)
          'fburl' => string 'https://www.facebook.com' (length=24)
          'fborder' => string '4' (length=1)         // order element
          'twenabled' => string '1' (length=1)
          'twurl' => string 'https://www.twiiter.com' (length=23)
          'tworder' => string '7' (length=1)         // order element
          'instaenabled' => string '1' (length=1)
          'instaurl' => string 'https://www.instagram.com' (length=25)
          'instaorder' => string '9' (length=1)      // order element

Can you help me please order this array?

Thanks in advance!

1
  • Have you tried anything so far? If so what specific issues are you getting? Commented Jul 12, 2019 at 8:48

1 Answer 1

2

The best option would be to use asort.

asort($mergeArr);

Now that I'm taking a second look at your code I'm noticing that the order you want to achieve is actually incredible difficult to do, you want to group the twitter, instagram and facebook related elements without actually grouping them.

You might want to re-organise your array to look something like this:

$mergeArr = array(
    'facebook'  => $facebookURL, 
    'twitter'   => $twitterURL, 
    'instagram' => $instagramURL, 
    'linkedin'  => $linkedinURL, 
    'pintrest'  => $pinterestURL
);

usort($mergeArr, function($a, $b) {

    return $a['order'] - $b['order'];

});

In order for this to work, you would need to change the 'order' keys to the word 'order', e.g. change 'pinkorder' to 'order', 'linkorder' to 'order', etc.

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

4 Comments

ok but how to organize? I can share it from 3 to 3 but I do not want this because I can have a dynamic number of items. I can not use a constant, but need to print the array in a dynamic way
$splitArr = array_chunk($mergeArr, 3); for example I can do this, but number 3 is constant, I want something dynamic
You need to change the structure of your code in order to get this to work, the way it is currently setup is not logical, please see edit above.
can you please make an example in the "codepen" or somewhere because I do not understand exactly how to apply this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.