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
0
votes
4 answers

Array Keys do not add up but makes keys go from 0 to 9 3 times

When I was combining 4 arrays into 1 array it made the keys [0] to [9] 4 times instead of making it go from [0] to [34]. The file I'm working with is a .txt file with the follwing content (just 4 lines with random text): gghondxs vishfgfh…
JohanStaart
  • 75
  • 11
0
votes
1 answer

php change array key with sub array value

[items] => stdClass Object ( [1] => stdClass Object ( [id] => 146300 [name]=>aa ) [2] => stdClass Object ( …
Ebru
  • 1
0
votes
2 answers

How to sum the 2nd level values of a 2-dim array based on the 2nd level keys?

I have a multi-dimensional array like this: array( 0 => array( 11 => 18.000, 14 => 25.100, 15 => 5.000, 16 => 8.000, 19 => 21.600' ), 1 => array( 11 => 9.100, 12 =>…
Wasswa Samuel
  • 2,139
  • 3
  • 30
  • 54
0
votes
0 answers

Preg_match with variable matching integer key

I have an array $op1 = array('45'=>123, '657'=>65, '6578'=>234, '65782'=>0, '3'=>25, '756'=>156, '3445'=>178, '20924'=>92); And then a variable taken from a form that I want to see if any of the array keys starts with it. Variable:…
user3329074
  • 127
  • 9
0
votes
0 answers

understanding array key output

Just something i cant understand why it does this or if i am getting something wrong. I am trying to check if an array key == a sertain string in a foreach loop. this is what i have (only really simple, but... bugging me...) foreach($option_arr as…
Ford
  • 537
  • 6
  • 20
0
votes
1 answer

PHP change array keys with key values from another array

I have 2 arrays. What I am trying to do is replace the $choices array keys with respective $data keys This is what I expect to see. I hope this makes sense $choices = array( 'fruits' => array( 'Red Apple' => '', 'Yellow Banana'…
AdRock
  • 2,959
  • 10
  • 66
  • 106
0
votes
2 answers

Array push - change name on key

I can not get this to work. How do I change key [0] to [id]. In the below array ex. I want the [0] => 2 to look like [id] => 2 Array ex.: Array ( [pickup] => 2014/11/10 15:15 [tire_front] => tire_front [service] => 4 [message] =>…
Nomis
  • 437
  • 1
  • 4
  • 13
0
votes
2 answers

compare string to sub key value in php array partial match

I need to figure out a way to partial match a string to a sub key in my PHP array. example: string = howdy-doody show as you can see there is a - dash and a space between the words. In my PHP array the sub key might be howdy doody show with no…
Mike
  • 607
  • 8
  • 30
0
votes
2 answers

Lua Changing Table Keys

Anyone tell me why this doesn't work? GET_TABLE {1=ID} key = string.format("%q", GET_TABLE[1]) RETURN_TABLE[key] = "ss" print(RETURN_TABLE[ID]) print(GET_TABLE[1]) First print result: nil. Second print result: ID I want the first print result to…
0
votes
2 answers

get key with specific value from multidimention array

i have an array like below: $array=["satu"=>"mangga","dua"=>array("melon","apel")]; how can i get "dua" with $buah="melon" I tried with this method, when $buah = "mangga" , the output is "satu" but when $buah = "melon" i got nothing, how can i get…
Sandhi
  • 89
  • 2
  • 11
0
votes
1 answer

How to shift array elements from associative array by one in following scenario in PHP?

I've following associative array titled $_POST as follows: Array ( [op] => add [product_id] => 12 [pack] => Array ( [1] => 1 ) [applicable_states] => Array ( [0] => multiselect-all …
PHPLover
  • 1
  • 51
  • 158
  • 311
0
votes
2 answers

reset key after count reached

So I am trying to grab just 4 keys at a time out of an array and then reset the key count back to 0 after the 4th one is reached (actually key #3, because the array starts with 0). Here's an example: 0 - USA Mix #1 1 - 24mg 2 - 252 3 - value 4 - USA…
MrTechie
  • 1,797
  • 4
  • 20
  • 36
0
votes
2 answers

PHP array keys changes to negative value between certain number range

By coincidence I ran into a very bizarre behaviour regarding PHP arrays and its keys. Consider this creation of an PHP array. $arr[2250572483]=1; //dump the array var_dump($arr); //Result: array(1) { [-2044394813]=> int(1) } Somehow the array key…
tuberider
  • 124
  • 9
0
votes
2 answers

Unit Testing: Give an invalid array key that doesn't break PHP. Possible?

I'm working on a unit test for a small class. I'm using this class to get down and dirty with PHPUnit so I can start properly test bigger pieces of code that I write in the future. Consider the following code I am trying to test: /** * Registers a…
Crackertastic
  • 4,958
  • 2
  • 30
  • 37
0
votes
3 answers

Getting no array key

Hey I'm trying to get the key of my array in a foreach. But got this error Warning: array_keys() expects parameter 1 to be array, string given on line 10 Here is my array: $status_de = array ( '1' => 'Anfrage', '2' => 'Angebot', '3' =>…
user1551496
  • 169
  • 2
  • 12