1

How can I obtain a key in a array just by knowing it's value? For example, here is an array:

$array = Array("Item1" => array("Number" => "One", "Letter" => "A"));

Just by knowing "One" or "A", how can I get the main key name, Item1?

I've looked into array_key_value and in_array but I do not think that those functions are helpful for my kind of array.

1
  • WIth a foreach Commented Jun 22, 2017 at 21:03

5 Answers 5

1

Since it is a 2d array, you will want to search the inner array for the value so you would have to make your own function to do this. Something like this:

function findInArray($array, $lookup){
    //loop over the outer array getting each key and value.
    foreach($array as $key=>$value){
        //if we found our lookup value in the inner array
        if(in_array($lookup, $value)){
            //return the original key
            return $key;
        }
    }
    //else, return null because not found
    return null;
}

$array = Array("Item1" => array("Number" => "One", "Letter" => "A"));

var_dump(findInArray($array, 'One')); //outputs string(5) "Item1"
var_dump(findInArray($array, 'Two')); //outputs null

Demo: https://3v4l.org/oRjHK

Sign up to request clarification or add additional context in comments.

Comments

0

This function may help you

function key_of_value($array, $value){
    foreach($array as $key=>$val){
        if(in_array($value, $val)){
            return $key;
        }
    }
    return null;
}

echo key_of_value(['Item1'=>['One','Two','Three','Hello',2,6]],'A');

Comments

0

There is no way around iterating through your data. This might be a little more elegant than two foreach loops:

<?php
$match = null;
$needle = 'Two';
$haystack = [
    'Item1' => [
        'Number' => 'One',
        'Letter' => 'A'
    ],
    'Item2' => [
        'Number' => 'Two',
        'Letter' => 'B'
    ],
    'Item3' => [
        'Number' => 'Three',
        'Letter' => 'C'
    ],
];

array_walk($haystack, function($entry, $key) use ($needle, &$match) {
    if(in_array($needle, $entry)) {
        $match = $key;
    }
});
var_dump($match);

The output obviously is:

string(5) "Item2"

Comments

0

You can use array_walk_recursive to iterate on array values recursive. I write a function that return main key of searched value in nested arrays.

<?php

$array = array("Item1" => array("Number" => "One", "Letter" => "A", 'other' => array('Number' => "Two")));

echo find_main_key($array, 'One'); //Output: "Item1"
echo find_main_key($array, 'A'); //Output: "Item1"
echo find_main_key($array, 'Two'); //Output: "Item1"
var_dump(find_main_key($array, 'nothing')); // NULL

function find_main_key($array, $value) {
    $finded_key = NULL;
    foreach($array as $this_main_key => $array_item) {
        if(!$finded_key) {
            array_walk_recursive($array_item, function($inner_item, $inner_key) use ($value, $this_main_key, &$finded_key){
                if($inner_item === $value) {
                    $finded_key = $this_main_key;
                    return;
                }
            });
        }
    }
    return $finded_key;
}

Comments

0

This is how I would do it:

foreach($array as $key => $value) { if(in_array('One', $value)) echo $key; }

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.