0

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

3
  • 1
    I'm not sure I full understand your question. Do you mean you want to remove those elements from code whose indices are listed in joint_committees? Commented Mar 11, 2020 at 16:09
  • @CherryDT Yes, you are right. . Commented Mar 11, 2020 at 16:13
  • 1
    @CherryDT I will update my question with your lines. Commented Mar 11, 2020 at 16:22

3 Answers 3

2

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. :-)

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

1 Comment

Hi, I hope all is well. I have one more question. It's not similar. I am wondering if you can have a look.
2

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 with array_diff_key

1 Comment

I was first making sure to convert joint_committees values to integers, but it doesn't seems to change anything, even with strict_types in PHP7+
0

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.

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.