返回特定数组键的值


return values of specific array key

我读了一些问题,但我没有解决我的问题,我正在使用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'
    )
);

现在我想删除两个元素unitPricedescription

$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'
    )
);
您需要

的 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.
foreach($product as $key => $prod) {
    unset($product[$key]['unitPrice']);
    unset($product[$key]['description']);
}

功能方法,作为对所有其他选项的平衡:

$customProduct = array_map(function (array $product) {
    return array_diff_key($product, array_flip(['unitPrice', 'description']));
}, $product);
$customProduct=array();
foreach($product as $p){
  if(isset($p['description'])){
      unset($p['description']);
  }
  if(isset($p['unitPrice'])){
      unset($p['unitPrice']);
  }
  array_push($customProduct,$p);
}

试试我更新的答案

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
    )
  )

只需运行一个循环.使用下面的代码

<?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);

希望这对你有帮助