2

I read some question and I didn't solve my problem I was use array_column() but I am confused of this silly problem

I have an array $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

$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'
    )
);
Community
  • 1
  • 1
Imran
  • 3,031
  • 4
  • 25
  • 41

6 Answers6

6

The PHP command you need is unset(array[key]), you can access individual indexes in your array by iterating over it.

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

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.
Rangad
  • 2,110
  • 23
  • 29
3
foreach($product as $key => $prod) {
    unset($product[$key]['unitPrice']);
    unset($product[$key]['description']);
}
goldlife
  • 1,949
  • 3
  • 29
  • 48
2

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);
deceze
  • 510,633
  • 85
  • 743
  • 889
  • BTW, if you gotta use `unset`: it accepts multiple arguments... `unset($data['unitPrice'], $data['description'])`. Just slightly more compact code. – deceze Jan 13 '15 at 13:30
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);
}
  • 2
    You don't need to and shouldn't check with `isset` before `unset`, it's entirely superfluous. – deceze Jan 13 '15 at 12:58
  • @deceze I am thankful to you. Your comment forced me to re-read the PHP manual and I found something more interesting. It says - isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does. Though it is not relevant here but helped me somewhere else. – Pushpendra Singh Thakur Jan 13 '15 at 13:13
1

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
    )

  )
Priyank
  • 3,778
  • 3
  • 29
  • 48
1

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

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38