0

I need to sort the following multidimensional array in php and thereafter get the respective key of any array that I want:

array:3 [▼
  0 => array:1 [▼
    4 => "404"
  ]
  1 => array:1 [▼
    5 => "373"
  ]
  2 => array:1 [▼
    6 => "305"
  ]
]

In the above case, I would like to get the position / key of 5 after sorting 404, 373 and 305.

The result of this should be value 2.

13
  • you want the last index number in response? Commented Aug 11, 2017 at 8:47
  • please add the result you want... Commented Aug 11, 2017 at 8:48
  • not the last index. the index of 5 after sorting Commented Aug 11, 2017 at 8:49
  • the result should be value 2 Commented Aug 11, 2017 at 8:49
  • Have you looked into Sorting Arrays and tried it yourself? There is a comparison of sorting functions. Commented Aug 11, 2017 at 8:52

1 Answer 1

2

Just do a foreach-loop:

$array = [
  0 => [
    4 => "404"
  ],
  1 => [
    5 => "373"
  ],
  2 => [
    6 => "305"
  ]
];

foreach ($array as $index => $arr) {
    if (array_key_exists(5, $arr)) {
        echo $index + 1;
        break;
    }
}

Demo: https://3v4l.org/HH54K

Even though PHP has a bunch of built in array-functions, they can't do everything, and they are sometimes much slower than a simple foreach.

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

Comments

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.