从多维索引中获取关键

时间:2013-08-27 17:17:30

标签: php arrays

我有以下数组结构:

$CATEGORIES = array(
    array(
        category => "music", 
        color => "#FF8800"
        ),
     array(
        category => "film & TV", 
        color => "#59AF56"
        ),
    array(
        category => "culture", 
        color => "#fc2525"
        ),
    array(
        category => "science/tech", 
        color => "#5da9d4"
        )
    );

基本上,将查找类别,我需要检索十六进制值。我找不到任何能为多维数组做这件事的事情(在尝试使用array_search()之后)。任何指针都将非常感激。

1 个答案:

答案 0 :(得分:1)

试试这个:

$category = 'culture';
foreach($CATEGORIES as $key){
   if($key['category'] == $category)
      $hex = $key['color'];//here is your color
}

DEMO