Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > PHP > PHP Forum > สอบถามวิธีการทำ topological sort หน่อยครับ (แบบที่มีการจัดเรียงโดยเอาตัวที่มัน depend มาขึ้นก่อนน่ะครับ)


[PHP] สอบถามวิธีการทำ topological sort หน่อยครับ (แบบที่มีการจัดเรียงโดยเอาตัวที่มัน depend มาขึ้นก่อนน่ะครับ)

 
Topic : 127612



โพสกระทู้ ( 4,762 )
บทความ ( 8 )



สถานะออฟไลน์



ผมมีข้อมูล array แบบนี้

Code (PHP)
1.$test = [
2.    ['id' => 'one', 'deps' => ['two']],
3.    ['id' => 'two', 'deps' => ['three', 'four']],
4.    ['id' => 'three', 'deps' => []],
5.    ['id' => 'four', 'deps' => []],
6.    ['id' => 'five', 'deps' => ['four']],
7.    ['id' => 'six', 'deps' => []],
8.];


ผมอยากให้มันเรียงออกมาเป็น...
Code (PHP)
1.[
2.    ['id' => 'three', 'deps' => []],
3.    ['id' => 'four', 'deps' => []],
4.    ['id' => 'two', 'deps' => ['three', 'four']],
5.    ['id' => 'one', 'deps' => ['two']],
6.    ['id' => 'five', 'deps' => ['four']],
7.    ['id' => 'six', 'deps' => []],
8.];


ปัจจุบันมีโค้ดที่ลองไปแล้วหลายตัว ได้แก่
1 http://stackoverflow.com/questions/39711720/php-order-array-based-on-elements-dependency
2 http://stackoverflow.com/questions/15901159/how-can-i-rearrange-array-items-moving-dependencies-on-top
แต่ว่ายังไม่มีตัวไหนทำงานได้อย่างที่ต้องการ ที่ทำได้ใกล้เคียงที่สุดลำดับมันก็ค่อนข้างมั่วเลยครับ ไม่ให้ความสำคัญกับลำดับที่ปรากฏมาก่อนในต้นฉบับเลย
ยกตัวอย่างลิ้งค์แรกจะได้ three, four, five, six, two, one ซึ่งควรจะเป็น three, four, two, one, five, six.

ทีนี้ส่วนของกระผมที่ทำไปแล้วก็มีอยู่ แต่มันออกไม่หมด เพราะมันติดตรงส่งผ่านค่าที่เป็น sub ย่อยลงไปแล้วตัวแม่มันหาย -_-!
Code (PHP)
01.function topologicalSort(array $items, array &$sorted = [])
02.{
03.    foreach ($items as $indexKey => $item) {
04.        echo 'loop items: '.$item['id'].'<br>';// debug
05.        if (isset($item['deps']) && is_array($item['deps']) && !empty($item['deps'])) {
06.            echo ' &nbsp;&nbsp; '.$item['id'].' has depend.<br>';// debug
07.            foreach ($item['deps'] as $dependency_id) {
08.                echo ' &nbsp;&nbsp; searching key for depend '.$dependency_id.'<br>';// debug
09.                if (false !== $items_key = array_search($dependency_id, array_column($items, 'id'))) {
10.                    echo ' &nbsp;&nbsp; depend on '.$dependency_id.' => '.$items_key.'<br>';// debug
11.                    echo ' &nbsp;&nbsp; &nbsp; &nbsp; &gt;&gt; ';// debug
12.                    //$sorted[] = $item;
13.                    topologicalSort([$items_key => $items[$items_key]], $sorted);
14.                    echo ' &nbsp;&nbsp; after request on depend '.$dependency_id.' from '.$item['id'].'<br>';// debug
15.                }
16.            }// endforeach;
17.            unset($dependency_id);
18.            echo ' &nbsp;&nbsp; end load dependencies for '.$item['id'].'<br>';// debug
19.        } else {
20.            echo ' &nbsp;&nbsp; '.$item['id'].' doesn\'t have depend.<br>';// debug
21.            if (isset($item['id']) && false === $item_key = array_search($item['id'], array_column($sorted, 'id'))) {
22.                echo ' &nbsp;&nbsp; [['.$item['id'].' was set.]]<br>';// debug
23.                $sorted[] = $item;
24.            }
25.        }// endif; dependency.
26.    }// endforeach;
27.    unset($indexKey, $item);
28.}// topologicalSort




Tag : PHP



ประวัติการแก้ไข
2017-05-15 19:12:59
2017-05-15 19:13:31
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-05-15 19:09:46 By : mr.v View : 739 Reply : 1
 

 

No. 1



โพสกระทู้ ( 4,762 )
บทความ ( 8 )



สถานะออฟไลน์


ได้ละ ใช้ marcj/topsort
https://github.com/marcj/topsort.php
ติดตั้งผ่าน composer https://packagist.org/packages/marcj/topsort

Code (PHP)
01.<?php
02. 
03.$test = [
04.    ['id' => 'one', 'deps' => ['two']],
05.    ['id' => 'two', 'deps' => ['three', 'four']],
06.    ['id' => 'three', 'deps' => []],
07.    ['id' => 'four', 'deps' => []],
08.    ['id' => 'five', 'deps' => ['four']],
09.    ['id' => 'six', 'deps' => []],
10.    ['id' => 'seven', 'deps' => ['eight']],
11.    ['id' => 'eight', 'deps' => ['nine']],
12.    ['id' => 'nine', 'deps' => ['one']],
13.];
14. 
15.// the sorted should be three, four, two, one, five, six, nine, eight, seven
16. 
17.require 'vendor/autoload.php';
18. 
19.var_dump($test);
20.echo '<hr>';
21.echo '<div>the result should be &quot;three, four, two, one, five, six, nine, eight, seven&quot;</div>'."\n";
22. 
23. 
24.$Sorter = new \MJS\TopSort\Implementations\FixedArraySort();
25. 
26.foreach ($test as $item) {
27.    if (isset($item['id']) && isset($item['deps'])) {
28.        $Sorter->add($item['id'], $item['deps']);
29.    }
30.}// endforeach;
31. 
32.$result = $Sorter->sort();
33. 
34.var_dump($result);
35.unset($result, $Sorter);



ประวัติการแก้ไข
2017-05-16 14:03:12
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-05-16 13:42:44 By : mr.v
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามวิธีการทำ topological sort หน่อยครับ (แบบที่มีการจัดเรียงโดยเอาตัวที่มัน depend มาขึ้นก่อนน่ะครับ)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่