我想从php中的多维数组下面删除诸如group_id
,star_rating
,$data = [];
foreach ($array as $key => $subArr) {
unset($subArr['id']);
unset($subArr['group_id']);
unset($subArr['star_rating']);
$data[$key] = $subArr;
}
print_r($data);
之类的键
我已经可以通过for循环使用下面的代码,并且可以正常工作,但是如果不使用任何单个循环会很好,尝试使用内置的php函数提供解决方案
Array
(
[0] => Array
(
[id] => 128
[receiver_id] => 18
[group_id] =>
[tipper_id] =>
[amount] => 150
[tip_type] => 3
[star_rating] =>
)
[1] => Array
(
[id] => 129
[receiver_id] => 16
[group_id] =>
[tipper_id] => 12
[amount] => 600
[tip_type] => 2
[star_rating] =>
)
[2] => Array
(
[id] => 130
[receiver_id] => 14
[group_id] =>
[tipper_id] => 18
[amount] => 750
[tip_type] => 1
[star_rating] =>
)
)
Array
(
[0] => Array
(
[receiver_id] => 18
[tipper_id] =>
[amount] => 150
[tip_type] => 3
)
[1] => Array
(
[receiver_id] => 16
[tipper_id] => 12
[amount] => 600
[tip_type] => 2
)
[2] => Array
(
[receiver_id] => 14
[tipper_id] => 18
[amount] => 750
[tip_type] => 1
)
)
删除指定的键(id,group_id,star_rating)后,结果将显示:
error_reporting(-1);
ini_set('display_errors', 'On');
$image_path = 'http://my-shop.com/img/my-shop-logo-1584646645.jpg';
$image_mime = 'image/jpg';
$args['image'] = new CurlFile($image_path, $image_mime);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'http://my-shop.com/api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
print_r($curlinfo);
答案 0 :(得分:0)
最后我已经使用array_map
$data = array_map(
function (array $subArr) {
unset($subArr['id']);
unset($subArr['group_id']);
unset($subArr['star_rating']);
return $subArr;
},
$array
);
print_r($data);