Given array item $array["first level"]["second LVL"]
, how can I get the key string second LVL
itself, not the key-value-pair value?
I have an array item variable $array["address"]["city"]
that I am passing to a function like so:
<?php
printKey( $array["address"]["city"] );
function printKey( $array_item ) {
return "Output: " . keyValue(array_item);
}
?>
How can I get the key value string city
itself from the array item $array["address"]["city"]
?
I've seen array_search(), array_keys(), and key(), but none seem to do the trick without a for loop at the least.
The problem is, for example, sometimes my function is passed $array["address"]["name"]
and sometimes it passes $array["address"]["company"]
.
I need to be able to dynamically output Name:
or Name:
Example function:
$array["address"]["name"] = "Andre";
$array["address"]["company"] = "StackNot";
function printITEMkeyAndValue( $arrayITEM ) {
//It's not possible to do a for loop on just an item, right? It's just a string (?)
return $array_item_key . ": " . $array_item_value;
}
echo printITEMkeyAndValue( $array["address"]["name"] );
echo printITEMkeyAndValue( $array["address"]["company"] );
Desired output:
Name: Andre
Company: StackNot
I understand what you want, but see when you call printKey function with a 2 value, it is not possible to find a key, because array does not exist in the function, the solution is to send the array and your item into the function and find it by search and then get the key.
<?php
$array["address"]["city"] = 2;
function printKey( $array, $item ) {
foreach($array["address"] as $key =>$value){
if ($value == $item){
return "Output: " . $key;
}
}
}
echo printKey($array, $array["address"]["city"]);
See this example
You will have to loop through the entire array to find match with value and get the key for that value using key($array) method
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
In case of 2D array you should pass level 1 element of 2D array.
See if you can do something like this
$arr1d= $array["address"];
while ($val_name = current($array)) {
if ($val_name == '$array_item') {
echo key($array).'<br />';
}
next($array);
}
I think you already passed in second level array key in function
echo printITEMkeyAndValue( $array["address"]["name"] );
so try this if this helpful for you.
function printITEMkeyAndValue( $address, $field_name ){
if( array_key_exists( $field_name, $address ) )
return ucfirst($field_name) .': '.$address[$field_name];
}
$address = array( 'address' => array(
'name' => 'My Name',
'city' => 'My city',
'state' => 'My state',
));
echo printITEMkeyAndValue( $address['address'], 'name' );
echo printITEMkeyAndValue( $address['address'], 'city' );
echo printITEMkeyAndValue( $address['address'], 'state' );
Simply no loop need.