2016-06-03 6 views
-1

나는 아래 json을 데이터베이스 테이블에서 값을 얻도록 구성했다. 이제 json 아래에서 PHP의 un-ordered list로 인쇄하고 싶습니다. 어떻게이 목록을 json 아래에 인쇄 할 수 있습니까?이 json 문자열을 인쇄하려면 어떻게해야합니까?

내 배열 및 예상 출력입니다.

{ 
    "id": "6", 
    "name": "aaaaa", 
    "actove_from": "2000", 
    "firstlevelrelations": { 
     "1": { 
      "name": "bbbbb", 
      "active_from": "1990" 

     }, 
     "5": { 
      "name": "cccccc", 
      "active_from": "2003" 

     }, 
     "4": { 
      "name": "dddddd", 
      "active_from": "1992", 

      "2": { 
       "name": "fffff", 
       "active_from": "1990" 

          } 

     }, 
     "7": { 
      "name": "ggggg", 
      "active_from": "2015" 

     }, 
     "9": { 
      "name": "hhhhhh", 
      "active_from": "1990", 

      "10": { 

       "name": "yyyyy", 
       "active_from": "1997" 

      } 
     } 

    } 
} 

ouptut : 당신 인덱스 이름 여기

foreach($jsonvar as $key=>$value){ 
    //if it is just a simple value 
    $valuesInsideYourJson = $value; 

    //if it is an array of other objects. 
    if(is_array($valuesInsideYourJson)){ 
     foreach($valuesInsideYourJson as $val){ 
      // this in inside the firstlevelrelation 
      // do whatever you want 
     } 
    } 
} 

당신이 원시 데이터에 대한 액세스를 변경할 수있는 경우

<ul> 
    <li>aaaaaa</li> 
    <li>2000</li> 
    <ul> 
     <li>bbbbbb</li> 
     <li>1990</li> 
     <li>ccccc</li> 
     <li>2003</li> 
     <li>dddddd</li> 
     <li>1992 
      <ul> 
       <li>fffff</li> 
       <li>1990</li>    
      </ul> 
     </li> 
     <li>gggggg</li> 
     <li>2015</li> 
     <li>hhhhhh</li> 
     <li>1990 
      <ul> 
       <li>yyyyyy</li> 
       <li>1997</li>    
      </ul> 
     </li> 
    </ul> 
</ul> 
+0

는 [json_decode (http://php.net/manual/en/function.json-decode.php)가 얻어 JSON 인코딩 된 문자열을 PHP 연관 배열로 변환합니다. 이제 배열을 반복하고 HTML을 출력하십시오. – bloodyKnuckles

+0

배열을 반복 할 때 하나씩 인쇄하는 대신 단일 루프 실행에서 두 개의 목록 (li)을 인쇄하려고합니다. – user3452547

답변

-1
$jsonvar = json_decode('{"id":"6","name":"aaaaa","actove_from":"2000","firstlevelrelations":{"1":{"name":"bbbbb","active_from":"1990"},"5":{"name":"cccccc","active_from":"2003"},"4":{"name":"dddddd","active_from":"1992","2":{"name":"fffff","active_from":"1990"}},"7":{"name":"ggggg","active_from":"2015"},"9":{"name":"hhhhhh","active_from":"1990","10":{"name":"yyyyy","active_from":"1997"}}}}', true); // now you can access like an array. 

$id = $jsonvar['id']; 
// so on and so forth 

//to access the firstlevelrelation 
for($i=0;$i<$jsonvar['firstlevelrelation'];$i++){ 
    echo $jsonvar['firstlevelrelation'][$i]; // here you will be accessing each element inside the firstLevelRelation field. 
    // so for example, you wanna acess the name from each element. 
    $nameInsideFirstLevelRelation = $jsonvar['firstlevelrelation'][$i]['name']; 

} 

가 아니면, 이제 DOM간에 인쇄 요소 태그.

+0

절대 바뀌지 않을 것이라는 것을 모르는 경우 고정 배열 인덱스 이름에 의지하는 것이 위험한 것 같습니다. 그의 문제에서 – Wes

+0

, 항상 동일한 색인을 갖고있는 것 같습니다. –

0

나는이 모든 것을 당신을 위해 풀지는 않았지만, 당신이 가고 싶은 곳으로 가야합니다.

$array = json_decode($json, true); 
foreach($array as $key => $value) 
{ 
    if(is_array($value)) 
    { 
     //start building your sublist 
    }else{ 
     //start building your list 
    } 

} 
2
// decode the string as assoc array 
$json = json_decode($your_json_string_here, true); 

function menu($arr) { 
    $result = ""; 
    foreach($arr as $key => $item) { 
     // if the item is array calls the menu function 
     $result .= "<li>" . (is_array($item) ? menu($item) : $item)."</li>"; 
    } 
    return "<ul>" . $result . "</ul>"; 
} 

echo menu($json); 

이 원하는 출력을 생성한다 (단, 압입없이)

관련 문제