Twiebie |
|
---|---|
I've got a quick PHP question, which I'm sure some of the developers here can easily answer. I've got the following array: Array ( [0] => stdClass Object ( [embed_count] => 3526 [stream_count] => 10216 [category] => gaming [channel_count] => 20331 [format] => live [featured] => 1 [site_count] => 6690 [abuse_reported] => [channel] => stdClass Object ( [timezone] => America/New_York [subcategory] => [category] => gaming [embed_enabled] => 1 ) [video_height] => 720 [language] => en [video_bitrate] => 2107.9375 [id] => 4426520352 [broadcaster] => delay [broadcast_part] => 3 [audio_codec] => uncompressed [up_time] => Mon Dec 24 09:46:39 2012 [video_width] => 1280 [geo] => US [channel_view_count] => 18592346 [channel_subscription] => 1 [embed_enabled] => 1 [stream_type] => live [video_codec] => AVC ) [1] => stdClass Object ( [embed_count] => 14 [stream_count] => 195 [category] => gaming [channel_count] => 312 [format] => live [featured] => 1 [site_count] => 181 [abuse_reported] => [channel] => stdClass Object ( [timezone] => US/Pacific [subcategory] => [category] => gaming [embed_enabled] => 1 ) [video_height] => 1080 [language] => en [video_bitrate] => 4398.9375 [id] => 4426659824 [broadcaster] => fme [broadcast_part] => 2 [audio_codec] => uncompressed [up_time] => Mon Dec 24 10:16:06 2012 [video_width] => 1920 [geo] => US [channel_view_count] => 5738728 [channel_subscription] => 1 [embed_enabled] => 1 [stream_type] => live [video_codec] => AVC ) ) Now I want to add user_id to it and foreach loop through the array afterwards.
How can I add a new value to the existing array? Array ( [0] => stdClass Object ( [user_id] => Twiebie [embed_count] => 3526 [stream_count] => 10216 etc... etc... ) [1] => stdClass Object ( [user_id] => AnotherUser [embed_count] => 14 [stream_count] => 195 etc... etc... ) ) If I do something like: // $result is the array as seen above here foreach ($userid as $value) { $result[] = $value; }
It adds it to the bottom of the array instead of to the stdClass Object. Thanks in advance. |
Trustmaster |
|
---|---|
The stdClass Object thingie tells you that the array is not an array of arrays but is rather an array of Objects. Another thing is that you might want to access items from both arrays by index, so you can use for loop instead of foreach. Putting it all together, here is how you can add user_id property to each object in the array using values from another array: for ($i = 0; $i < count($userid); $i++) { $videos[$i]->user_id = $userid[$i]; }
May the Source be with you!
|
Twiebie |
|
---|---|
Awesome, that is exactly what I need! Thanks a lot for the quick reply, Trustmaster! |