如何从对象数组中取消设置元素

发布于 2024-12-20 02:33:00 字数 5193 浏览 0 评论 0原文

我有一个操作系统数组 ($os2),我想从数组 $phones 中删除所有元素,这些元素没有这些操作系统之一。所以我这样做了,但由于某种原因它不起作用...

$os2 = array("BlackBerry", "Android");
if(count($os2)!=0)
{
    for($i = 0; $i<count($phones); $i++)
    {
        if(!in_array($phones[$i]->os, $os2))
        {
            unset($phones[$i]);
        }
    }
}

这是电话数组中的一个元素:

[0] => Equipment Object
        (
            [carrierDescription:Equipment:private] => T-Mobile
            [carrierId:Equipment:private] => 59
            [manufacturerID:Equipment:private] => 13
            [manufacturerName:Equipment:private] => BlackBerry
            [manufacturerCode:Equipment:private] => Curve 9360  
            [productId:Equipment:private] => 60558
            [currentEquipmentID:Equipment:private] => 29305
            [equipmentName:Equipment:private] => BlackBerry Curve 9360 Black
            [equipmentType:Equipment:private] => Smartphone
            [equipmentShortDescription:Equipment:private] =>  
            [equipmentLongDescription:Equipment:private] => The BlackBerry Curve 9360 for T-Mobile is the next Curve in BlackBerry's line of smartphones. Running the latest BlackBerry 7 operating system and utilizing the powerful BlackBerry 7 Webkit-browser, the BlackBerry Curve 9360 provides an intuitive smartphone experience, perfect for those who depend on their smartphone to keep their personal and work lives organized. Encased in a sleek package, including a wide 2.4 inch screen, full QWERTY keyboard and navigation track-pad, the BlackBerry Curve 9360 is ideal for efficient mobile communication. This new smartphone is also equipped with essential smartphone features such as a 5 megapixel camera with flash, video recorder, and WiFi connectivity for voice and data for a premium smartphone experience, The new Curve 9360 delivers the world's best mobile communications and social experiences with the latest release of BlackBerry Messenger, or BBM. 
            [sugRetailPrice:Equipment:private] => 400
            [displayPrice:Equipment:private] => $999.99
            [equipmentPrice:Equipment:private] => 0
            [numericCost:Equipment:private] => 0
            [rebateAmount:Equipment:private] => $0.00
            [priceAfterRebate:Equipment:private] => Free
            [rebateDescription:Equipment:private] => Free Gift
            [equipmentImageFileSmall:Equipment:private] => images/phones/60558_s.jpg
            [equipmentImageFileMedium:Equipment:private] => images/phones/60558_m.jpg
            [equipmentImageFileLarge:Equipment:private] => images/phones/60558_l.jpg
            [manufacturerLogo:Equipment:private] => _images/logos/manufacturers/selected_manu_blackberry.gif
            [purchaseMode:Equipment:private] => Add a Line
            [productLink:Equipment:private] => r.aspx?p=BlackBerry%20Curve%209360%20Black&c=T-Mobile&r=wowphonefinder&mode=1&s=1
            [upc:Equipment:private] => 610214626622
            [compatibleAccessoriesPageURL:Equipment:private] => DeviceSpecificAccessories.aspx?referringdomain=wowphonefinder&refcode1=standarddatafeed&refcode2=&productid=60558
            [mods:Equipment:private] => 
            [os:Equipment:private] => BlackBerry
            [tethering:Equipment:private] => 
            [hotspot:Equipment:private] => 
            [html:Equipment:private] => HTML
            [triG:Equipment:private] => 3G
            [fourG:Equipment:private] => 
            [fiveMP:Equipment:private] => 
            [bluetooth:Equipment:private] => bluetooth
            [camera:Equipment:private] => camera
            [mail:Equipment:private] => mail
            [gps:Equipment:private] => gps
            [international:Equipment:private] => international
            [mp3:Equipment:private] => MP3
            [vcast:Equipment:private] => 
            [wifi:Equipment:private] => wifi
            [tscreen:Equipment:private] => 
            [appstore:Equipment:private] => appstore
            [fcamera:Equipment:private] => 
            [ptt:Equipment:private] => 
            [rugged:Equipment:private] => 
            [sbluetooth:Equipment:private] => 
            [qwerty:Equipment:private] => qwerty
            [nocolor:Equipment:private] => Curve 9360  
            [colors:Equipment:private] => Black Black
            [hasShort:Equipment:private] => 
            [hasReview:Equipment:private] => 
            [hasAlternative:Equipment:private] => 
            [hasAlternativeExpert:Equipment:private] => 
            [order:Equipment:private] => 
            [review:Equipment:private] => 
            [id] => 260
        )

