简体   繁体   中英

How two compare two JSON objects array in php?

I have JSON objects array as shown below. The following JSON objects array is in the file (process/ptp-hello.json)

{
    "list": ["1", "2"],
    "code": ["ABCD", "DEFG", "PQRT", "KJHG", "OOPO", "MNBG", "IUYT"]
}

Php code:

<?php
if (file_exists('process/ptp-hello.json')) {
    $letter = json_decode(file_get_contents('process/ptp-hello.json'));  // Line A
}
?>

<?php foreach ($letter->list as $key => $value) { ?>
    <a href="/en/?s=&port=<?php echo $letter->code[$value]; ?>">
        <div class="beats">
            <div class="color-green"><?php echo $letter->code[$value]; ?></div>    // Line B 
        </div>
    </a>
<?php }

Line B prints DEFG and PQRT

Problem Statement:

I am wondering what changes I should make in the php code above so that it prints ABCD, KJHG, OOPO, MNBG and IUYT.

In short, I want to remove those elements from code whose indices are listed in list

You can filter the codes like this :

$remainingCodes = array_diff_key($data_house->code, array_flip($data_house->joint_committees));

var_dump($remainingCodes);
// array(5) { [0]=> string(4) "CACN" [3]=> string(4) "CIMM" [4]=> string(4) "ENVI" [5]=> string(4) "ETHI" [6]=> string(4) "FAAE" }

Then you can loop over the $remainingCodes variable.

This is how it works :

  1. Flip the keys and values of $data_house->joint_committees array with array_flip
  2. Remove the keys of $data_house->code corresponding to the values of the last array witharray_diff_key

There are many ways to achieve this, three are listed below:

1. Iterate over code but don't print anything if key exists in joint_committees

<?php foreach ($data_house->code as $key => $value) {
    if (!in_array($key, $data_house->joint_committees) { ?>
        <!-- Your HTML here -->
    <?php }
}

Note: This could also be done by having if (!in_array($key, $data_house->joint_committees) continue; without putting putting the HTML into the condition.

2. Loop over joint_committees first and remove any matching items from code

<?php
for ($data_house->joint_committees as $index) {
    $data_house->code = array_splice($data_house, $index, 1);
}

foreach ($data_house->code as $value) { ?>
    <!-- Your HTML here -->
<?php }

Note: You could also use unset($data_house->code[$index]) but it would leave a "weird" array with non-consecutive indices.

3. Map code to an array without the joint_committees indices

<?php
$filteredCodes = array_filter($data_house->code, function ($key) use ($data_house) {
    return !in_array($key, $data_house->joint_committees)
}, ARRAY_FILTER_USE_KEY);

foreach ($filteredCodes as $value) { ?>
    <!-- Your HTML here -->
<?php }

Note: Starting with PHP 7.4, the array_filter call can be simplified to this:

$filteredCodes = array_filter($data_house->code, fn($key) => !in_array($key, $data_house->joint_committees), ARRAY_FILTER_USE_KEY);

Update: Another, probably even more elegant solution was proposed by Brewal in their answer . :-)

It should be as simple as

$json = '{
"joint_committees": ["1", "2"],
"code": ["CACN", "CHPC", "CIIT", "CIMM", "ENVI", "ETHI", "FAAE"]
}';

$data_house = json_decode($json, true);


foreach($data_house['code'] as $key => $val) {
    if(!in_array($key, $data_house['joint_committees'])) {
        echo $key . ' ' . $val ."\n";
    }
}

This returns

0 CACN
3 CIMM
4 ENVI
5 ETHI
6 FAAE

You can do a lot with the return, make a new array, echo out information, whatever you'd like to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM