数组中的字符串比较

时间:2016-10-28 09:35:51

标签: php

在我的数组中,我想比较"JUDGE OF APPEAL"关键字,如果找到,它应该打印与JUDGE OF APPEAL相关的所有记录。请注意我没有手动定义我的数组。

Array
(
    [0] => Chief Justice Sundaresh Menon
    [1] => JUDGE ANDREW PHANG BOON LEONG
    [2] => JUDICIAL COMMISSIONER HOO SHEAU PENG
    [3] => JUDGE OF APPEAL JUDITH PRAKASH
    [4] => JUDGE OF APPEAL TAY YONG KWANG
)

我的预期结果如下:

[3] => JUDGE OF APPEAL JUDITH PRAKASH
[4] => JUDGE OF APPEAL TAY YONG KWANG

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

<?php
$temp = array(
    '0' => 'Chief Justice Sundaresh Menon',
    '1' => 'JUDGE ANDREW PHANG BOON LEONG',
    '2' => 'JUDICIAL COMMISSIONER HOO SHEAU PENG',
    '3' => 'JUDGE OF APPEAL JUDITH PRAKASH',
    '4' => 'JUDGE OF APPEAL TAY YONG KWANG');

     foreach($temp as $k=>$value){
        if (preg_match("/(JUDGE OF APPEAL)/i", $value)) {
            echo $value."<br>"  ;
        }
}
?>

答案 1 :(得分:-1)

请看这里:http://sandbox.onlinephpfunctions.com/code/ae28e4aee264a1ea26048681719b0e89ad19b72a

  $array = [
    'Chief Justice Sundaresh Menon',
    'JUDGE ANDREW PHANG BOON LEONG',
    'JUDICIAL COMMISSIONER HOO SHEAU PENG',
    'JUDGE OF APPEAL JUDITH PRAKASH',
    'JUDGE OF APPEAL TAY YONG KWANG'
  ];

  print_r(array_filter($array, function($s) { return (strpos( $s, 'JUDGE OF APPEAL' ) !== false); }));