|
|
|
จะแปลง nested array+object ให้เป็น ul li ยังไง? |
|
|
|
|
|
|
|
ผม query ข้อมูลออกมาจาก db จนได้รูปแบบ array อย่างนี้แล้ว แต่คิดไม่ออกจริงๆว่าจะเอามาทำเป็น ul li ได้ยังไงครับ
Code (PHP)
Array
(
[0] => stdClass Object
(
[tid] => 1
[parent_id] => 0
[language] => th
[t_type] => category
[t_name] => cat1
[t_description] =>
[t_uri] => cat1
[t_uri_encoded] => cat1
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
[childs] => Array
(
[0] => stdClass Object
(
[tid] => 3
[parent_id] => 1
[language] => th
[t_type] => category
[t_name] => cat1.1
[t_description] =>
[t_uri] => cat1.1
[t_uri_encoded] => cat1.1
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
[childs] => Array
(
[0] => stdClass Object
(
[tid] => 5
[parent_id] => 3
[language] => th
[t_type] => category
[t_name] => cat1.1.1
[t_description] =>
[t_uri] => cat1.1.1
[t_uri_encoded] => cat1.1.1
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
)
)
)
[1] => stdClass Object
(
[tid] => 4
[parent_id] => 1
[language] => th
[t_type] => category
[t_name] => cat1.2
[t_description] =>
[t_uri] => cat1.2
[t_uri_encoded] => cat1.2
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
)
)
)
[1] => stdClass Object
(
[tid] => 2
[parent_id] => 0
[language] => th
[t_type] => category
[t_name] => cat2
[t_description] =>
[t_uri] => cat2
[t_uri_encoded] => cat2
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
)
)
ต้องการให้ออกมาเป็น
Code (PHP)
<ul>
<li>cat1
<ul>
<li>cat1.1
<ul>
<li>cat1.1.1</li>
</ul>
</li>
<li>cat1.2</li>
</ul>
</li>
<li>cat2</li>
</ul>
array นี้ในแบบ json ครับ
Code (JavaScript)
[{"tid":"1","parent_id":"0","language":"th","t_type":"category","t_name":"cat1","t_description":null,"t_uri":"cat1","t_uri_encoded":"cat1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null,"childs":[{"tid":"3","parent_id":"1","language":"th","t_type":"category","t_name":"cat1.1","t_description":null,"t_uri":"cat1.1","t_uri_encoded":"cat1.1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null,"childs":[{"tid":"5","parent_id":"3","language":"th","t_type":"category","t_name":"cat1.1.1","t_description":null,"t_uri":"cat1.1.1","t_uri_encoded":"cat1.1.1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]},{"tid":"4","parent_id":"1","language":"th","t_type":"category","t_name":"cat1.2","t_description":null,"t_uri":"cat1.2","t_uri_encoded":"cat1.2","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]},{"tid":"2","parent_id":"0","language":"th","t_type":"category","t_name":"cat2","t_description":null,"t_uri":"cat2","t_uri_encoded":"cat2","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]
Tag : PHP
|
ประวัติการแก้ไข 2012-04-25 10:01:46 2012-04-25 10:37:17 2012-04-25 16:12:09
|
|
|
|
|
Date :
2012-04-25 10:01:08 |
By :
mr.v |
View :
1469 |
Reply :
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http://blog.cnizz.com/2008/04/08/convert-php-object-to-an-array/
http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html
|
|
|
|
|
Date :
2012-04-25 10:32:01 |
By :
sakuraei |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ต้องการแปลงเป็น ul li ครับ ไม่ใช่แปลง array <-> object เพราะผมแปลงมาแล้ว ตามที่คำถามถามอยู่ครับ
|
|
|
|
|
Date :
2012-04-25 10:36:12 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ออกมาเลยครับ
งั้นเอาอาเรย์ที่แปลงแล้ว (ที่ใช้งานกับ foreachได้จริง) มาลงซิครับ
|
|
|
|
|
Date :
2012-04-25 10:47:59 |
By :
sakuraei |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คงต้องใช้ recursive function ครับถ้า level ไม่ตายตัว แต่ถ้าตายตัว ใช้ loop เอาแบบง่ายๆ ก็ได้ครับ
|
|
|
|
|
Date :
2012-04-25 10:50:25 |
By :
ikikkok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอ array ตัวนี้มาลองหน่อยครับ เด๋วเบื่อๆ ว่างๆ ผมลองดู
|
|
|
|
|
Date :
2012-04-25 15:55:24 |
By :
ikikkok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อัพเดท array ในแบบ json_encode ให้แล้วครับ คุณ plakrim
|
|
|
|
|
Date :
2012-04-25 16:13:10 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอา serialize แทน json น่าจะดีกว่า
|
|
|
|
|
Date :
2012-04-25 16:17:51 |
By :
ikikkok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
serialize ครับ
Code (PHP)
a:2:{i:0;O:8:"stdClass":13:{s:3:"tid";s:1:"1";s:9:"parent_id";s:1:"0";s:8:"language";s:2:"th";s:6:"t_type";s:8:"category";s:6:"t_name";s:4:"cat1";s:13:"t_description";N;s:5:"t_uri";s:4:"cat1";s:13:"t_uri_encoded";s:4:"cat1";s:6:"t_uris";N;s:10:"meta_title";N;s:16:"meta_description";N;s:13:"meta_keywords";N;s:6:"childs";a:2:{i:0;O:8:"stdClass":13:{s:3:"tid";s:1:"3";s:9:"parent_id";s:1:"1";s:8:"language";s:2:"th";s:6:"t_type";s:8:"category";s:6:"t_name";s:6:"cat1.1";s:13:"t_description";N;s:5:"t_uri";s:6:"cat1.1";s:13:"t_uri_encoded";s:6:"cat1.1";s:6:"t_uris";N;s:10:"meta_title";N;s:16:"meta_description";N;s:13:"meta_keywords";N;s:6:"childs";a:1:{i:0;O:8:"stdClass":12:{s:3:"tid";s:1:"5";s:9:"parent_id";s:1:"3";s:8:"language";s:2:"th";s:6:"t_type";s:8:"category";s:6:"t_name";s:8:"cat1.1.1";s:13:"t_description";N;s:5:"t_uri";s:8:"cat1.1.1";s:13:"t_uri_encoded";s:8:"cat1.1.1";s:6:"t_uris";N;s:10:"meta_title";N;s:16:"meta_description";N;s:13:"meta_keywords";N;}}}i:1;O:8:"stdClass":12:{s:3:"tid";s:1:"4";s:9:"parent_id";s:1:"1";s:8:"language";s:2:"th";s:6:"t_type";s:8:"category";s:6:"t_name";s:6:"cat1.2";s:13:"t_description";N;s:5:"t_uri";s:6:"cat1.2";s:13:"t_uri_encoded";s:6:"cat1.2";s:6:"t_uris";N;s:10:"meta_title";N;s:16:"meta_description";N;s:13:"meta_keywords";N;}}}i:1;O:8:"stdClass":12:{s:3:"tid";s:1:"2";s:9:"parent_id";s:1:"0";s:8:"language";s:2:"th";s:6:"t_type";s:8:"category";s:6:"t_name";s:4:"cat2";s:13:"t_description";N;s:5:"t_uri";s:4:"cat2";s:13:"t_uri_encoded";s:4:"cat2";s:6:"t_uris";N;s:10:"meta_title";N;s:16:"meta_description";N;s:13:"meta_keywords";N;}}
|
|
|
|
|
Date :
2012-04-25 16:47:35 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ได้เล่นเลย ทำได้ซะละ
|
|
|
|
|
Date :
2012-04-26 01:41:58 |
By :
ikikkok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|