Psy*_*che 6 php arrays multidimensional-array
给定以下数组:
Array
(
[0] => Array
(
[id_contract] => 1
[contract_months] => 5
[months_with_expenses] => 1
)
[1] => Array
(
[id_contract] => 2
[contract_months] => 12
[months_with_expenses] => 0
)
[2] => Array
(
[id_contract] => 3
[contract_months] => 1
[months_with_expenses] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
如何从数组中删除键“contract_months”与键“month_with_expenses”不匹配的所有元素?
我正在使用 PHP。
尝试这个:
foreach ($array as $key => $element) {
if (conditions) {
unset($array[$key]);
}
}
Run Code Online (Sandbox Code Playgroud)
一个带有 if 条件和 unset 的简单循环就可以了
<?php
for( i=0; i<count($arr); i++){
if($arr[i]['contract_months'] != $arr[i]['months_with_expenses']) {
unset($arr[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7224 次 |
最近记录: |