0

I am new to php and i want to remove element from array Here is my array:

Array
(
[Total] => 21600000
[Items] => Array
    (
        [2-13] => Array
            (
                [Item] => 2
                [PID] => 13
                [UPrice] => 11000000
                [Qty] => 1
                [Total] => 11000000
            )

        [58-167] => Array
            (
                [Item] => 58
                [PID] => 167
                [UPrice] => 5300000
                [Qty] => 1
                [Total] => 5300000
            )

    )

)

And i want to remove array element by PID. I have try this but no luck:-

    $ShoppingBag =$_SESSION['ssss'];        

    if ($ShoppingBag !== null && $ShoppingBag['Total'] > 0) {          
        foreach ($ShoppingBag['Items'] as $IOrder) {               

          if($IOrder["PID"]==13)
          {                 
              unset($ShoppingBag[$IOrder]);

          }else
          {

          }
        }
    }

Please help. Thanks

4
  • 1
    You unset $IOrder variable but it doesn't actually modify the original array. Pass it by reference: foreach ($ShoppingBag['Items'] as & $IOrder) { — notice the & added before $IOrder. Commented Jun 2, 2014 at 10:51
  • do you want to remove every PID element or only PID with value "13" Commented Jun 2, 2014 at 10:51
  • i want to remove [2-13] array if PID value 13 Commented Jun 2, 2014 at 10:53
  • @MohitArora - Have you tried my provided solution ? Commented Jun 2, 2014 at 10:54

4 Answers 4

5

You can try with one simple array map :)

$arr = [
    'Total' => 21600000,
    'Items' => [
                '2-13' => [
                        'Item' => 2,
                        'PID' => 13,
                        'UPrice' => 11000000,
                        'Qty' => 1,
                        'Total' => 11000000
                ],
                '58-167'=> [
                        'Item' => 58,
                        'PID' => 167,
                        'UPrice' => 5300000,
                        'Qty' => 1,
                        'Total' => 5300000
                ]
            ]
    ];

$test = array_map(function($ar) { 
    foreach($ar as $k=>$i) { 
        if( isset($i['PID']) && $i['PID'] == '13') 
            unset($ar[$k]); 
    } 
    return $ar; } , $arr);

var_dump($test);
Sign up to request clarification or add additional context in comments.

Comments

2

You need 2 loop to do the action you want.

foreach($my_array as $key=>$value)
{
    if(is_array($value))
    {
       foreach($value as $k=>$v)
       {
         if($k == 'PID')
         {
             unset($value[$k]);
         }
       }
   }
}

with this you can remove only element with key PID.

4 Comments

reason for downvote? This is a legit answer explain the reason for down vote
It doesn't work, the $k variable will be PID for both rows, but they don't want to remove all of them, they want to remove entries with a PID of a specific value.
so we can check with value for example if $v == 13 then use unset.
@scragar: this is a generic answer tho i didnt answer it but i think it clears the intention fairly so i dont see a point on downvoting it just for specific reason
2

Hi youre unsetting the $IOrder instead of the Item that you want to delete: This code is a solution an i tested it :

$ShoppingBag = Array
(
"Total" => 21600000,
"Items" => Array
    (
        "2-13" => Array
            (
                "Item" => 2,
                "PID" => 13,
                "UPrice" => 11000000,
                "Qty" => 1,
                "Total" => 11000000,
            ),

        "58-167" => Array
            (
                "Item" => 58,
                "PID" => 167,
                "UPrice" => 5300000,
                "Qty" => 1,
                "Total" => 5300000,
            ),

    ),

);

foreach($ShoppingBag["Items"] as $key => $value){
    if($value["PID"]==13){
        unset($ShoppingBag["Items"][$key]);
    }
}

You should know that always when you're using foreach loop the foreach( $a as $b ) when you do something to $b , $a remains the same because tey are different variables :) Hope it will help you .

Regards.

Comments

0
$arr = [
    'Total' => 21600000,
    'Items' => [
                '2-13' => [
                        'Item' => 2,
                        'PID' => 13,
                        'UPrice' => 11000000,
                        'Qty' => 1,
                        'Total' => 11000000
                ],
                '58-167'=> [
                        'Item' => 58,
                        'PID' => 167,
                        'UPrice' => 5300000,
                        'Qty' => 1,
                        'Total' => 5300000
                ]
            ]
    ];

$pid_to_remove = 13;

$new_ar = array_filter(
    $arr,
    function ($v) using ($pid_to_remove) {
        return (!isset($v['PID'])) || ($v['PID'] != $pid_to_remove);
    }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.