❮ PHP Array Reference
Example
Return an array containing the keys:
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
?>
Try it Yourself »
Definition and Usage
The array_keys() function returns an array containing the keys.
Syntax
array_keys(array, value, strict)
Parameter Values
array | Required. Specifies an array |
value | Optional. You can specify a value, then only the keys with this value are returned |
strict | Optional. Used with the value parameter. Possible values:
|
Technical Details
Returns an array containing the keys |
4+ |
The strict parameter was added in PHP 5.0 |
More Examples
Example
Using the value parameter:
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a,"Highlander"));
?>
Try it Yourself »
Example
Using the strict parameter, false:
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",false));
?>
Try it Yourself »
Example
Using the strict parameter, true:
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>
Try it Yourself »
❮ PHP Array Reference
PHP: Return all the keys of an array
The array_keys() function is used to get all the keys or a subset of the keys of an array.
Version:
(PHP 4 and above)
Syntax:
array_keys(input_array, search_key_value, strict)Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
Parameters:
input_array | Specified array. | Required | Array |
search_key_value | Value to be checked. | Optional | Array |
strict | As of PHP 5, this parameter determines if strict comparison (===) should be used during the search. | Optional | Boolean |
Return value:
An array of all the keys of input_arrray.
Value Type: Array
Example - 1:
<?php $array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400); print_r(array_keys($array1)); ?>Output:
Array ( [0] => Orange [1] => Apple [2] => Banana [3] => Cherry )Pictorial Presentation:
View the example in the browser
Example - 2 :
Output :
Array ( [0] => 1 [1] => 3 )
View the example in the browser
Practice here online :
See also
PHP Function Reference
Previous:array_key_exists
Next: array_map
PHP: Tips of the Day
PHP: Javascript Equivalent to PHP Explode()
This is a direct conversion from your PHP code:
//Loading the variable var mystr = '0000000020C90037:TEMP:data'; //Splitting it with : as the separator var myarr = mystr.split(":"); //Then read the values from the array where 0 is the first //Since we skipped the first element in the array, we start at 1 var myvar = myarr[1] + ":" + myarr[2]; // Show the resulting value console.log(myvar); // 'TEMP:data'Ref : //bit.ly/3hSDp4q
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
array_fill_keys — Fill an array with values, specifying keys
Description
array_fill_keys(array $keys, mixed $value): array
Parameters
keysArray of values that will be used as keys. Illegal values for key will be converted to string.
valueValue to use for filling
Return Values
Returns the filled array
Examples
Example #1 array_fill_keys() example
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
The above example will output:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
See Also
- array_fill() - Fill an array with values
- array_combine() - Creates an array by using one array for keys and another for its values
sergli at nigma dot ru ¶
10 years ago
<?php
$a = array("1");
var_dump(array_fill_keys($a, "test"));
?>
array(1) {
[1]=>
string(4) "test"
}
now string key "1" become an integer value 1, be careful.
atul dot kr_singh at hotmail dot com ¶
9 years ago
If an associative array is used as the second parameter of array_fill_keys, then the associative array will be appended in all the values of the first array.
e.g.
<?php
$array1 = array(
"a" => "first",
"b" => "second",
"c" => "something",
"red"
);
$array2 = array(
"a" => "first",
"b" => "something",
"letsc"
);
print_r(array_fill_keys($array1, $array2));
?>
The output will be
Array(
[first] => Array(
[a] => first,
[b] => something,
[0] => letsc
),
[second] => Array(
[a] => first,
[b] => something,
[0] => letsc
),
[something] => Array(
[a] => first,
[b] => something,
[0] => letsc
),
[red] => Array(
[a] => first,
[b] => something,
[0] => letsc
)
)
ray.paseur sometimes uses gmail ¶
5 months ago
Get an associative array of zeros for counting letter frequency
<?php
$ltrs = array_fill_keys( range('a', 'z'), 0 );
Scratchy ¶
14 years ago
RE: bananasims at hotmail dot com
I also needed a work around to not having a new version of PHP and wanting my own keys. bananasims code doesn't like having an array as the second parameter...
Here's a slightly modified version than can handle 2 arrays as inputs:
//we want these values to be keys
$arr1 = (0 => "abc", 1 => "def");
/we want these values to be values
$arr2 = (0 => 452, 1 => 128);
function array_fill_keys($keyArray, $valueArray) {
if(is_array($keyArray)) {
foreach($keyArray as $key => $value) {
$filledArray[$value] = $valueArray[$key];
}
}
return $filledArray;
}
array_fill_keys($arr1, $arr2);
returns:
abc => 452, def =>128
manavchugh988 at gmail dot com ¶
3 months ago
see array_fill_keys are basically used to make a new array from a pre-existing array in a form that the value of the pre-existing array will now be the key of the new Array .And there value will be same That we had given in the 2nd parameter . Example Below---->>>
<?php
//pre existing array
$a = array("a","b","c","d","e");//new array with a single same value $newArray = array_fill_keys($a, "Testing");//printing the array echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
output;
Array
(
[a] => Testing
[b] => Testing
[c] => Testing
[d] => Testing
[e] => Testing
)
bananasims at hotmail dot com ¶
15 years ago
Some of the versions do not have this function.
I try to write it myself.
You may refer to my script below
function array_fill_keys($array, $values) {
if(is_array($array)) {
foreach($array as $key => $value) {
$arraydisplay[$array[$key]] = $values;
}
}
return $arraydisplay;
}
matrebatre ¶
14 years ago
This function does the same as:
<?php
$array = array_combine($keys,array_fill(0,count($keys),$value));
?>
phydeaux ¶
14 years ago
Scratchy's version still doesn't work like the definition describes. Here's one that can take a mixed variable as the second parameter, defaulting to an empty string if it's not specified. Don't know if this is exactly how the function works in later versions but it's at least a lot closer.
function array_fill_keys($target, $value = '') {
if(is_array($target)) {
foreach($target as $key => $val) {
$filledArray[$val] = is_array($value) ? $value[$key] : $value;
}
}
return $filledArray;
}
This works for either strings or numerics, so if we have
$arr1 = array(0 => 'abc', 1 => 'def');
$arr2 = array(0 => 452, 1 => 128);
$arr3 = array(0 => 'foo', 1 => 'bar');
then
array_fill_keys($arr1,$arr2)
returns: [abc] => 452, [def] => 128
array_fill_keys($arr1,0)
returns: [abc] => 0, [def] => 0
array_fill_keys($arr2,$arr3)
returns: [452] => foo, [128] => bar
array_fill_keys($arr3,'BLAH')
returns: [foo] => BLAH, [bar] => BLAH
and array_fill_keys($arr1)
returns: [abc] =>, [def] =>
ntd at entidi dot it ¶
6 years ago
To remove arbitrary keys from an associative array:
<?phpfunction nuke_keys($keys, $array) {
return array_diff_key($array, array_fill_keys($keys, 0));
}$array = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$keys = array('red', 'purple');print_r(nuke_keys($keys, $array));
?>
The above snippet will return:
Array
(
[blue] => 1
[green] => 3
)
taylanaktepe at yahoo dot com ¶
8 years ago
$keys = array(1, 2, 3);
// Fill it with value.
$keys = array_fill_keys($keys, 'banana');
print_r($keys);
// Fill it different value.
$apples = array_fill_keys(array_keys($keys), 'apple');
print_r($apples);
// Output:
Array (
[1] => banana
[2] => banana
[3] => banana
)
Array (
[1] => apple
[2] => apple
[3] => apple
)