2

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.

12
  • 3
    Are you trying to make JSON? Commented Sep 29, 2013 at 14:38
  • yes exactely i am making jason Commented Sep 29, 2013 at 14:39
  • 2
    Use json_encode() for that instead of trying to echo the correct characters. Could you add the structure of the desired json to the question? Commented Sep 29, 2013 at 14:45
  • end() will give you the last item in your array, not the item you just matched. Also, don't build JSON manually. Use json_encode() Commented Sep 29, 2013 at 14:47
  • i use end() just to get the last row id for the condition in the end Commented Sep 29, 2013 at 15:00

1 Answer 1

1

Assuming $get_mails contains the Array you posted above (including company_name), you can write something like this:

$output = Array( "Messages" => Array() );
foreach( $get_mails as $k => $arr ) {
  $t = Array();
  $t['CompanyName'] = $arr['company_name'];
  $t['MessageID'] = $arr['id'];
  $t['MessageBody'] = $arr['message'];

  $output['Messages'][] = $t;
}

echo json_encode( $output );

First you prepare an Array with the structure of your JSON. The syntax $array[] = a will append a to $array. json_encode( ... ) at the end will take care of turning it into valid JSON, even if one of your keys included a quote or other special character that is invalid in JSON.

I believe you only want to display messages from a certain user, and try to accomplish that with if($get_mails[0]['userid'] == $_GET['userid']). I recommend to change your SQL-query to something that accomplishes that, because the performance of your page will greatly increase if you try to crawl through all messages with the following code:

$output = Array( "Messages" => Array() );
foreach( $get_mails as $k => $arr ) {
  if( $arr['user_id'] == $_GET['userid'] ) {
    $t = Array();
    $t['CompanyName'] = $arr['company_name'];
    $t['MessageID'] = $arr['id'];
    $t['MessageBody'] = $arr['message'];

    $output['Messages'][] = $t;
  }
}

echo json_encode( $output );
Sign up to request clarification or add additional context in comments.

2 Comments

i solved my issue there was no error in my code except for the }. by the way thanks every one for their kind and nice and on time suggestions. Thaks overstack and also all you guys.
here is my correct code if($get_mails) { $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'},'; } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.