我读了一些question并且我没有解决我的问题我使用的是array_column()但我对这个愚蠢的问题感到困惑
我有一个数组$product
$product = array(
0 => array(
'id' => '123',
'name' => 'Facebook status robot',
'description'=> 'Post your wall in your given time',
'quantity' => '1',
'unitPrice' => '120',
'taxable' => 'true'
),
1 => array(
'id' => '123',
'name' => 'Facebook status robot',
'description'=> 'Post your wall in your given time',
'quantity' => '1',
'unitPrice' => '120',
'taxable' => 'true'
),
2 => array(
'id' => '123',
'name' => 'Facebook status robot',
'description'=> 'Post your wall in your given time',
'quantity' => '1',
'unitPrice' => '120',
'taxable' => 'true'
)
);
现在我要删除两个元素unitPrice
和description
$customProduct = array(
0 => array(
'id' => '123',
'name' => 'Facebook status robot',
'quantity' => '1',
'taxable' => 'true'
),
1 => array(
'id' => '123',
'name' => 'Facebook status robot',
'quantity' => '1',
'taxable' => 'true'
),
2 => array(
'id' => '123',
'name' => 'Facebook status robot',
'quantity' => '1',
'taxable' => 'true'
)
);
答案 0 :(得分:6)
您需要的PHP命令是unset(array[key])
,您可以通过迭代来访问数组中的各个索引。
基本解决方案如下所示。请注意,这会修改您的原始阵列。如果那不是你想要的,那么首先将product
数组分配给另一个变量(下面的第二个例子):
foreach($product as &$data) {
unset($data['unitPrice']);
unset($data['description']);
}
var_dump($product);
会变成:
$customProduct = $product;
foreach($customProduct as &$data) {
unset($data['unitPrice']);
unset($data['description']);
}
var_dump($customProduct);
// $product will have its original value.
答案 1 :(得分:3)
foreach($product as $key => $prod) {
unset($product[$key]['unitPrice']);
unset($product[$key]['description']);
}
答案 2 :(得分:2)
功能性方法,作为对所有其他选项的抵消:
$customProduct = array_map(function (array $product) {
return array_diff_key($product, array_flip(['unitPrice', 'description']));
}, $product);
答案 3 :(得分:1)
$customProduct=array();
foreach($product as $p){
if(isset($p['description'])){
unset($p['description']);
}
if(isset($p['unitPrice'])){
unset($p['unitPrice']);
}
array_push($customProduct,$p);
}
答案 4 :(得分:1)
尝试我的最新回答
foreach($product as &$products) {
unset($products['unitPrice']);
unset($products['description']);
}
print_r($product);
输出
Array
(
[0] => Array
(
[id] => 123
[name] => Facebook status robot
[quantity] => 1
[taxable] => true
)
[1] => Array
(
[id] => 123
[name] => Facebook status robot
[quantity] => 1
[taxable] => true
)
[2] => Array
(
[id] => 123
[name] => Facebook status robot
[quantity] => 1
[taxable] => true
)
)
答案 5 :(得分:1)
只需运行一个循环。使用下面的代码
<?php
$product = array(
0 => array(
'id' => '123',
'name' => 'Facebook status robot',
'description'=> 'Post your wall in your given time',
'quantity' => '1',
'unitPrice' => '120',
'taxable' => 'true'
),
1 => array(
'id' => '123',
'name' => 'Facebook status robot',
'description'=> 'Post your wall in your given time',
'quantity' => '1',
'unitPrice' => '120',
'taxable' => 'true'
),
2 => array(
'id' => '123',
'name' => 'Facebook status robot',
'description'=> 'Post your wall in your given time',
'quantity' => '1',
'unitPrice' => '120',
'taxable' => 'true'
)
);
$p= count($product);
for($i=0;$i<=$p;$i++){
unset($product[$i]["description"]);
unset($product[$i]["unitPrice"]);
}
print_r($product);
希望这有助于你