0

假设我有一个像下面这样的数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Group1
            [description] => This is the group1.
        )

    [1] => Array
        (
            [id] => 2
            [title] => Group2
            [description] => This is group2.
        )

)

假设标题被称为“Group2”。如果它不知道它的、key、id 等只有标题,我如何能够确定使用 PHP 的等效描述(即“这是 group2”)?

谢谢你的帮助。

4

3 回答 3

3

试试这个 :

$title = "Group2";

foreach($your_array as $val){
   if($val['title'] == $title){
      echo $val['description'];
      break; //cut back on unnecessary looping
   }
}
于 2013-02-22T08:52:08.923 回答
0

您必须遍历主数组并扫描它以获取该标题。

假设您的主数组被称为$groups

$title = 'Group2';
foreach($groups as $key => $group){
  if ($group['title'] == $title){
    $groupDescription = $group['description'];
    // if you need to reference this group again, save it's key.
    $groupKey = $key;
  }
}

您可以在找到您要查找的组后插入一个break命令来终止循环,这样它就不会在您找到您要查找的组后继续扫描阵列。

于 2013-02-22T08:53:37.077 回答
0

像这样试试

foreach($myarray as $val){
  if($val['title'] == "Group2"){
     echo 'This is description '.$val['description'];
   }
}
于 2013-02-22T08:54:07.143 回答