如何用PHP过滤输出数据JSON foreach?

时间:2017-01-20 16:46:00

标签: php json loops filtering

我有以下数据:

$data = '
[{"kode":"AX5","harga":"6200","status":"1"},
{"kode":"AX10","harga":"11250","status":"1"},
{"kode":"AX25","harga":"25750","status":"1"},
{"kode":"AX50","harga":"50800","status":"1"},
{"kode":"AX100","harga":"100600","status":"1"},
{"kode":"B25","harga":"25500","status":"1"},
{"kode":"B50","harga":"49800","status":"1"},
{"kode":"B100","harga":"99100","status":"1"},
{"kode":"B150","harga":"147850","status":"1"},
{"kode":"B200","harga":"196600","status":"1"},
{"kode":"C5","harga":"4750","status":"2"},
{"kode":"C10","harga":"9775","status":"2"},
{"kode":"C20","harga":"19850","status":"2"},
{"kode":"C50","harga":"50100","status":"2"},
{"kode":"C100","harga":"100050","status":"2"},
{"kode":"E5","harga":"5100","status":"1"},
{"kode":"E10","harga":"10425","status":"1"},
{"kode":"E25","harga":"25350","status":"1"}]'

以下代码段返回kode的所有值:

$hasil = json_decode($data);
foreach ($hasil as $hasilz) {
    echo $hasilz->kode . PHP_EOL;
}

我想过滤代码并只显示以下值(AXC):

AX5 AX10 AX25 AX50 AX100 
C5 C10 C20 C50 C100

任何正文都可以帮助我使用$hasilz->kode过滤foreach

3 个答案:

答案 0 :(得分:0)

此处,如果您知道字符串模式和字符串的长度,请使用 substr()方法检查字符串的内容。

foreach ($hasil as $hasilz) {
  if(substr($hasilz->kode,0,2)=="AX"||substr($hasilz->kode,0,1)=="C"){
     echo $hasilz->kode . PHP_EOL; 
  }
}

您还可以使用$hasilz->kode[0]=='A' and $hasilz->kode[0]=='C'检查第一个字符。
您还可以使用strstr() - 查找字符串的第一个匹配项

答案 1 :(得分:0)

Here is online regex tester,下面是AX [编号]和C [编号]。

foreach(json_decode($data) as $hasilz) 
{
    if(preg_match("/^(AX|C)\d+$/",$hasilz->kode)) 
            echo $hasilz->kode . PHP_EOL;
}

<强>脚本

[akshay@gold tmp]$ cat test.php
<?php
$data = '
[{"kode":"AX5","harga":"6200","status":"1"},
{"kode":"AX10","harga":"11250","status":"1"},
{"kode":"AX25","harga":"25750","status":"1"},
{"kode":"AX50","harga":"50800","status":"1"},
{"kode":"AX100","harga":"100600","status":"1"},
{"kode":"B25","harga":"25500","status":"1"},
{"kode":"B50","harga":"49800","status":"1"},
{"kode":"B100","harga":"99100","status":"1"},
{"kode":"B150","harga":"147850","status":"1"},
{"kode":"B200","harga":"196600","status":"1"},
{"kode":"C5","harga":"4750","status":"2"},
{"kode":"C10","harga":"9775","status":"2"},
{"kode":"C20","harga":"19850","status":"2"},
{"kode":"C50","harga":"50100","status":"2"},
{"kode":"C100","harga":"100050","status":"2"},
{"kode":"E5","harga":"5100","status":"1"},
{"kode":"E10","harga":"10425","status":"1"},
{"kode":"E25","harga":"25350","status":"1"}, 
{"kode":"CXX25","harga":"25350","status":"1"}]';

foreach(json_decode($data) as $hasilz) 
{
    if(preg_match("/^(AX|C)\d+$/",$hasilz->kode)) 
            echo $hasilz->kode . PHP_EOL;
}
?>

<强>输出

[akshay@gold tmp]$ php test.php
AX5
AX10
AX25
AX50
AX100
C5
C10
C20
C50
C100

答案 2 :(得分:0)

此解决方案使用array_filter考虑了更具功能性的范例。唉php并不支持some,因此不能轻易避免使用一个foreach。

<?php
$data = '[{"kode":"AX5","harga":"6200","status":"1"},
{"kode":"AX10","harga":"11250","status":"1"},
{"kode":"AX25","harga":"25750","status":"1"},
{"kode":"AX50","harga":"50800","status":"1"},
{"kode":"AX100","harga":"100600","status":"1"},
{"kode":"B25","harga":"25500","status":"1"},
{"kode":"B50","harga":"49800","status":"1"},
{"kode":"B100","harga":"99100","status":"1"},
{"kode":"B150","harga":"147850","status":"1"},
{"kode":"B200","harga":"196600","status":"1"},
{"kode":"C5","harga":"4750","status":"2"},
{"kode":"C10","harga":"9775","status":"2"},
{"kode":"C20","harga":"19850","status":"2"},
{"kode":"C50","harga":"50100","status":"2"},
{"kode":"C100","harga":"100050","status":"2"},
{"kode":"E5","harga":"5100","status":"1"},
{"kode":"E10","harga":"10425","status":"1"},
{"kode":"E25","harga":"25350","status":"1"}]';

$data = json_decode($data);

$wantedKodes = [
    'AX',
    'C',
];

$filtered = array_filter(
    $data,
    function ($object) use ($wantedKodes) {
        foreach ($wantedKodes as $kode) {
            if (strpos($object->kode, $kode) === 0) {
                return true;
            }
        }

        return false;
    }
);

foreach ($filtered as $item) {
    echo "{$item->kode} \n";
}

输出将是:

AX5 
AX10 
AX25 
AX50 
AX100 
C5 
C10 
C20 
C50 
C100