2016-04-10 24 views
-1

で連想配列に私は配列を持っている:しますprint_rとき追加キー値PHP

$data["messages"][] = array("chatId" => $chat_id, "type" => "text", "to" => $username, "body" => "success"); 

配列は、それは次のようになります。

Array ([messages] => Array ([0] => Array ([chatId] => d3a611401de00e8c8b3e225a7cf95484033f57ea18c442249ee9b5bcb63c9a96 [type] => text [to] => pitashi [body] => success))) 

しかし、私は、新しいキー値を追加しようとすると、このようなエンド:

$arr_keyboards[] = array(
    "type" => "suggested", 
    "responses" => array(
    array("type" => "text", "body" => "Yes"), 
     array("type" => "text", "body" => "No") 
     ) 

); 

$data["messages"]["keyboards"] = $arr_keyboards; 

私はこれで終わる:

Array ([messages] => Array ([0] => Array ([chatId] => d3a611401de00e8c8b3e225a7cf95484033f57ea18c442249ee9b5bcb63c9a96 [type] => text [to] => pitashi [body] => success) [keyboards] => Array ([0] => Array ([type] => suggested [responses] => Array ([0] => Array ([type] => text [body] => Yes) [1] => Array ([type] => text [body] => No)))))) 

新しいキー値 "keyboards" => array(...は、前の配列が閉じられた後に追加されます。私はそれが見えるようにしたいです:

Array ([messages] => Array ([0] => Array ([chatId] => d3a611401de00e8c8b3e225a7cf95484033f57ea18c442249ee9b5bcb63c9a96 [type] => text [to] => pitashi [body] => success [keyboards] => Array ([0] => Array ([type] => suggested [responses] => Array ([0] => Array ([type] => text [body] => Yes) [1] => Array ([type] => text [body] => No))))) 

私はそれをエンコードしています。ここでは、それがどのように見えるのか、そして私がそれをhttps://www.evernote.com/l/AETNv5OhBI1HtLzqXalq-BCfOIwz4U0jlWwにする必要があるスクリーンショットを示します。そして、最終的に私が見たい方法のスクリーンショットです。https://www.evernote.com/l/AETRjOREKvBHLpXyh3NdZdGAryyxO2rIiwg

私はダムです。助けてくれてありがとう。

+0

:: '$ arr_keyboards [] = ...'あなたが実際にこれをしたい: '$ arr_keyboards = ...' – arkascha

答えて

1

$arr_keyboardsアレイを$data["messages"]アレイの最初の要素に追加しようとしています。

代わりにそのようなことの
$data["messages"][0]["keyboards"] = $arr_keyboards; 
+0

BAM!それがチケットです。ありがとう@romanperekhrest –

+0

@JeffSolomon、ようこそ – RomanPerekhrest

1

これを試してみてください:

$data["messages"][] = array(
    "chatId" => $chat_id, 
    "type"  => "text", 
    "to"  => $username, 
    "body"  => "success" 
); 

$arr_keyboards = array(
    "type" => "suggested", 
    "responses" => array(
     array("type" => "text", "body" => "Yes"), 
     array("type" => "text", "body" => "No") 
    ) 
); 

foreach ($data["messages"] as $message) { 

    $message["keyboards"] = $arr_keyboards; 
} 

echo json_encode($message); 

は、この情報がお役に立てば幸いです。

+0

ああ、次のようなコードの "決定的な" の行を変更し
。それは動作します。ちょうど追加するのではなく、ループバックしなければならないような過度のようだが、うまくいく。ありがとう@オブジェクトマニピュレータ –

関連する問題