Good Morning All, and HAPPY NEW YEAR to everyone, whether you celebrate it or not...

I have what should be a relatively simple question that I would like to pose that could avoid a very lengthy one...

if I create an associative array and place several key>value pairs in the array, will they remain in the order that they are placed, or will they be re-ordered in any way?

I want to create the array and then run an array_search on the $values to find the minimum value in the array.

I guess that would make a second question... I know I can do an array search on an array that I push values onto, but that isn't an associative array, so not sure I can even do that..

Any feedback would be greatly appreciated and would most likely save me hours of research time.

thanks
Douglas

Dani AI

Generated

Short answer: PHP associative arrays preserve insertion order (they are implemented as ordered maps), so keys stay in the order you added them unless you explicitly reorder or re-index the array. This matches ’s reply and can be confirmed with a dump as suggested.

If the goal is “find the minimum value and its key”, common approaches:

  • Built-ins: min($arr) returns the smallest value. To get all keys that have that value use array_keys($arr, $minValue, true). To get the first key only use array_search($minValue, $arr, true) but remember array_search returns false when not found — check with === false to avoid confusing key 0 with not-found.

Example to get every key with the minimum value:

$min = min($arr);
$keys = array_keys($arr, $min, true);

For a single-pass, slightly faster method (one loop, useful for very large arrays or custom comparisons):

$minKey = null;
$minVal = null;
foreach ($arr as $k => $v) {
  if ($minKey === null || $v < $minVal) {
    $minVal = $v;
    $minKey = $k;
  }
}

Caveats and troubleshooting:

  • Be careful about types: numeric strings vs numbers can yield surprising results; cast to int/float if needed before comparing.
  • Duplicate minimums: array_search returns the first match; use array_keys to get all matches.
  • Sorting/reindexing functions change order or keys: sort(), rsort(), usort() reindex numeric keys; asort()/arsort() preserve key association but reorder the array; unset() then re-adding a key moves it to the end while updating an existing key does not change its position.

This expands on the earlier replies and shows both quick built-in methods and a robust single-pass option.

Recommended Answers

All 4 Replies

Happy New Year you too.

First part of question you can make answered with just output of array:

$myArray = array('zero' => '0', 'first' => '1st', 'second' => '2nd', 'third' => '3rd');
print_r($myArray);

Second part of question you can solve with function min but also depends of type of variables you have in values of array.

Hello Douglas and a happy new year,

if I create an associative array and place several key>value pairs in the array, will they remain in the order that they are placed, or will they be re-ordered in any way?

They will remain in the order that you inserted them to the array , except if you want to re-ordered the associative array in any way through various PHP function (e.g. ksort asort)

I know I can do an array search on an array that I push values onto, but that isn't an associative array, so not sure I can even do that..

The same way … array_search will give the index of the array or false if not any matching your criteria.

Thank you Both for your responses...

jkon, I appreciate precise answers, and I believe that your answers were exactly what I was looking for...

I should know how it works for me in the morning and will post the results before marking this as solved...

Thanks again
Douglas

Thanks again jkon.

that was exactly what I needed to solve the problem.

working fine

Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.