最简单的问题:从数组中提取值

时间:2010-08-16 19:03:38

标签: php arrays foreach

所以这是一个例子:

Array ( 
[0] => Array ( [title] => Title_1 [checkout] => 1 [no_gateway] => 0 ) 
[1] => Array ( [title] => Title_2 [checkout] => 1 [no_gateway] => 1 )
[2] => Array ( [title] => Title_3 [checkout] => 0 [no_gateway] => 0 )
[3] => Array ( [title] => Title_4 [checkout] => 1 [no_gateway] => 1 )
[4] => Array ( [title] => Title_5 [checkout] => 0 [no_gateway] => 0 )
[5] => Array ( [title] => Title_6 [checkout] => 1 [no_gateway] => 0 )
)

我需要打印出[title]键下的所有值[checkout] => 1& [no_gateway] => 0

在我看来,它应该是

  • TITLE_1
  • Title_6

请帮助php-beginner :)谢谢!

5 个答案:

答案 0 :(得分:9)

foreach($array as $row) {
  if ($row['checkout'] && !$row['no_gateway']) {
    print $row['title'];
  }
}

答案 1 :(得分:4)

foreach ($items as $item) {
  if($item['checkout'] == 1 && $item['no_gateway'] == 0) {
      echo $item['title'];
  }
}

假设您的数组名为$ items

答案 2 :(得分:3)

print_r(
    array_map(function ($a) { return $a["title"]; },
        array_filter($original,
            function ($a) { return $a["checkout"] && !$a["no_gateway"]; }
        )
    )
);

答案 3 :(得分:2)

您使用答案标记了问题:foreach

// assuming $arr is the array containing the values from the example
foreach ($arr as $record) {
    if ($record['checkout'] && !$record['no_gateway']) {
        echo $record['title'], "\n";
    }
}

答案 4 :(得分:2)

foreach( $array as $value ) {
    if( $value["checkout"] == 1 && $value["no_gateway"] == 0 ) {
        print $value["title"].PHP_EOL;
    }
}