<?php
class treeView
{
public $treeview = array();
// In the construct, we transform your current array to an array that we can use
function __construct($main)
{
$main = (array) $main;
foreach($main as $view)
{
// We add the parent ONLY if we don't already have it
$this->treeview['parent'][$view['win_name']]['win_name'] = $view['win_name'];
$this->treeview['parent'][$view['win_name']]['win_id'] = $view['win_id'];
$this->treeview['parent'][$view['win_name']]['sales_mode'] = $view['sales_mode'];
// Adding all of the childs
$this->treeview['child'][$view['win_name']][$view['item_id']]['item_name'] = $view['item_name'];
$this->treeview['child'][$view['win_name']][$view['item_id']]['item_id'] = $view['item_id'];
}
}
public function buildTree()
{
// Looping through each parent and outputting it's content and it's children
foreach($this->treeview['parent'] as $branch)
{
if(isset($this->treeview['child'][$branch['win_name']]))
{
echo "<ol>";
echo "<li>Win Name: {$branch['win_name']}</li>";
echo "<li>Win ID: {$branch['win_id']}</li>";
echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
$this->buildChild($branch['win_name']); // Our call which builds the children
echo "</ol>";
}
else
{
echo "<ol>";
echo "<li>Win Name: {$branch['win_name']}</li>";
echo "<li>Win ID: {$branch['win_id']}</li>";
echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
echo "</ol>";
}
}
}
private function buildChild($parent)
{
// Looping through each child... technically we could make this recursive and allow children to have children
foreach($this->treeview['child'][$parent] as $child)
{
echo "<li style=\"list-style-type: none;\">";
echo "<ol>";
echo "<li>Item Name: {$child['item_name']}</li>";
echo "<li>Item ID: {$child['item_id']}</li>";
echo "</ol>";
echo "</li>";
}
}
}
$array = array (
array (
'item_name' => "\$rocks",
'item_id' => "3045",
'win_name' => "Tequila Mods",
'win_id' => "3",
'sales_mode' => "1",
),
array (
'item_name' => "Queso Fundido",
'item_id' => "1101",
'win_name' => "Snacks",
'win_id' => "29",
'sales_mode' => "1",
),
array (
'item_name' => "Texaz Dip",
'item_id' => "1102",
'win_name' => "Snacks",
'win_id' => "29",
'sales_mode' => "1",
),
array (
'item_name' => "Summer Sea",
'item_id' => "1481",
'win_name' => "Snacks",
'win_id' => "29",
'sales_mode' => "1",
)
);
// Initiating our class
$tree = new treeView($array);
// Building our tree
$tree->buildTree();
?>
That would generate the following list
<ol><li>Win Name: Tequila Mods</li><li>Win ID: 3</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: $rocks</li><li>Item ID: 3045</li></ol></li></ol><ol><li>Win Name: Snacks</li><li>Win ID: 29</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: Queso Fundido</li><li>Item ID: 1101</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Texaz Dip</li><li>Item ID: 1102</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Summer Sea</li><li>Item ID: 1481</li></ol></li></ol>
To be honest, this is very dirty and ugly. I made this code to work with your array, but it's not the best approach. A better approach is to use PDO, and use FETCH_GROUP to group by win_name (the parent)
and get the desired array that way.
Won't be bad to study Veerendra's solution 'Thank you ficuscr' and possibly try to implement a similar system with your requirements.