2017-10-15 16 views
0

私はすべてのメッセージを取得し、配列にそれをすべて入れたいが、私は、たとえば、ソートすべてのIDによるメッセージを望む別の方法があります: 私のJSON:配列からのすべてのメッセージを取得する - PHP

{"msg_c":[{"message":"Hi","sender":"2","receve":"3","name":"Muhamad Bagzada"},{"message":"Hello","sender":"3","receve":"2","name":"Danyal Join"},{"message":"What's up , How are you ?","sender":"2","receve":"3","name":"Muhamad Bagzada"},{"message":"fine and you ?","sender":"3","receve":"2","name":"Danyal Join"},{"message":"fine","sender":"2","receve":"3","name":"Muhamad Bagzada"}]} 

は、まさに私が欲しい:別の値によって、アレイ内の

ショーmessgaes、(message_u:私のMSG、messages_m:彼のMSG)

{ 
    "msg_c": 
    [ 
     {"message_u":"Hi","sender":"2","receve":"3","name":"Muhamad Bagzada"}, 
     {"message_m":"Hello","sender":"3","receve":"2","name":"Danyal Join"}, 
     {"message_u":"What's up , How are you ?","sender":"2","receve":"3","name":"Muhamad Bagzada"}, 
     {"message_m":"fine and you ?","sender":"3","receve":"2","name":"Danyal Join"}, 
     {"message_m":"fine","sender":"2","receve":"3","name":"Muhamad Bagzada"} 
    ] 
} 

をPHPの機能:

public function MessageApp ($user_id,$sender){ 
    $data =''; 
    $Messg = mysqli_query($this->_join, "SELECT * FROM `messages` WHERE (`from` = '$sender' AND `to` = '$user_id') OR (`from` = '$user_id' AND `to` = '$sender') ORDER BY dates ASC LIMIT 12 "); 
    while ($m = mysqli_fetch_assoc($Messg)) { 

    $from = $m['from']; 
    $to = $m['to']; 
    $chat = $m['chat']; 
    $dates = $m['dates']; 
    $id = $m['id']; 
    if ($user_id === $sender) { 
    $select_id = $to; 
    }else{ 
    $select_id = $from; 
    } 
    $user = new user($from); 
    $fullname = ($user->data()->fname)." ".($user->data()->lname); 
    $gettype['msg_c'][] = array(
     'message' => $chat, 
     'sender' => $from, 
     'receve' => $to, 
     'name'=>$fullname 
    ); 

    $data = json_encode($gettype); 

    } 

    return json_encode($gettype); 
} 

私はここにJSONデータと呼ばれる:

<?php 

include $_SERVER['DOCUMENT_ROOT']."/php/core/init.php"; 
include $_SERVER['DOCUMENT_ROOT']."/php/core/connect.php"; 
$my_id = input::get('user_id'); 
$user = new user($my_id); 
$sender = $user->data()->id; 
$db = db::getInstance(); 

$message = new message(); 

$id = input::get("id"); 
$user = new user($id); 
$user_id = $user->data()->id; 

echo $message->MessageApp($user_id,$sender); 

?> 

答えて

0

をあなたはmsg_c内の行を構築する場合、あなたはこの

<?php 
$msg_row = array(
    'sender' => $from, 
    'receve' => $to, 
    'name'=> $fullname 
); 
if ($user_id === $sender) { 
    $msg_row['message_m'] = $chat; 
} else { 
    $msg_row['message_u'] = $chat; 
} 
$gettype['msg_c'][] = $msg_row; 

などさまざまな状況で、セットキーを分離することができますコードを再利用する前に、たとえelse文で配列を作成

+0

そこに多くの方法があるが、それは、AndroidのためですプロジェクトとJSONオブジェクトを、私が使用している場合(else文の場合)の解析、JSONは、配列PHPから一つの値を受け入れます –

関連する問題