在php中从数组中删除元素

时间:2023-01-04 21:22:56

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

我是php新手,我想从数组中删除元素这是我的数组:

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

我想通过PID删除数组元素。我试过这个但没有运气: -

    $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


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

#2


2  

You need 2 loop to do the action you want.

你需要2个循环来完成你想要的动作。

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.

使用此功能,您只能删除带有键PID的元素。

#3


2  

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

嗨,你取消了$ IOrder而不是你要删除的项目:这段代码是我测试过的解决方案:

$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 .

你应该知道,当你使用foreach循环foreach($ a as $ b)当你对$ b执行某些操作时,$ a仍然是相同的,因为tey是不同的变量:)希望它会帮助你。

Regards.

问候。

#4


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

#1


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

#2


2  

You need 2 loop to do the action you want.

你需要2个循环来完成你想要的动作。

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.

使用此功能,您只能删除带有键PID的元素。

#3


2  

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

嗨,你取消了$ IOrder而不是你要删除的项目:这段代码是我测试过的解决方案:

$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 .

你应该知道,当你使用foreach循环foreach($ a as $ b)当你对$ b执行某些操作时,$ a仍然是相同的,因为tey是不同的变量:)希望它会帮助你。

Regards.

问候。

#4


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