2016-09-22 6 views
1

php配列でjson_encodeを使用しようとしています。返されたJSONを次のように構造化する必要があります。JSONエンコード専用の文字列ではなく、配列キー0

[ 
    {"text": "Title1"}, 
    {"text": "URL"} 
] 

私は以下のことを試しましたが、キーとして0が得られます。

$xml = simplexml_load_file($url); 

$title1 = $xml->results->result->jobtitle; 
$snippet1 = $xml->results->result->snippet; 
$url1 = $xml->results->result->url; 




$arrays = array('text'=>$title1); 
echo json_encode($arrays); 

エンコードされた配列で何が問題になっていますか?どうすれば0に戻らないようにすることができますか?

{"text":{"0":"CDL-A Dry Bulk Drivers Wanted - Regional - OH, WV, PA"}} 
+1

を返し

$array = [ ['text' => 'hello'], ['text' => 'hello again'], ]; $encoded = json_encode($array); print_r($encoded); 

あなたは変数$ URLの値を付けるだろうか? –

+0

XMLファイルの例を含めることができますか? –

答えて

1

json_encodeに間違いはありません。

$title1 = $xml->results->result->jobtitle; 

...

$arrays = array('text'=>$title1[0]); 
-1

彼らは道あなたの配列を設定しているが正しくありません。あなたがしたいことは何ですか。

[ 
    {"text":"hello"}, 
    {"text":"hello again"} 
] 
+0

私の間違い。その「jobtitle」が配列として返されていることに気づかなかった。 – SeaFuzz

関連する問題