How to Check If PHP Array Is Associative or Sequential

How to check if PHP array is associative or sequential?

You have asked two questions that are not quite equivalent:

  • Firstly, how to determine whether an array has only numeric keys
  • Secondly, how to determine whether an array has sequential numeric keys, starting from 0

Consider which of these behaviours you actually need. (It may be that either will do for your purposes.)

The first question (simply checking that all keys are numeric) is answered well by Captain kurO.

For the second question (checking whether the array is zero-indexed and sequential), you can use the following function:

function isAssoc(array $arr)
{
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}

var_dump(isAssoc(['a', 'b', 'c'])); // false
var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false
var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true
var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true

Determine whether an array is associative (hash) or not

Pulled right out of the kohana framework.

public static function is_assoc(array $array)
{
// Keys of the array
$keys = array_keys($array);

// If the array keys of the keys match the keys, then the array must
// not be associative (e.g. the keys array looked like {0:0, 1:1...}).
return array_keys($keys) !== $keys;
}

PHP smooth way to check if array keys are consecutive

You may use array_replace combined with array_fill:

$keys = array_keys($a);
$result = array_replace(array_fill(0, max($keys), 999), $a);

Grabbing keys through array_keys first should make it work for any number of elements (provided the keys are numeric, obviously).

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

Adding looped data into associative array with readline

When you read values from the $dreams array with foreach ($dreams as $key => $value), you're expecting names as keys, but that's not how you inserted the values. You can use the name as the array key like this instead:

for ($i = 1; $i <= $many; $i++) {
echo "What is your name?" . PHP_EOL;

// set a name variable here
$name = readline() . PHP_EOL;
echo "What is your dream?" . PHP_EOL;

// then use that variable as the array key here
$dreams[$name] = readline() . PHP_EOL;
}

How to check if PHP array is associative or sequential?

You have asked two questions that are not quite equivalent:

  • Firstly, how to determine whether an array has only numeric keys
  • Secondly, how to determine whether an array has sequential numeric keys, starting from 0

Consider which of these behaviours you actually need. (It may be that either will do for your purposes.)

The first question (simply checking that all keys are numeric) is answered well by Captain kurO.

For the second question (checking whether the array is zero-indexed and sequential), you can use the following function:

function isAssoc(array $arr)
{
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}

var_dump(isAssoc(['a', 'b', 'c'])); // false
var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false
var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true
var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true

How to get one by one elements in associative array with min value and group PHP

If you sort on price_per_l descending and then index the array on product_uname then you will get only the lowest, as later items will replace previous ones and the later items are the lowest price:

array_multisort(array_column($array, 'price_per_l'), SORT_DESC, $array);
$array = array_column($array, null, 'product_uname');

If needed you can re-index:

$array = array_values($array);


Related Topics



Leave a reply



Submit