I have a rather complex situation that I need to get working, for some reason I am almost there but still not quite.
In PHP I create arrays and send to it javascript through json, I am quite sure that my problem is in this part of the code.
$insertIDs = array();
foreach($recipients as $userID)
{
//MySQL insert removed from question
$insertID = mysql_insert_id();
array_push($insertIDs, $array[$userID] = $insertID);
}
$json = array();
$json['fromID'] = $session;
$json['insertIDs'] = $insertIDs;
$json['recipients'] = $recipients;
echo json_encode($json);
when I console.log the results of this in javascript, I get:
{ messageID: 40,
fromID: '1',
insertIDs: [ 44 ],
recipients: [ '3' ] }
Now in javascript when I try to access the insertIDs by the userID which I set it to be in the php above, I just get undefined as a result. How can I do this so I can access each insertID by the userID?
for example: json.insertIDs[userID]
if I simply call it by: json.insertIDs[0] it does return the first insertID, however when there are multiple I need to be able to have an easy way to access each usersID insertID.
I’m not quite sure what you are trying to do, since in the forloop $insertID is always the same value but:
It seems like json.InsertIDs is an array of the form json.InsertIds = Array(44);
Where as you want it to be Array(3 => 44)
try changing your forloop from
to