Enoch
Enoch

Reputation: 1001

Matching array key and value and displaying only those in the matched result

I am working with an API which provides a result set in JSON, I transform that data into a PHP array to work with it on my interface. The issue i'm having now is trying to filter the data based on a particular key having a certain value.

Here is the result set returned:

[  
   {  
      "id":5,
      "firstname":"Joel ",
      "lastname":"Abase",
      "displayName":"Abase, Joel ",
      "officeId":3,
      "officeName":"Birmingham",
      "isLoanOfficer":true,
      "isActive":true
   },
   {  
      "id":1,
      "firstname":"Precious ",
      "lastname":"Love",
      "displayName":"Love, Precious ",
      "officeId":4,
      "officeName":"Manchester",
      "isLoanOfficer":true,
      "isActive":true
   },
   {  
      "id":2,
      "firstname":"Bernard ",
      "lastname":"Aikins",
      "displayName":"Aikins, Bernice ",
      "officeId":2,
      "officeName":"Manchester",
      "isLoanOfficer":false,
      "isActive":true
   },
   {  
      "id":8,
      "firstname":"Kwame",
      "lastname":"Joseph",
      "displayName":"Joseph, Kwame",
      "officeId":2,
      "officeName":"Manchester",
      "isLoanOfficer":true,
      "isActive":true,
      "joiningDate":[  
         2018,
         5,
         1
      ]
   },
   {  
      "id":4,
      "firstname":"Janine ",
      "lastname":"Hayden",
      "displayName":"Hayden, Janine ",
      "officeId":1,
      "officeName":"Head Office",
      "isLoanOfficer":false,
      "isActive":true
   },
   {  
      "id":6,
      "firstname":"Esther",
      "lastname":"Monroe",
      "displayName":"Monroe, Esther",
      "officeId":2,
      "officeName":"London",
      "isLoanOfficer":true,
      "isActive":true,
      "joiningDate":[  
         2017,
         11,
         1
      ]
   }
]

I would like to filter the results in a loop where the 'isLoanOfficer' is equal to true, essentially only showing those who are loan officers.

Here is what I have tried so far:

<div class="col-sm-5">
<label class="control-label" for="staffId">Loan Officer<span style="color:red;">*</span></label>
<?php 
$s = 0;
$temp_staff = json_decode($staff_json, true);
$count_staff = count($temp_staff);
    echo '<select class="form-control" type="text" name="staffId" required>';
                echo "<option value=". ">" . " </option>";
                    while($s < $count_staff && $temp_staff[$s]['isLoanOfficer'] == true){   

                        echo "<option value=" . $temp_staff[$s]['id'] . ">" . $temp_staff[$s]['displayName'] . " </option>";
                    $s++;


                } 
    echo '</select>'; 
?>
<br>
</div>

I also tried adding this code inside the while loop:

if($temp_staff[$s]['isLoanOfficer'] == true) {  
echo "<option value=" . $temp_staff[$s]['id'] . ">" . $temp_staff[$s]['displayName'] . " </option>";
                $s++;
}

It seems that it only evaluates the first value and then exits. Any help?

Upvotes: 1

Views: 180

Answers (4)

The fourth bird
The fourth bird

Reputation: 163477

If you want to keep the arrays which only have "isLoanOfficer":true you could use array_filter:

$temp_staff = array_filter($temp_staff, function($x){
    return $x["isLoanOfficer"] === true;
}
);

Demo

Upvotes: 2

Andreas
Andreas

Reputation: 23958

You do not need to loop.

You can use array_intersect, array_intersect_key and array_column to find the true values.

First filter out the loanofficer column and use array_intersect to only get the true values.
Now we have an array with true values and it's corresponding key.
Use array_intersect_key to match it with the original array and the return is your filtered array.

$arr = json_decode($str, true);
$loan = array_intersect(array_column($arr, "isLoanOfficer"), [true]);
var_dump(array_intersect_key($arr, $loan));

From here is't a simple foreach outputing every value or use implode to build the output string.

https://3v4l.org/eh4uD

Output:

array(4) {
  [0]=>
  array(8) {
    ["id"]=>
    int(5)
    ["firstname"]=>
    string(5) "Joel "
    ["lastname"]=>
    string(5) "Abase"
    ["displayName"]=>
    string(12) "Abase, Joel "
    ["officeId"]=>
    int(3)
    ["officeName"]=>
    string(10) "Birmingham"
    ["isLoanOfficer"]=>
    bool(true)
    ["isActive"]=>
    bool(true)
  }
  [1]=>
  array(8) {
    ["id"]=>
    int(1)
    ["firstname"]=>
    string(9) "Precious "
    ["lastname"]=>
    string(4) "Love"
    ["displayName"]=>
    string(15) "Love, Precious "
    ["officeId"]=>
    int(4)
    ["officeName"]=>
    string(10) "Manchester"
    ["isLoanOfficer"]=>
    bool(true)
    ["isActive"]=>
    bool(true)
  }
  [3]=>
  array(9) {
    ["id"]=>
    int(8)
    ["firstname"]=>
    string(5) "Kwame"
    ["lastname"]=>
    string(6) "Joseph"
    ["displayName"]=>
    string(13) "Joseph, Kwame"
    ["officeId"]=>
    int(2)
    ["officeName"]=>
    string(10) "Manchester"
    ["isLoanOfficer"]=>
    bool(true)
    ["isActive"]=>
    bool(true)
    ["joiningDate"]=>
    array(3) {
      [0]=>
      int(2018)
      [1]=>
      int(5)
      [2]=>
      int(1)
    }
  }
  [5]=>
  array(9) {
    ["id"]=>
    int(6)
    ["firstname"]=>
    string(6) "Esther"
    ["lastname"]=>
    string(6) "Monroe"
    ["displayName"]=>
    string(14) "Monroe, Esther"
    ["officeId"]=>
    int(2)
    ["officeName"]=>
    string(6) "London"
    ["isLoanOfficer"]=>
    bool(true)
    ["isActive"]=>
    bool(true)
    ["joiningDate"]=>
    array(3) {
      [0]=>
      int(2017)
      [1]=>
      int(11)
      [2]=>
      int(1)
    }
  }
}

Upvotes: 2

Bal&#225;zs Varga
Bal&#225;zs Varga

Reputation: 1856

The easiest and most readable way, using a foreach:

$temp_staff = json_decode($staff_json, true);
foreach($temp_staff as $member) {
    if($member['isLoanOfficer']) {
        echo '<option value="' . $member['id'] . '">' . $member['displayName'] . '</option>';
    }
}

Upvotes: 0

Kishen Nagaraju
Kishen Nagaraju

Reputation: 2200

You'll have to use the condition inside the loop. Try the below code :

while($s < $count_staff){
    if ($temp_staff[$s]['isLoanOfficer'] == true) {
        echo "<option value=" . $temp_staff[$s]['id'] . ">" . $temp_staff[$s]['displayName'] . " </option>";
    }
    $s++;
}

Due to that condition your $s++ is not being executed properly.

Hope this helps.

Upvotes: 2

Related Questions