Questions tagged [array-key]

array-key refers to key portion of array holding key and value pair. In PHP array_keys() returns the keys, numeric and string, from the input array.

array-key() refers to key portion of array holding key and value pair. In PHP array_keys() returns the keys, numeric and string, from the input array.

array array_keys ( array $array [, mixed $search_value [, bool $strict = FALSE ]] )

array_keys() — Return all the keys or a subset of the keys of an array

Example:

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

Result:

Array
(
    [0] => 0
    [1] => color
)

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)

Reference

155 questions
217
votes
9 answers

PHP - Get key name of array value

I have an array as the following: function example() { /* some stuff here that pushes items with dynamically created key strings into an array */ return array( // now lets pretend it returns the created array …
headacheCoder
  • 4,503
  • 8
  • 30
  • 33
47
votes
4 answers

How to get the position of a key within an array

Ok, so I need to grab the position of 'blah' within this array (position will not always be the same). For example: $array = ( 'a' => $some_content, 'b' => $more_content, 'c' => array($content), 'blah' => array($stuff), 'd' =>…
SoLoGHoST
  • 2,673
  • 7
  • 30
  • 51
23
votes
6 answers

Is it okay to use array[key] in PHP?

Is it okay to use array without single or double quotion like $array[key]? I thought it is bad because PHP look for constant first if I don't use single or double quotation. One of my colleagues told me that it does not matter. What do you guys…
Moon
  • 22,195
  • 68
  • 188
  • 269
21
votes
11 answers

replace array keys with given respective keys

I have an array like below $old = array( 'a' => 'blah', 'b' => 'key', 'c' => 'amazing', 'd' => array( 0 => 'want to replace', 1 => 'yes I want to' ) ); I have another…
Maulik Vora
  • 2,544
  • 5
  • 28
  • 48
10
votes
8 answers

How to get a subset of $_POST array with keys starting with a prefix

Let's say my $_POST variable looks like: 65 [action] => editpost [originalaction] => editpost [post_author] => 154 [empl_bd_dd] => 6 [empl_bd_mm] => 5 [empl_bd_yy] => 1987 [empl_gen] => 1 …
Ana Ban
  • 1,395
  • 4
  • 21
  • 29
9
votes
4 answers

array_key_exists is not working

array_key_exists is not working for large multidimensional array. For ex $arr = array( '1' => 10, '2' => array( '21' => 21, '22' => 22, '23' => array( 'test' => 100, '231' => 231 ), …
ArK
  • 20,698
  • 67
  • 109
  • 136
8
votes
3 answers

How to merge two arrays by taking over only values from the second array that has the same keys as the first one?

I'd like to merge two arrays with each other: $filtered = array(1 => 'a', 3 => 'c'); $changed = array(2 => 'b*', 3 => 'c*'); Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key…
hakre
  • 193,403
  • 52
  • 435
  • 836
6
votes
2 answers

PHP array keys values

I have an array like this $data = array( "some" => "163", "rand" => "630", "om" => "43", "words" => "924", "as" => "4", "keys" => "54" ); How can I get each set's key associated with their values like this: foreach…
Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
5
votes
3 answers

Why does the typeof a numerical array index in a "for..in" loop considered a string?

I noticed that in Javascript a variable used as the index in a for..in loop will be always a string even if I define it the following way: var s_array = new Array(); s_array[0] = 'foo'; s_array[1] = 'bar'; for(i in s_array){ alert(typeof(i)); //…
talsibony
  • 8,448
  • 6
  • 47
  • 46
5
votes
6 answers

Populate a javascript array shorthand for keys like php

In Javascript, know I can set an array so that the key is a autonumbered (starting at 0) assigned array: var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); // Key for Sunday is '0'…
Sablefoste
  • 4,032
  • 3
  • 37
  • 58
5
votes
5 answers

Filter 2D associative array using keys from multiple levels of another 2D associative array

I have two 2-dimensional arrays and want to filter the first array's data using the second array so that the only elements retained are where the keys in the first and second levels match. $array1 = [ 'a1' => ['a_name' => 'aaaaa', 'a_value' =>…
kcssm
  • 1,727
  • 3
  • 16
  • 19
4
votes
2 answers

How to get keyname of array in the view of twig

Im coding in twig to visualize values that I get from php and I want that the keyname of array appear above the values. {% if user.address %} {% for address in user.address %} {% for parts in address %} …
Batichico
  • 676
  • 1
  • 6
  • 15
4
votes
2 answers

How to search for a substring in array keys?

I have an array like below $array =array( "123,456,789"=> "1,1,1", "333"=>"1", "777"=>"1" ) Now if I am searching 456 then need to return me array key(123,456,789) and its value (1,1,1) I also tried like below to make it working but i didn't get…
Ankit Doshi
  • 1,164
  • 3
  • 21
  • 43
4
votes
1 answer

How to access $array[@key] value

I am working with expedia API's and its working well but I don't know how to access this special type of array key. Response is given below $response = stdClass Object ( [@size] => 1 [@activePropertyCount] => 144 [city] => 1 …
vikujangid
  • 728
  • 3
  • 8
  • 19
4
votes
5 answers

Array key in php

I'm trying to understand this code: 1, 2, 3, "first_name"=>"mike", 4, 5, 10=>-2.3); print_r(array_keys($list)); ?> Output: Array ( [0] => -10 [1] => 0 [2] => 1 [3] => first_name [4] => 2 [5] => 3 [6] => 10 ) I'm…
user3562135
  • 159
  • 2
  • 9
1
2 3
10 11