some time small things in programming get giants. I am working on 2 dimensional array but I am unable to get what I need.
below is my array structure.
Array
(
[0] => Array
(
[0] => 16
[id] => 16
[1] => 1
[userid] => 1
[2] => [email protected]
[email] => [email protected]
[3] => dffsdf
[message] => dffsdf
[4] => 0
[status] => 0
)
[1] => Array
(
[0] => 17
[id] => 17
[1] => 1
[userid] => 1
[2] => [email protected]
[email] => [email protected]
[3] => dffsdfnnnnnnnnnnn
[message] => dffsdfnnnnnnnnnnn
[4] => 0
[status] => 0
)
)
what I am doing here is getting the messages for a user with some id. I am doing it like that
if($get_mails[0]['userid'] == $_GET['userid'])
{
$last_key = end(array_keys($get_mails));
echo '{"Messages":[';
foreach($get_mails as $key => $get_each_mail){
$company_name = $get_each_mail['company_name'];
$email_id = $get_each_mail['id'];
$email_body = $get_each_mail['message'];
}
echo '{"CompanyName":"'.$company_name.'","MessageID":"'.$email_id.'","MessageBody":"'.$email_body.'"';
if ($key == $last_key)
{
echo '}]}';
}else{
echo'},';
}
}
what I am unable to do is so funny that I need a loop for [0] in this line of code
if($get_mails[0]['userid'] == $_GET['userid'])
like
if($get_mails[i]['userid'] == $_GET['userid']) and it give me all the records against specific user.
here is what I want to get for a specific user
{"Messages":[{"CompanyName":"newtech","MessageID":"14","MessageBody":"hi how are you"},{"CompanyName":"newtech","MessageID":"15","MessageBody":"hi how are you"},{"CompanyName":"newtech","MessageID":"24","MessageBody":"asfasdfsdfsdfsdfsdfsdfsdfsd"}]}
respose like that, it will add more and more if more records would available against specific user.
json_encode()for that instead of trying to echo the correct characters. Could you add the structure of the desired json to the question?end()will give you the last item in your array, not the item you just matched. Also, don't build JSON manually. Usejson_encode()