使用 PHP 中的变量取消设置数组
unset array using a variable in PHP
这是我的阵列样本:
Array
(
[productId] => 7740792
[productCode] => 1019534
[productPrice] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 0
[text] => £0.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
[variants] => Array
(
[0] => Array
(
[variantId] => 7740915
[sku] => 5784194
[isInStock] => 1
[isLowInStock] => 1
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
[1] => Array
(
[variantId] => 7740906
[sku] => 5784195
[isInStock] => 1
[isLowInStock] =>
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
我想从整个数组中 delete/unset "productPrice"、"sku" 和 "price"。
到目前为止我用过这个:
unset($alldata[0]['variants'][0]['price']);
unset($alldata[0]['variants'][1]['price']);
但是数组发生了变化并且有数千个条目,因此像这样对 unset 进行编码并不容易。我是 PHP 的新手,我已经尽我所能地搜索了数组函数,但没有找到合适的东西。
unset ($alldata['productPrice']);
foreach ($alldata['variants'] as $key => $value) {
unset (
$alldata['variants'][$key]['price'],
$alldata['variants'][$key]['sku']
);
}
这是我的阵列样本:
Array
(
[productId] => 7740792
[productCode] => 1019534
[productPrice] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 0
[text] => £0.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
[variants] => Array
(
[0] => Array
(
[variantId] => 7740915
[sku] => 5784194
[isInStock] => 1
[isLowInStock] => 1
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
[1] => Array
(
[variantId] => 7740906
[sku] => 5784195
[isInStock] => 1
[isLowInStock] =>
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
我想从整个数组中 delete/unset "productPrice"、"sku" 和 "price"。 到目前为止我用过这个:
unset($alldata[0]['variants'][0]['price']);
unset($alldata[0]['variants'][1]['price']);
但是数组发生了变化并且有数千个条目,因此像这样对 unset 进行编码并不容易。我是 PHP 的新手,我已经尽我所能地搜索了数组函数,但没有找到合适的东西。
unset ($alldata['productPrice']);
foreach ($alldata['variants'] as $key => $value) {
unset (
$alldata['variants'][$key]['price'],
$alldata['variants'][$key]['sku']
);
}