匹配数组键和值,仅显示匹配结果中的那些

时间:2018-05-21 09:28:14

标签: php arrays json

我正在使用一个在JSON中提供结果集的API,我将该数据转换为PHP数组,以便在我的界面上使用它。 我现在遇到的问题是尝试根据具有特定值的特定密钥过滤数据。

这是返回的结果集:

[  
   {  
      "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
      ]
   }
]

我想在循环中过滤结果,其中' isLoanOfficer'等于是真的,基本上只显示那些贷款人员。

这是我到目前为止所尝试的内容:

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

我也尝试在while循环中添加此代码:

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

它似乎只评估第一个值然后退出。有什么帮助吗?

4 个答案:

答案 0 :(得分:2)

您必须使用循环内的条件。请尝试以下代码:

{{1}}

由于这种情况,你的$ s ++没有正确执行。

希望这有帮助。

答案 1 :(得分:2)

您无需循环。

您可以使用array_intersectarray_intersect_keyarray_column来查找真实值。

首先过滤出loanofficer列并使用array_intersect仅获取真值 现在我们有一个具有真值的数组及其对应的键 使用array_intersect_key将其与原始数组匹配,返回是过滤后的数组。

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

从这里开始是一个简单的foreach输出每个值或使用implode来构建输出字符串。

https://3v4l.org/eh4uD

输出:

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)
    }
  }
}

答案 2 :(得分:2)

如果您想保留只有"isLoanOfficer":true的数组,可以使用array_filter

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

Demo

答案 3 :(得分:0)

最简单,最易阅读的方式,使用foreach

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