0

I have an array whith the following content:

array (size=5)
  0 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string 'https://www.facebook.com' (length=24)
      2 => string '4' (length=1)    // order number
  1 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string 'https://www.twiiter.com' (length=23)
      2 => string '7' (length=1)   // order number
  2 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string 'https://www.instagram.com' (length=25)
      2 => string '9' (length=1)   // order number
  3 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string 'https://www.linkedin.com' (length=24)
      2 => string '2' (length=1)  // order number
  4 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string 'https://www.pinterest.com' (length=25)
      2 => string '1' (length=1)  // order number

I want to sort this array based on the number in the above code. (where is a written comment).

How can I do this?

So far I have written the following code but I do not know how to make it properly.

$arrMerge = array_merge($facebookURL, $twitterURL, $instagramURL, $linkedinURL, $pinterestURL);
$splitArr = array_chunk($arrMerge, 3);
Cristi
  • 531
  • 1
  • 8
  • 21
  • check this thread https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value – Giacomo M Jul 09 '19 at 10:25

2 Answers2

3

You can use array_multisort with array_column

array_multisort(array_column($arr, 2), SORT_ASC,$arr)

You can use SORT_ASC OR SORT_DESC as you required

Live DEMO

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
1

You can use usort.

usort($data, function($a, $b) {
    return $a[2] - $b[2];
});

You might also want to type-cast the result to an integer before-hand as it looks like your data is treated as a string.

usort($data, function($a, $b) {
    return (int)$a[2] - (int)$b[2];
});

This is also a possible duplicate of this question.

Mark
  • 1,852
  • 3
  • 18
  • 31