จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

Re: จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

โดย ballthai2015 » 27/07/2015 4:06 pm

รอคำตอบเพิ่มด้วยคนค่ะ เพราะ array ถือว่าสำคัญมากๆเลย
ขอบคุณด้วยนะคะ :wink:

รูปภาพ

Re: จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

โดย sunzandesign » 26/07/2015 11:42 am

เจ้าของกระทู้ลองใส่แท็ก <pre></pre> คร่อมตอน print_r() จะช่วยให้เห็นภาพชัดขึ้นนะครับ

เช่น

echo '<pre>', print_r($array,true),'</pre>';

Re: จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

โดย mindphp » 26/07/2015 5:48 am

แทน

โค้ด: เลือกทั้งหมด

$level[] = $value->level;  
ด้วย

โค้ด: เลือกทั้งหมด

if(!in_array($value->level, $level)) {
 $level[] = $value->level;
} 
แล้ว ลอง print_r กับ array_unique ไปอยู่ นอก loop
print_r($level_menu);

Re: จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

โดย thatsawan » 26/07/2015 12:53 am

file_get_contents มาจาก json

โค้ด: เลือกทั้งหมด

       if($this->config['m_main_menu_enable']){
                                $gat_path_url_json = file_get_contents($this->config['m_main_menu_path']);
                                $data  = json_decode($gat_path_url_json);
                                $level  = array();
                          
                                foreach ($data as $key => $value) {
                           
                                        $level[] = $value->level;
                                        $level_menu = array_unique($level); //  หา level ของ menu ว่ามี level อะไรบ้าง 
                                        print_r($level_menu);
                                    
                                }  
                        }

Re: จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

โดย mindphp » 26/07/2015 12:41 am

ตัวแปล สร้างมา ยังไง ครับ
ลอง เอาตัวแปลมาดูหน่อย

จะ loop เเล้วหาว่ามี level อะไรใน array บ้าง ทำยังไง

โดย thatsawan » 26/07/2015 12:17 am

Array
(
[0] => stdClass Object
(
[id] => 101
[menutype] => mainmenu
[title] => MindPHP
[alias] => home
[note] =>
[path] => home
[link] => index.php?option=com_content&view=featured
[type] => component
[published] => 1
[parent_id] => 1
[level] => 1
[component_id] => 22
[checked_out] => 0
[checked_out_time] => 0000-00-00 00:00:00
[browserNav] => 0
[access] => 1
[img] =>
[template_style_id] => 0
[params] => {"featured_categories":[""],"layout_type":"blog","num_leading_articles":"0","num_intro_articles":"0","num_columns":"1","num_links":"0","multi_column_order":"1","orderby_pri":"","orderby_sec":"front","order_date":"","show_pagination":"0","show_pagination_results":"0","show_title":"","link_titles":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_vote":"","show_readmore":"","show_readmore_title":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_hits":"","show_tags":"","show_noauth":"","show_feed_link":"1","feed_summary":"","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"","show_page_heading":"0","page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}
[lft] => 11
[rgt] => 12
[home] => 1
[language] => *
[client_id] => 0
)

[1] => stdClass Object
(
[id] => 102
[menutype] => mainmenu
[title] => บทเรียนออนไลน์
[alias] => 2015-07-14-09-34-08
[note] =>
[path] => 2015-07-14-09-34-08
[link] =>
[type] => url
[published] => 1
[parent_id] => 1
[level] => 1
[component_id] => 0
[checked_out] => 0
[checked_out_time] => 0000-00-00 00:00:00
[browserNav] => 0
[access] => 1
[img] =>
[template_style_id] => 0
[params] => {"menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1}
[lft] => 13
[rgt] => 46
[home] => 0
[language] => *
[client_id] => 0
)
)

โค้ด: เลือกทั้งหมด

        $level  = array();
                
                                foreach ($data as $key => $value) {

                                        $level[] = $value->level;
                                        $level_menu = array_unique($level); //  หา level ของ menu ว่ามี level อะไรบ้าง 
                                        print_r($level_menu);
              
                               
                                }
พอลองเเล้วมันได้เเบบนี้
Array
(
[0] => 1
)
Array
(
[0] => 1
)
Array
(
[0] => 1
[2] => 2
)
Array
(
[0] => 1
[2] => 2
)
Array
(
[0] => 1
[2] => 2
)
Array
(
[0] => 1
[2] => 2
)
Array
(
[0] => 1
[2] => 2
)

ข้างบน