I have an array that is a json_decode of some data retrieved from the twitter API.
Here is the section of the array than I'm using:
Array(
[0]=>stdClass Object (
[user] => stdClass Object (
[screen_name] => User1
)
[text] => Text of message
[id] => 220301332135952385
[created_at] => Tue Jul 03 23:41:45 +0000 2012
)
[1]=>stdClass Object (
[user] => stdClass Object (
[screen_name] => User2
)
[text] => Text of message
[id] => 220301332135952385
[created_at] => Tue Jul 03 23:41:45 +0000 2012
)
)
The problem I'm running into is that my PHP is only getting the values User1
and Text of message
from it. My actual array is much longer, but it follows the above pattern.
Here is my PHP:
foreach($tweet as $num => $val) {
$username = $tweet[$num]->user->screen_name;
echo $num . "-USER-" . $username . "-NAME ";
$tweet = $tweet[$num]->text;
$id = $tweet[$num]->id_str;
$year = substr($tweet[$num]->created_at, -4);
$statement = $connect->prepare("INSERT INTO table (username, tweet, id, year) VALUES ('$username', '$tweet', '$id', '$year')");
$statement->execute();
}
$tweet
is my array. The echo
in there is something I was using for debugging. It outputs:
1-USER-User1-NAME 2-USER--NAME 3-USER--NAME
I'm expecting:
1-USER-User1-NAME 2-USER-User2-NAME
Lastly, my database only receives User1
in the username column and Text of message
in the tweet column. Nothing gets inserted into id or year. Although, it does create empty rows for each tweet after the first. Not sure what I'm doing wrong. Any help would be greatly appreciated. Thanks!