简体   繁体   English

返回特定数组键的值

[英]return values of specific array key

I read some question and I didn't solve my problem I was use array_column() but I am confused of this silly problem 我读了一些问题并且我没有解决我的问题我使用的是array_column()但是我对这个愚蠢的问题很困惑

I have an array $product 我有一个数组$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'
    )
);

Now I want remove two elements unitPrice and description 现在我要删除两个元素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'
    )
);

The PHP command you need is unset(array[key]) , you can access individual indexes in your array by iterating over it. 您需要的PHP命令是unset(array[key]) ,您可以通过遍历它来访问数组中的各个索引。

The basic solution would look like the following. 基本解决方案如下所示。 Please be aware that this would modify your original array. 请注意,这会修改您的原始阵列。 If that is not what you want assign the product array to another variable first (2nd example below): 如果这不是你想要的product数组首先分配给另一个变量(下面的第二个例子):

foreach($product as &$data) {
    unset($data['unitPrice']);
    unset($data['description']);
}

var_dump($product);

would become: 会成为:

$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']);
}

The functional approach, as counterbalance to all the other options: 功能方法,作为所有其他选项的平衡:

$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);
}

Try my updated answer 试试我的最新答案

foreach($product as &$products) {
unset($products['unitPrice']);
unset($products['description']);
 }
print_r($product);

output 产量

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
    )

  )

Just run a loop .use the code below 只需运行一个循环。使用下面的代码

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

Hope this helps you 希望这对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM