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

Replace substring in keys of an array in PHP

Assume that you have a PHP array like this, coming from a mysqli query: $array = ( 'user_id' => 1 'user_name' => 'User', 'user_email' => 'mail@user.com' ); Now I would like to create an instance of my user class. The constructor takes…
andreas
  • 7,844
  • 9
  • 51
  • 72
3
votes
3 answers

Get a sub array from multidimensional array using php

I want to get a value form a multidimensional array using PHP. I pass the key to the function and if the key contains the value (i.e. without any array value), it will return that. But if that key contains an array value, it will return the whole…
3
votes
2 answers

Missing values when replacing keys in multidimensional array

I'm trying to replace the keys in my output array. There are missing values. The missing values are ‘Precio’, ‘Monto’, ‘Operaciones’ and ‘Hora’ -- the only values that don't change. I tried changing the new key name but it didn't worked. This is…
3
votes
1 answer

dot(.) not supported as array keys character after form submit

I have an array like: $arr = array("a.b" => "a.b", "b.c" => "b.c"); When i print this it simply displays Array ( [a.b] => a.b [b.c] => b.c ) But when i use a loop to use the key and value in checkbox name and values like:
user6364857
3
votes
3 answers

To get array keys if value is not zero

I have array like and used array_keys to get keys: $arr = array( 1 => 1, 2 => 3, 3 => 2, 5 => 0, 6 => 0 ); $new_arr = array_keys($arr); Now, I want to get array_keys if value is not zero. How can i do this? Please…
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
3
votes
4 answers

How to get the keys in a PHP Array by position?

I have an array where I store key-value pair only when the value is not null. I'd like to know how to retrieve keys in the array?
Richard77
  • 20,343
  • 46
  • 150
  • 252
3
votes
3 answers

Read key of array two dimension

I try to read some keys of an array like array(3) { ["poste5:"]=> array(3) { [0]=> string(7) "APPLE:" [1]=> string(5) "demo1" [2]=> string(5) "demo2" } ["services:"]=> array(4) { [0]=> string(9) "orange" [1]=> string(5) "demo3" [2]=>…
mpgn
  • 7,121
  • 9
  • 67
  • 100
3
votes
1 answer

How to wrap around in PHP array when index falls off the end?

I want to be able to retrieve the value of an array by using the numeric key. The catch is that if the key is beyond the array length, I need it to loop through the array again. $my_array =…
amaster
  • 1,915
  • 5
  • 25
  • 51
3
votes
3 answers

php array difference against keys of an array and an array of keys?

Suppose we have two arrays: $a=array('1'=>'Apple','2'=>'Microsoft', '3'=>'Microapple','4'=>'Applesoft','5'=>'Softapple'); $b=array(1,3); Where $b array represents the keys of array $a to be differentiated against. And we expect to receive…
Anonymous
  • 4,692
  • 8
  • 61
  • 91
2
votes
1 answer

PHP multidimensional array: increase key if last of sub-array

For a calendar, I read start date and end date from database, and a dateRange function creates an array with one day for each key, like…
Chris
  • 3,756
  • 7
  • 35
  • 54
2
votes
3 answers

Make array keys insensitive in php

Am using DB2 with my PHP. DB2 returns all column names in UPPERCASE. If I use db2_fetch_assoc, then the result would be like this Array { [USERID] => 4 } But in my php file, the array values assigned like this and its in camelstyle $this->userId =…
Coder
  • 133
  • 1
  • 5
  • 11
2
votes
6 answers

PHP: Multidimensional array, multidimensional keys?

$products = array( 'paper' => "Paper Section" => array ( 'copier' => "Copier and Multipurpose", 'inkjet' => "Inkjet Printer", ), 'pens' => "Pen Section" => array ( 'ball' => "Ballpoint Pens", 'hilite' => "Highlighters" …
Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
2
votes
2 answers

Using empty string as key in associative array

I need to select and group some items according to some values and it's easy using an associative multidimensional array: $Groups = array( "Value1" = array("Item1", "Item3"), "Value2" = array("Item2", "Item4") ); But some items hasn't…
genespos
  • 3,211
  • 6
  • 38
  • 70
2
votes
1 answer

How does PHP handle integer indexed non-consecutive keyed arrays?

I'm curious as to how efficient it is in PHP to store an array with integer indices that are non-consecutive. If I had an array $b = array(); $b[1] = "Hello"; $b[18] = "World"; $b[999] = "Test"; Are those keys stored, and then hashed to a new…
Nikhil
  • 485
  • 1
  • 4
  • 16
1
2
3
10 11