我有一个数组,看起来像这样50项:如何从数组中提取每个项目? laravel/PHP
array:50 [▼
0 => {#253 ▼
+"message": "message_1"
+"created_time": {#254 ▼
+"date": "2016-03-03 07:54:05.000000"
+"timezone_type": 1
+"timezone": "+00:00"
}
+"id": "167633226631991_1051771021551536"
}
1 => {#255 ▼
+"message": "message_2"
+"created_time": {#256 ▼
+"date": "2016-03-02 13:35:26.000000"
+"timezone_type": 1
+"timezone": "+00:00"
}
+"id": "167633226631991_1051313571597281"
}
]
我需要抓住阵列中的每个变量。不过,我不断收到读取错误:
Undefined property: stdClass::$message
林不知道我在想念着我试着这样做:
foreach ($posts as $post) {
$test = array('message' => $post->message);
}
也
$a = -1;
$alpha = 0;
$omega = count($posts);
$empty_array = array();
foreach (range($alpha,$omega) as $i) {
++$a;
$test = $posts[$a]->message;
array_push($empty_array, $test);
}
,但我得到的同样的错误。我使用laravel 5.2。
更新: 该数组来自Facebook的图形API。我转换使用JSON格式:
$posts = json_decode($userNode['posts']);
这样做然后显示array:50
如上。
尝试写$ post ['message'] –
'dd($ post)'在foreach中显示的是什么? – Joseph
@RaviHirani我试过$ post ['message']我得到这个错误'不能使用类型为stdClass的对象作为数组' – kevinabraham