我有一个返回关联数组的查询。在该数组中是我必须在输出之前删除的字段。我已尝试过array_diff并取消设置,但没有任何运气。
开始数组:
Array
(
[id] => 13461
[ln] => 605717
[ptyp] => COND
[lag] => 86484
[st] => S
[lp] => 475000
[sp] => 475000
[olp] => 475000
[hsn] => 2033
)
我需要删除3个项目,每个实例的值都不同(键是相同的)。这是我尝试过的,但输出并不是我想要的。
$result = array( ['id'] => '13461', ['ln'] => '605717', ['ptyp'] => 'COND', ['lag'] => '86484', ['st'] => 'S', ['lp'] => '475000', ['sp'] => '475000', ['olp'] => '475000', ['hsn'] => '2033');
while ($row = $result) {
// remove empty values
$filtered = array_filter($row);
// array of disallowed keys
$disallowed = array($row['lp'],$row['lph'],$row['olp']);
foreach ($filtered as $filter) {
# code...
unset($disallowed);
}
echo "<pre>";
print_r ($filter);
echo "</pre>";
}
编辑:我的$ disallowed数组中的项目不一定存在于每一行中,但如果它们必须被删除。
答案 0 :(得分:0)
我希望我理解这是正确的。我只想删除密钥符合您要求的元素。
$result = array( ['id'] => '13461', ['ln'] => '605717', ['ptyp'] => 'COND', ['lag'] => '86484', ['st'] => 'S', ['lp'] => '475000', ['sp'] => '475000', ['olp'] => '475000', ['hsn'] => '2033');
while ($row = $result) {
// remove empty values
$filtered = array_filter($row);
// define an array of disallowed keys
$disallowed = array('lp','lph','olp');
foreach ($disallowed as $disallowed_key) {
// code...
unset($row["$disallowed_key"]); // remove the element which key is not allowed
}
echo "<pre>";
print_r ($row); // output the final result with all disallowed elements removed
echo "</pre>";
}