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

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

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
)
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ตัวแปล สร้างมา ยังไง ครับ
ลอง เอาตัวแปลมาดูหน่อย
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

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);
                                    
                                }  
                        }
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

แทน

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

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

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

if(!in_array($value->level, $level)) {
 $level[] = $value->level;
} 
แล้ว ลอง print_r กับ array_unique ไปอยู่ นอก loop
print_r($level_menu);
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
sunzandesign
PHP Newbie
PHP Newbie
โพสต์: 6
ลงทะเบียนเมื่อ: 08/05/2012 11:59 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย sunzandesign »

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

เช่น

echo '<pre>', print_r($array,true),'</pre>';
-------------------------------------
ballthai2015
PHP Newbie
PHP Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 21/07/2015 5:35 pm

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

โพสต์ที่ยังไม่ได้อ่าน โดย ballthai2015 »

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

รูปภาพ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 109