0

Given array item $array["first level"]["second LVL"], how can I get the key string second LVL itself, not the key-value-pair value?

More Detailed Example

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.

EDIT / Clarification:

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
3
  • Why do you not want to use loops? Commented Aug 6, 2016 at 5:31
  • Because I'm using this once, for one item, for a simple logging function. And if I was to use a for loop, how would I loop through the array if only the array item is passed to the function like $array["string"]["string2"]? And further, how could I search for string2 if I can't dynamically reference it? Commented Aug 6, 2016 at 5:33
  • You'll have to pass array to compare the value with.Otherwise you have something to compare but with whom will you compare that? You are passing the copy of value of an element in an array not exactly array. So looping through array is required. Commented Aug 6, 2016 at 5:55

3 Answers 3

2

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"]);
Sign up to request clarification or add additional context in comments.

1 Comment

I was afraid of this :/ This is a bit bizarre -- the function is already passed the string and I can't output it.... I don't even know.
1

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);
    }

2 Comments

Problem is, this won't d the trick at all. My question needs to be cleared up. Please see my edit in a minute.
Problem is, how do I dynamically reference apple when the item passed to function is $array["fruits"]["apple"]?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.