0

I have the following data :

$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"}]'

The following code-snippet returns all values from kode:

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

I want to filter code and only show the following values (AX and C):

AX5 AX10 AX25 AX50 AX100 
C5 C10 C20 C50 C100

Any body can help me filter $hasilz->kode with foreach ?

4
  • 1
    What you want, I mean what is you intended output? Currently your output is AX5 AX10 AX25 AX50 AX100 B25 B50 B100 B150 B200 C5 C10 C20 C50 C100 E5 E10 E25 Do you want only Ax and C? Commented Jan 20, 2017 at 16:52
  • Yes, AX and C only Commented Jan 20, 2017 at 16:53
  • Check the solution you just need to see the string functions available in php. Commented Jan 20, 2017 at 16:57
  • Looking at comments, the question got more clear, added that to the question to clearify what OP's expected result is. Commented Jan 24, 2017 at 13:55

3 Answers 3

1

Here, Use substr() method to check the content of the string if you know the string pattern and length of the string.

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

You can also use $hasilz->kode[0]=='A' and $hasilz->kode[0]=='C' to check the first character.
You can also use strstr() — Find the first occurrence of a string

Sign up to request clarification or add additional context in comments.

2 Comments

In the future I have more data 'kode' This code will show me data 'kode' AXX or AXB or CAB or CB it's possible filter AX[number] and C[number] only ?
ok if so then use substr($hasilz->kode,0,1)=="A"||substr($hasilz->kode,0,1)=="C" to check only the first character because its does not matter other character.
0

Here is online regex tester, below one is for AX[number] and C[number].

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

Script

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

Output

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

Comments

0

This solution takes into account a more functional paradigm using array_filter. Alas php does not support some nativly, hence one foreach could not have been avoided easily.

<?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";
}

Output will be:

AX5 
AX10 
AX25 
AX50 
AX100 
C5 
C10 
C20 
C50 
C100

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.