我有数组,其中有很多空字段。如何删除填充的字段,并使用空字段创建新数组。 所以,例如:
[0] => Array
(
[id] => 26
[user_type] => 2
[user_name] => Julian
[password] => b941da1629f4742de62796d51730edbb
[fpassword] => 1
[email] => akombakom@abrakadabra.dabra
[name] =>
[surname] =>
[birthday] => 0000-00-00
[country] =>
[city] =>
[adress] =>
[post_code] =>
[mob_number] => 77077412
[tel_number] => 0
[web_page] =>
[registration_date] => 2016-11-19 05:03:05
[active] => 1
[activation_code] => 714779
[last_login] => 2016-11-20 12:06:36
)
所以我想要这个:
[0] => Array
(
[name] =>
[surname] =>
[birthday] => 0000-00-00
[country] =>
[city] =>
[adress] =>
[post_code] =>
[tel_number] => 0
[web_page] =>
)
我累了array_diff($data, array(''));
,但没有发生任何事情.Thx
答案 0 :(得分:3)
你需要一个"反向" array_filter
,如:
$empty = array_filter($data, function($item) {
return empty($item);
});
但需要针对各种值进行调整,例如日期0000-00-00
不是"假的"所以它不会被empty
抓住。规则是,您需要从array_filter
的{cal}中返回true,以获取您认为的任何值"空"。
此示例中您需要的代码是:
$data = [
'int' => 100,
'str' => 'val',
'empty' => '',
'null' => null,
'date' => '0000-00-00',
];
$empty = array_filter($data, function($item) {
return empty($item) || '0000-00-00' === $item;
});
答案 1 :(得分:0)
你可以在$a1
中执行类似的操作 - 只需保留要在新数组中看到的值的规则。
$a1 = array('empty' => '', 'another_rule' => '0000-00-00');
$a2 = array(
'id' => 26,
'user_type' => 2,
'name' => '',
'birthday' => '0000-00-00',
);
$result = array_intersect($a2, $a1);