以下是我的$newArr0
,它是一个对象数组。
Array
(
[0] => stdClass Object
(
[created_at] => Mon Dec 08 03:04:47 +0000 2014
[text] => How a individual man can adopt a village in andhra real guds needed hats off to you MASTER BLASTER #Sachin
[source] => Twitter Web Client
)
[1] => stdClass Object
(
[created_at] => Sun Dec 07 17:23:25 +0000 2014
[text] => #two #cool #peoples ..#Coolfieee ..#me nd #Sachin http://t.co/JU971nWAPo
[source] => Instagram
[2] => stdClass Object
(
[created_at] => Sun Dec 07 15:18:22 +0000 2014
[text] => Snga hit 90 odi international fifty...can he hit 6 more to break sachin s fifty record ..#sachin 96 odi fifty ...
kya lgta h tod dega record
[source] => Twitter Web Client
)
[3] => stdClass Object
[created_at] => Sun Dec 07 14:50:53 +0000 2014
[text] => @jeeturaaj Jeeturaaj want @sachin chi book pahje, please give #Sachin #playingitmyway ......:)
[source] => Twitter Web Client
)
[4] => stdClass Object
[created_at] => Sun Dec 07 14:33:49 +0000 2014
[text] => RT @UthMag: Old... http://t.co/b1HMzE3TZI #BCCI #cricket #featured #global #ICC #india #littlemaster #News #ODI #retire #sachin #sports #te…
[source] => Twitter for Android
)
)
我正在尝试从数组中删除一个带有source
名称的密钥,所以我尝试了这段代码 -
foreach ($newArr0 as $nkey1 => $nval1) {
if($nkey1 == "source") {
unset($newArr0["source"]);
}
}
但是 从$newArr0
移除密钥是让我知道我在这里做错了什么。
答案 0 :(得分:1)
<?php
$newArr = array();
foreach ($newArr0 as $key => $value) {
unset($value->source);
$newArr[$key] = $value;
}
$newArr0 = $newArr;
答案 1 :(得分:1)
首先,它是数组中对象的集合,因此您可以在循环内的每个对象上使用->
箭头运算符。然后,或者,您可以引用foreach中对象的每个副本并使其未设置。
foreach($newArr0 as &$nval1) {
// for each object inside `$newArr0` is in `$nval1`
unset($nval1->source);
// unset $nval1's source
}
答案 2 :(得分:-1)
下面:
foreach ($array as $key => & $object) {
if ($object->source === 'source') {
unset($array[$key]);
}
}