0

I have an array and a number, assigned to the variable $cat.

$categories_array = array(
    "Belts" => array(131),
    "Headwear" => array(171,172,185,186),
    "ScarvesAndGloves" => array(166,173,184)
);

If $cat is 171, I want $category to be Headwear.

I've looked at array_search but it doesn't seem to work for multidimensional arrays. All solutions I've found around the site seem to rely on a key for each of the entries in the sub-array. I just want to assign the name of the key to the variable if it appears in the array. There will be no duplicates.

Can it be achieved in this format or should I be re-structuring this array? Is there a better way of achieving this result?

  • What is $cat in your code? And what is $category? Are you saying that you want to search for 171 in the array, and return the key for that sub-array? – random_user_name Feb 11 '14 at 15:03
  • possible duplicate of [PHP multi dimensional array search](http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search) – random_user_name Feb 11 '14 at 15:04
  • `$cat` is a category id returned by a foreach loop from a mysql query. I want `$category` to be the name of the key from the array where the number `$cat` happens to be. –  Feb 11 '14 at 15:05
  • @cale_b Thanks for the link, I've looked at that question already and is an example of one of the ones that appear to rely on a sub-array key. –  Feb 11 '14 at 15:06

4 Answers4

1

My answer is nearly similar as above just added a break, so if you already get the result then no need to continue the loop. Will be efficient if the array is too large.

$cat              = 171;
$categories_array = array (
    "Belts"            => array (131),
    "Headwear"         => array (171, 172, 185, 186),
    "ScarvesAndGloves" => array (166, 173, 184)
);

echo getaName($categories_array, $cat);


function getaName($array, $searchKey) {
    $name = '';
    foreach($array as $key => $val) {
        if(in_array($searchKey, $val)){
            $name = $key;
            break;
        }
    }
    return $name;
}
Tahmina Khatoon
  • 1,081
  • 3
  • 11
  • 29
0

I don't see a problem with your array.

This is how I would search through the array:

$cat = 171; 
$category = '';

$categories_array = array(
  "Belts" => array(131),
  "Headwear" => array(171,172,185,186),
  "ScarvesAndGloves" => array(166,173,184)
);

foreach($categories_array as $category_name => $categories) {
  if(in_array($cat, $categories)) {
    $category = $category_name;
  }
}

echo "You have chosen: $category"; 
albertski
  • 2,428
  • 1
  • 25
  • 44
  • Great, thanks. I really wasn't that far away myself in the end! I'll give you an accept as soon as I can. –  Feb 11 '14 at 15:10
0

Following code would be one of many solutions for your problem:

$cat = 171;
$category = '';

foreach ($categories_array as $k => $v) {
    if (false !== array_search($cat, $v)) {
        $category = $k;
    }
}

echo $category;
agassner
  • 689
  • 8
  • 25
  • Thanks, this works just as well but came in a touch later. Thanks for your help. –  Feb 11 '14 at 15:11
0

try:

<?php
    function getvalue($cat){
       foreach($categories_array as $categorie =>$list){
          if(in_array($cat,$list))
             return $categorie;
        }
       return false
     }
?>