2012-10-23 5 views
0

이 질문을 먼저 게시했지만 모든 사람이 더 쉽게 사용할 수 있도록하기 위해 몇 가지 사항을 명확히해야했습니다.PHP로 JSON 표시

<?php 
    $array[0]=array("request"=>array("act"=>'user_create', 
            "user_email"=>"[email protected]", 
            "user_zone"=>"example.com")); 
    $array[1]=array('result' => 'success'); 
    $array[2]=array('msg' => 'A message that will be displayed to the end user'); 
    echo json_encode($array); 
?> 

그 코드 내가 가진 : 여기

{ 
    "request": { 
    "act": "user_create", 
    "user_email": "[email protected]", 
    "user_zone": "example.com" 
    }, 
    "result": "success", 
    "msg": "A message that will be displayed to the end user" 
    ... 
} 

내가 현재 사용하고 코드입니다 ... 내가 JSON으로 출력이 노력하고있어 및 내가 뭘 아무것도 작동하는 것 같다 이 출력은 가깝지 만 꽤 좋지는 않지만 유효한 json 출력이 아닙니다. 여기 변경 만 할 것 확실하지 내가 현재 받고 있어요 무엇 : 사람이 좋은 것 내 고정 된 PHP 코드의 예를 게시하시기 바랍니다 수

[{"request":{"act":"user_create","user_email":"[email protected]","user_zone":"example.com"}},{"result":"success"},{"msg":"A message that will be displayed to the end user"}] 

합니다. 나는 지난 몇 시간 동안 서로 다른 솔루션을 테스트하면서 앞뒤로 튀어 오르고 있었고, 내가 원하는 것을 정확히 표시하지 못하는 것 같습니다 :/미리 감사드립니다!

+1

왜 당신이 잘못된 JSON 출력 생각하십니까?를 참조하십시오 []는 배열 구문이고 {}는 객체 구문입니다. – steffen

답변

2

그와 시도 ..

$array = array(
    "request" => array(
     "act"=>"user_create", 
     "user_email"=>"[email protected]", 
     "user_zone"=>"example.com" 
     ), 
    "result"=>"success", 
    "msg"=>"A message that will be displayed to the end user" 
    ); 
    echo json_encode($array); 
+0

완벽하게 작동했습니다! 정말 고맙습니다! –

+0

@ user1698602 허용되는 답변을 선택하는 것을 잊지 마십시오. – Stefan

0
<?php 
    $array["request"]=array("act"=>'user_create', 
            "user_email"=>"[email protected]", 
            "user_zone"=>"example.com"); 
    $array["result"] = 'success'; 
    $array["msg"] = 'A message that will be displayed to the end user'; 
    echo json_encode($array); 
?> 

이 당신을 위해 작동합니다.

0
$array = array(
    "request" => array(
    "act"  => "user_create", 
    "user_email" => "[email protected]", 
    "user_zone" => "example.com" 
), 
    "result" => "success", 
    "msg" => "A message that will be displayed to the end user" 
    ... 
); 

그냥 모든 {}=>: 모든 array()에 등을 변경하고 당신은 PHP 구문에서 같은 일을 얻는다.

0
<?php 
    $array[0]["request"] = array(
     "act"  => 'user_create', 
     "user_email" => "[email protected]", 
     "user_zone" => "example.com") 
    ); 
    $array[0]['result'] = 'success'; 
    $array[0]['msg'] = 'A message that will be displayed to the end user'; 

    ... etc. for other requests and results and msgs 

    echo json_encode($array); 
?> 
0

JSON 형식으로 배열을 시작할 때마다 PHP에서 배열을 만듭니다.

$array = array(// "request" array 
    "request" => array(// inner array 
     "act"=>"user_create", 
     "user_email"=>"[email protected]", 
     "user_zone"=>"example.com" 
     ), 
    "result"=>"success", 
    "msg"=>"A message that will be displayed to the end user" 
    ); 
    echo json_encode($array); 
0
$array = array(
"key1" => array(
    "innerkey1"=>"innerkeyvalue1", 
    "innerkey2"=>"innerkeyvalue2", 
    "innerkey3"=>"innerkeyvalue3" 
    ), 
"key2"=>"value2", 
"key3"=>"value3" 
); 
echo json_encode($array); 

here jsone_encode