我有以下数据:
$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;
}
我想过滤代码,只显示以下值(AX
和C
):
AX5 AX10 AX25 AX50 AX100
C5 C10 C20 C50 C100
任何身体都可以帮助我用foreach
过滤$hasilz->kode
?
在这里,如果您知道字符串的字符串模式和长度,请使用 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()
— 查找字符串
这是在线正则表达式测试器,下面是 AX[number] 和 C[number]。
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
此解决方案考虑了使用 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