How Does PHP Index Associative Arrays

Accessing an associative array by integer index in PHP

Use array_keys.

$keys = array_keys($my_arr);
$my_arr[$keys[1]] = "not so much bling";

How does PHP index associative arrays?

Within your user defined array you are assigning the keys manually your array means as

array(1 => 'One',3, 2 => 'Two');//[1] => One [2] => 3 [2] => Two

Here we have two identical index and as per DOCS its mentioned that the last overwrite the first

Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1.

Note that when two identical index are defined, the last overwrite the first.

In order to filter this case you can simply make some changes as

array(1 => 'One',2 =>'Two',3) // array ([1] => One [2] => Two [3] => 3)
array(3,1 => 'One',2 =>'Two') //array ([0] => 3 [1] => One [2] => Two)
array(1 => 'One',2 => 3 ,3 =>'Two')// array([1] => One [2] => 3 [3] => Two)

DOCS CHECK PARAMETERS

php - get numeric index of associative array

echo array_search("car",array_keys($a));

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 combine associative array by indexes

You can achieve this with a nested loop. Loop through the sub-arrays first. On each iteration, loop through the elements in the sub-array, and add them into the result array. The important part here is what we use as the index for the $result array. $index will be the position of the array element in the sub-array. For example, Category would have an index of 0, so it would be pushed to $result[0][].

foreach ($array as $sub) {
foreach ($sub as $index => $val) {
$result[$index][] = $val;
}
}

print_r($result);

Demo

php: how to get associative array key from numeric index?

You don't. Your array doesn't have a key [1]. You could:

  • Make a new array, which contains the keys:

    $newArray = array_keys($array);
    echo $newArray[0];

    But the value "one" is at $newArray[0], not [1].

    A shortcut would be:

    echo current(array_keys($array));
  • Get the first key of the array:

     reset($array);
    echo key($array);
  • Get the key corresponding to the value "value":

    echo array_search('value', $array);

This all depends on what it is exactly you want to do. The fact is, [1] doesn't correspond to "one" any which way you turn it.

php - Why does key of first element of an associative array cannot be zero?

The function you're referring to has flaws and it is not authoritative about the fact whether or not an array in PHP is associative or not.

In-fact, in PHP it is not strictly defined what the term associative array actually means.

However it is important to understand that

  1. PHP is loosely typed
  2. PHP differs in array keys between integer (bool, NULL, float, integer, numeric string represented as integer) and string (nun-numeric strings) for keys.

Most likely an associative array in PHP is one where inside the array (that is after creating it, not while it seems when writing the array definition) that all keys in that array are strings.

But keep in mind that no set-in-stone definition exists. PHP arrays are just a mixture of array and hash-map where you need to know both without having both properly differentiated and the need to keep in mind the difference between numeric and non-numeric keys and how the loosely type-system of PHP converts for the array key needs.

First page to go as usual:

  • http://php.net/language.types.array

For those who really can only understand it with code, here the pony goes:

function is_array_assoc(array $array) {
return is_array($array);
}

If you can ensure that you're not using an error-handler that catches catchable errors, then the following code is correct, too:

function is_array_assoc(array $array) {
return TRUE;
}

The later example makes it perhaps a bit more clear that all arrays in PHP are associative.



Related Topics



Leave a reply



Submit