The function I have is below and the output of it.
$results = mysql_query("SELECT * FROM tmsessions WHERE session_user_id ='".$_SESSION['user_id']."'");
$count = mysql_num_rows($results);
$username = mysql_fetch_assoc(mysql_query("SELECT user_fname FROM tmusers WHERE user_id = '".$_SESSION['user_id']."'"));
$json_data = array( 'userID'=>$_SESSION['user_id'], 'userName'=>$username['user_fname'], 'total'=>$count );
while($session = mysql_fetch_assoc($results))
{
$numListItems = mysql_num_rows(mysql_query("SELECT session_id, listing_unique_id FROM tmdata WHERE session_id = '".$session['session_id']."'"));
$listItems = array('sessionID'=>$session['session_id'],
'sessionName'=>$session['session_name'],
'sessionCount'=>$numListItems,
'sessionDC'=>date('h:iA - M m y',strtotime($session['session_date_created'])),
'sessionDM'=>date('h:iA - M m y',strtotime($session['session_date_modified'])),
'sessionActive'=>$session['session_active']);
array_push($json_data, $listItems);
}
return json_encode($json_data);
Which outouts:
{"0":
{"sessionID":"9",
"sessionName":"dataName0",
"sessionCount":100,
"sessionDC":"12:11AM - Jun 06 11",
"sessionDM":"01:00AM - Jan 01 70",
"sessionActive":"1"},
"1":
{"sessionID":"10",
"sessionName":"dataName1",
"sessionCount":100,
"sessionDC":"05:04PM - Jun 06 11",
"sessionDM":"01:00AM - Jan 01 70",
"sessionActive":"1"},
"userID":"1",
"userName":"Ezra",
"total":2}
I need the “0”: and “1”: bits to become “listItem”: of each new associative array, but I don’t know howto manipulate the arrays correctly to get that :(.
In PHP, indexed and associative arrays are handled as the same type (i.e. matrices or one-dimensional arrays, a[0], a[1], … a[N], vs a[‘one’], a[‘two’], … a[‘something’]).
When using array_push() you are not creating or adding to an associative hash, but an indexed value to the end of the array.
Instead of using the array_push() function, you could do a direct assignment with a name of your choice, e.g.