顺便说一句...它以这种方式工作,但如果有人在不使用另一个变量的情况下找到解决方案,我会非常高兴,只是使用未设置的方法...

if(count($os2)!=0)
{
    $ph = array();
    foreach($phones as $phone)
    {
        foreach($os2 as $o)
        {
            if($phone->os == $o)
            {array_push($ph,$phone);}
        }
    }
    $phones = $ph;
}

有人看到我做错了什么吗? :) 感谢您的回答...

I have an array of operating systems ($os2) and i would like to remove all elements from array $phones , which dont have one of these operating systems. So i did this, but for some reason it's not working...

$os2 = array("BlackBerry", "Android");
if(count($os2)!=0)
{
    for($i = 0; $i<count($phones); $i++)
    {
        if(!in_array($phones[$i]->os, $os2))
        {
            unset($phones[$i]);
        }
    }
}

This is one element from the phones array:

[0] => Equipment Object
        (
            [carrierDescription:Equipment:private] => T-Mobile
            [carrierId:Equipment:private] => 59
            [manufacturerID:Equipment:private] => 13
            [manufacturerName:Equipment:private] => BlackBerry
            [manufacturerCode:Equipment:private] => Curve 9360  
            [productId:Equipment:private] => 60558
            [currentEquipmentID:Equipment:private] => 29305
            [equipmentName:Equipment:private] => BlackBerry Curve 9360 Black
            [equipmentType:Equipment:private] => Smartphone
            [equipmentShortDescription:Equipment:private] =>  
            [equipmentLongDescription:Equipment:private] => The BlackBerry Curve 9360 for T-Mobile is the next Curve in BlackBerry's line of smartphones. Running the latest BlackBerry 7 operating system and utilizing the powerful BlackBerry 7 Webkit-browser, the BlackBerry Curve 9360 provides an intuitive smartphone experience, perfect for those who depend on their smartphone to keep their personal and work lives organized. Encased in a sleek package, including a wide 2.4 inch screen, full QWERTY keyboard and navigation track-pad, the BlackBerry Curve 9360 is ideal for efficient mobile communication. This new smartphone is also equipped with essential smartphone features such as a 5 megapixel camera with flash, video recorder, and WiFi connectivity for voice and data for a premium smartphone experience, The new Curve 9360 delivers the world's best mobile communications and social experiences with the latest release of BlackBerry Messenger, or BBM. 
            [sugRetailPrice:Equipment:private] => 400
            [displayPrice:Equipment:private] => $999.99
            [equipmentPrice:Equipment:private] => 0
            [numericCost:Equipment:private] => 0
            [rebateAmount:Equipment:private] => $0.00
            [priceAfterRebate:Equipment:private] => Free
            [rebateDescription:Equipment:private] => Free Gift
            [equipmentImageFileSmall:Equipment:private] => images/phones/60558_s.jpg
            [equipmentImageFileMedium:Equipment:private] => images/phones/60558_m.jpg
            [equipmentImageFileLarge:Equipment:private] => images/phones/60558_l.jpg
            [manufacturerLogo:Equipment:private] => _images/logos/manufacturers/selected_manu_blackberry.gif
            [purchaseMode:Equipment:private] => Add a Line
            [productLink:Equipment:private] => r.aspx?p=BlackBerry%20Curve%209360%20Black&c=T-Mobile&r=wowphonefinder&mode=1&s=1
            [upc:Equipment:private] => 610214626622
            [compatibleAccessoriesPageURL:Equipment:private] => DeviceSpecificAccessories.aspx?referringdomain=wowphonefinder&refcode1=standarddatafeed&refcode2=&productid=60558
            [mods:Equipment:private] => 
            [os:Equipment:private] => BlackBerry
            [tethering:Equipment:private] => 
            [hotspot:Equipment:private] => 
            [html:Equipment:private] => HTML
            [triG:Equipment:private] => 3G
            [fourG:Equipment:private] => 
            [fiveMP:Equipment:private] => 
            [bluetooth:Equipment:private] => bluetooth
            [camera:Equipment:private] => camera
            [mail:Equipment:private] => mail
            [gps:Equipment:private] => gps
            [international:Equipment:private] => international
            [mp3:Equipment:private] => MP3
            [vcast:Equipment:private] => 
            [wifi:Equipment:private] => wifi
            [tscreen:Equipment:private] => 
            [appstore:Equipment:private] => appstore
            [fcamera:Equipment:private] => 
            [ptt:Equipment:private] => 
            [rugged:Equipment:private] => 
            [sbluetooth:Equipment:private] => 
            [qwerty:Equipment:private] => qwerty
            [nocolor:Equipment:private] => Curve 9360  
            [colors:Equipment:private] => Black Black
            [hasShort:Equipment:private] => 
            [hasReview:Equipment:private] => 
            [hasAlternative:Equipment:private] => 
            [hasAlternativeExpert:Equipment:private] => 
            [order:Equipment:private] => 
            [review:Equipment:private] => 
            [id] => 260
        )

