我有一系列操作系统($ 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;
}
有人看到我做错了什么吗? :) 谢谢你的回答...
答案 0 :(得分:2)
这:$os2 = ("BlackBerry", "Android");
不数组。
我甚至不确定它是什么。如果您使用PHP,PHP可能会向您发出错误和警告。
php中的数组形成如下:
$os2 = array("BlackBerry", "Android");
答案 1 :(得分:0)
$os2 = ("BlackBerry", "Android");
是语法错误。它应该是:
$os2 = array("BlackBerry", "Android");
通过其值取消设置特定数组元素:
if (($key = array_search('Android', $os2, TRUE)) !== FALSE) {
unset($os2[$key]);
}
答案 2 :(得分:0)
如果你看一下这条线:
[os:Equipment:private] => BlackBerry
您发现os
是私有变量,因此您无法使用$object->os
获取它,您需要使用它的getter。