Reputation: 6574
I am getting the contents of mySQL data for messages, then I'm sorting through it per user to create a general array of messages.
IDEA Like So:
Array(
["Mr.EasyBB"] => Array(
[0] => array(//message data here)
[1] => array(//message data here)
...
);
);
PHP so far
$messages = $controller->loadResult(
"SELECT * FROM messages WHERE from_username='$username' AND from_uid='$uid'"
);
if($messages){
foreach($messages as $data){
$to_username = strtolower($data["to_username"]);
if(!in_array($to_username,$conversations)){
$conversations[$to_username] = array();
}
$conversations[$to_username][]= $data;
}
Though my output is
Array
(
[ianbruce] => Array
(
[0] => Array
(
[0] => 231
[to_uid] => 231
[1] => 0001
[from_uid] => 0001
[2] => IanBruce
[to_username] => IanBruce
[3] => Mr.EasyBB
[from_username] => Mr.EasyBB
[4] => n
[deleted] => n
[5] => n
[sent_deleted] => n
[6] => Howdy Again my Friend
[message] => Howdy Again my Friend
[7] => 2014-10-18 00:01:04
[to_timestamp] => 2014-10-18 00:01:04
[8] => 0000-00-00 00:00:00
[from_timestamp] => 0000-00-00 00:00:00
[9] => y
[from_seen] => y
[10] => n
[to_seen] => n
)
)
)
Now I know it's overwriting either the array inside the user or it's overwriting the user altogether making it show the last push of the array. Not sure what I did wrong here, and thought maybe a fresh pair of eyes will help.
Upvotes: 1
Views: 53
Reputation: 91762
Your problem is caused by the use of in_array()
. That function checks for a value to be set in an array and not a key.
You need to replace this:
if(!in_array($to_username,$conversations)){
with something like this:
if(!isset($conversations[$to_username])){
You can also use functions like key_exists()
, etc. but you probably don't need this condition at all as you can set $conversations[$to_username][]
even when $conversations[$to_username]
does not exist.
Upvotes: 4