返回特定数组键的值

Imr*_*ran 2 php arrays

我读了一些问题并且我没有解决我的问题我使用的是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'
    )
);
Run Code Online (Sandbox Code Playgroud)

现在我要删除两个元素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'
    )
);
Run Code Online (Sandbox Code Playgroud)

Ran*_*gad 6

您需要的PHP命令是 unset(array[key]),您可以通过迭代来访问数组中的各个索引.

基本解决方案如下所示.请注意,这会修改您的原始阵列.如果那不是你想要的那么首先将product数组分配给另一个变量(下面的第二个例子):

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

var_dump($product);
Run Code Online (Sandbox Code Playgroud)

会成为:

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

var_dump($customProduct);
// $product will have its original value.
Run Code Online (Sandbox Code Playgroud)