Btw...its working this way, but i will be really happy if someone have the solution without using the another variable, just with unset method...

if(count($os2)!=0)
{
    $ph = array();
    foreach($phones as $phone)
    {
        foreach($os2 as $o)
        {
            if($phone->os == $o)
            {array_push($ph,$phone);}
        }
    }
    $phones = $ph;
}

Does anybody see what am I doing wrong? :)
Thanks for your answers...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-12-27 02:33:00

这个:$os2 = ("BlackBerry", "Android"); 不是一个数组。

我什至不确定它是什么。如果您打开了 PHP,PHP 可能会向您抛出错误和警告。

php中的数组是这样形成的:

$os2 = array("BlackBerry", "Android");

This: $os2 = ("BlackBerry", "Android"); is NOT an array.

I am not even sure what it is. PHP will probably throw errors and warnings at you about it if you had them on.

An array in php is formed like this:

$os2 = array("BlackBerry", "Android");
愁杀 2024-12-27 02:33:00
$os2 = ("BlackBerry", "Android");

是一个语法错误。它应该是:

$os2 = array("BlackBerry", "Android");

按其值取消设置特定数组元素:

if (($key = array_search('Android', $os2, TRUE)) !== FALSE) {
    unset($os2[$key]);
}
$os2 = ("BlackBerry", "Android");

is a syntax error. It should be:

$os2 = array("BlackBerry", "Android");

To unset a particular array element by its value:

if (($key = array_search('Android', $os2, TRUE)) !== FALSE) {
    unset($os2[$key]);
}
拧巴小姐 2024-12-27 02:33:00

如果您查看以下行:

[os:Equipment:private] => BlackBerry

您会发现 os 是一个私有变量,因此您无法通过使用 $object->os 来获取它,您将需要使用它的吸气剂。

If you look at the line:

[os:Equipment:private] => BlackBerry

You see that os is a private variable, so you can't get it by using $object->os, you will need to use it's getter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文