kwk.stack
kwk.stack

Reputation: 553

loop through multidimensional array in php

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.

Upvotes: 2

Views: 95

Answers (1)

Sumurai8
Sumurai8

Reputation: 20745

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 );

Upvotes: 1

Related Questions