2017-01-20 1 views
0

나는 다음과 같은 데이터가 있습니다출력 데이터 필터링 방법 JSON foreach with 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에서 모든 값을 반환

$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 w ith foreach?

+1

, 나는 당신이 무엇을 출력을위한 의미? 현재 출력은'AX5 AX10 AX25 AX50 AX100 B25 B50 B100 B150 B200 C5 C10 C20 C50 C100 E5 E10 E25'Ax와 C 만 원합니까? – webDev

+0

예, AX 및 C 만 –

+0

PHP에서 사용 가능한 문자열 함수를 확인하는 데 필요한 솔루션을 확인하십시오. – webDev

답변

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()을 사용할 수 있습니다 - 하나 AX [번호]와 C [수]입니다 아래 문자열

+0

앞으로 더 많은 데이터 'kode'가 있습니다. 이 코드는 데이터 'kode'AXX 또는 AXB 또는 CAB 또는 CB AX [number] 및 C [number] 만 가능합니다. –

+0

만약 그렇다면'substr ($ hasilz-> kode, 0,1) == "A"|| substr ($ hasilz-> kode, 0,1) == "C"'를 사용하여 첫번째 문자 그것의 다른 문자가 중요하지 않기 때문에. – webDev

0

Here is online regex tester의 첫 번째 항목을 찾습니다.

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

스크립트

[[email protected] 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; 
} 
?> 

출력

[[email protected] tmp]$ php test.php 
AX5 
AX10 
AX25 
AX50 
AX100 
C5 
C10 
C20 
C50 
C100 
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