Get key and value in php
I want to run a for loop through an array and create anchor elements for each element in the array, where the key is the text part and the value is the URL. Show How can I do this please? Thank you. asked Apr 21, 2011 at 14:36
2 This should do it
Edit: As per Capsule's comment - changed to single quotes. answered Apr 21, 2011 at 14:37
Marek KarbarzMarek Karbarz 28.6k6 gold badges51 silver badges73 bronze badges 5 For some specific purposes you may want to know the current key of your array without going on a loop. In this case you could do the following:
The above example will show the Key and the Value of the first record of your Array. The following functions are not very well known but can be pretty useful in very specific cases:
answered May 22, 2012 at 17:23
DrupalFeverDrupalFever 3093 silver badges2 bronze badges 1 Like this:
answered Apr 21, 2011 at 14:38
In a template context, it would be:
You shouldn't write your HTML code inside your PHP code, hence avoid echoing a bunch of HTML. This is not filtering anything, I hope your array is clean ;-) answered Apr 21, 2011 at 14:40
CapsuleCapsule 6,0801 gold badge19 silver badges27 bronze badges 0 (PHP 4, PHP 5, PHP 7, PHP 8) array_keys — Return all the keys or a subset of the keys of an array Descriptionarray_keys(array array_keys(array If a Parametersarray An array containing keys to return. search_value If specified, then only keys containing this value are returned. strict Determines if strict comparison (===) should be used during the search. Return Values Returns an array of all the keys in ExamplesExample #1 array_keys() example
The above example will output: Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size ) See Also
pat dot leblanc at gmail dot com ¶ 11 years ago
= array('329462291595' =>null, 'ZZ291595' => null); Sven (bitcetera.com) ¶ 16 years ago
phpnet at holodyn dot com ¶ 8 years ago
Ian (maxianos at hotmail dot com) ¶ 8 years ago
$arrayKeys; zammit dot andrew at gmail dot com ¶ 8 years ago
= array(); Robert C. ¶ 6 years ago
$array as $key => $val ) { Paul Hirsch ¶ 7 years ago
Md. Abutaleb ¶ 2 years ago
0] => first_index el dot quick at gmail dot com ¶ 11 years ago
jochem ¶ 16 years ago
rodrigo at NOSPAM dot dhweb dot com dot br ¶ 19 years ago
" ferrerna at gmail dot com ¶ 13 years ago
= db_query('SELECT name, value FROM properties');$result = db_returnAll($query);/* This will return an array like so: izzecold at live dot de ¶ 10 years ago
function mKeyChecker($arr,$keys=array()) { Hayley Watson ¶ 14 years ago
vesely at tana dot it ¶ 16 years ago
ru dot dy at gmx dot net ¶ 17 years ago
sip at email dot ee ¶ 19 years ago
creator at mindcreations dot com ¶ 15 years ago
$ar as $k => $v) {
craig1231 at hotmail dot com ¶ 10 years ago
$keys as $k) webmaster [at] baz-x [dot] at ¶ 17 years ago
$array; glennh at webadept dot net ¶ 19 years ago
neil at 11 out of 10 ¶ 14 years ago
qeremy [atta] gmail [dotta] com ¶ 10 years ago
array_keys_prefix_multi($arr, $pref = "") { anthony at berglas dot org ¶ 10 years ago
Na'ven Enigma ¶ 10 years ago
nodarinodo at mail dot ru ¶ 13 years ago
$i =0; $i < count(array_keys($es, $ess)); $i++){ How do you find the key and value of an array?array_keys() returns the keys, numeric and string, from the array . If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
How get the key of an object in PHP?You can cast the object to an array like this: $myarray = (array)$myobject; And then, for an array that has only a single value, this should fetch the key for that value. $value = key($myarray);
What is $Key in PHP?The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .
What is key and value in array?What are a key and value in an array? Keys are indexes and values are elements of an associative array. Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.
|