0

Element 1 of the following array represents the element with the greatest x value, and element 2 represents the element with the smallest x element.

Without looping over each array using PHP code, how can the index of the element with the greatest and smallest x be determined?

$array = [
    ['x'=>5,'y'=>3],
    ['x'=>9,'y'=>3],
    ['x'=>3,'y'=>3],
    ['x'=>7,'y'=>3]
];
7
  • 1
    You have to loop over each element how else do you know if there is a bigger value than you found?! Commented Sep 9, 2016 at 15:57
  • 2
    Just because you don't see it in the code doesn't mean array_column() does not loop through the entire array, because it does. And you could use a simple foreach loop and then you would only loop over the array once and not like with array_column() twice. Commented Sep 9, 2016 at 15:58
  • 1
    To be more exact, if you use array_column() you not only have to loop over the array once to get the column, but also need to use min() and max(), which also needs to loop over the entire array. So in total you iterate over your array 3 times, just because you don't want to see "the loop" in your code. You could use a simple foreach loop and do everything in just one loop! Commented Sep 9, 2016 at 16:02
  • 1
    By using the restriction: Without looping over each - do you mean: 1) don't use a foreach loop statement 2) Use some instructions that don't compare adjacent entry values? 1) is satisfied using the 'array_*' functions. 2) Is not possible with any computer that uses a standard CPU and memory. To make it clear - all conventional computers always loop over the data. Commented Sep 9, 2016 at 16:08
  • 1
    @RyanVincent I should have said "not looping over using PHP code". Commented Sep 9, 2016 at 16:12

1 Answer 1

4

Extract the x column and compute it:

$x = array_column($array, 'x');
$min = min($x);
$max = max($x);

Or, as JustOnUnderMillions points out:

list($min, $max) = [min($x=array_column($array, 'x')), max($x)];

To get the index of the array containing that value, just use array_search(). It will return the first key if there are multiple identical values:

$x = array_column($array, 'x');
$min = array_search(min($x), $x, true);
$max = array_search(max($x), $x, true);

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column()

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

10 Comments

Please use list() to finally get a one-liner ;-)
An answer without array_column() isn't an answer right? :P Also you could include a simple foreach loop with which OP would only need to iterate the array once.
Actually, I am trying to get the index of the element (i.e. 1 for greatest and 2 for smallest), and not the value.
@Rizier123 Sure, array_map with function($a)use(&$min,&max){} would work also. Maybe the task was just a little bit to easy. :)
@user1032531 Opps, but nobody saw it like this! And for that you need another code snippet.
|

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.