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

6 Answers 6

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.
Sign up to request clarification or add additional context in comments.

Comments

3
foreach($product as $key => $prod) {
    unset($product[$key]['unitPrice']);
    unset($product[$key]['description']);
}

2 Comments

You hit the nail on the head.
thanks :). it's in my opinion the easiest way to solve imrans problem.
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);

1 Comment

BTW, if you gotta use unset: it accepts multiple arguments... unset($data['unitPrice'], $data['description']). Just slightly more compact code.
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 Comments

You don't need to and shouldn't check with isset before unset, it's entirely superfluous.
@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.
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
    )

  )

1 Comment

actually i forgot to add & in $products
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.