Creating menu with sub menus from two arrays in PHP - php

Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Tops
[parent_id] =>
)
[1] => stdClass Object
(
[id] => 2
[name] => Trousers
[parent_id] =>
)
[2] => stdClass Object
(
[id] => 3
[name] => Dresses
[parent_id] =>
)
[3] => stdClass Object
(
[id] => 4
[name] => Skirts
[parent_id] =>
)
[4] => stdClass Object
(
[id] => 5
[name] => Accessories
[parent_id] =>
)
[5] => stdClass Object
(
[id] => 6
[name] => Coats & Jackets
[parent_id] =>
)
)
Above is the parent categories i fetch from db.
And below are the sub categories
Array
(
[0] => stdClass Object
(
[id] => 8
[name] => Mini Tops
[parent_id] => 1
)
[1] => stdClass Object
(
[id] => 9
[name] => Long Tops
[parent_id] => 2
)
[2] => stdClass Object
(
[id] => 10
[name] => Cargo
[parent_id] => 2
)
[3] => stdClass Object
(
[id] => 11
[name] => Bermuda
[parent_id] => 2
)
[4] => stdClass Object
(
[id] => 12
[name] => Formal Dresses
[parent_id] => 3
)
[5] => stdClass Object
(
[id] => 13
[name] => Casual Dresses
[parent_id] => 3
)
[6] => stdClass Object
(
[id] => 14
[name] => Party Wear
[parent_id] => 3
)
[7] => stdClass Object
(
[id] => 15
[name] => Jewelry
[parent_id] => 4
)
[8] => stdClass Object
(
[id] => 16
[name] => Bracelets
[parent_id] => 4
)
[9] => stdClass Object
(
[id] => 17
[name] => Caps
[parent_id] => 4
)
)
below is how i am making my navitaion bar
<ul>
<li>
Tops
<ul class="sub-menu">
<li>Tshirts</li>
<li>Jumpers</li>
<li>Cardigans</li>
<li>Knitwear</li>
</ul>
</li>
</ul>
As my logics are not so much good i actually trying to make navitaion with these two arrays in a way that the first array which are main links should be on top order where as all respective childs will appear in submenu of there respective parent_id example
Long Top, Cargo and Bermuda should be falling under Trousers Link.
I need guidance how can i achieve this .. I tried foreach loop but stucked in middle
Thanks alot

If you're in control you should try to use a different choice of arrays because now you will be doing an unnecessary amount of looping. To demonstrate:
foreach($categories as $cat) {
echo "<li>".$cat->name."<ul>";
foreach($subcategories as $subcat) {
if($subcat->parent_id == $cat->id) echo "<li>".$subcat->name."</li>"
}
echo "</ul></li>";
}
You have to loop over all subcategories for each category. But if you instead stored subcategories in an array where the index represented the parent ID, you could easily retrieve them.
If you want to solve this with just one array you could make the list of subcategories a property of the corresponding parent category. This would be easy to handle and semantic.

Related

php add and merge data from two table

I have this query from first table:
$this->db->table('categories')
->select('id, name, parent_id')
->orderBy('parent_id')
->get()
->getResultObject();
data output is:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => cat1
[parent_id] => 0
)
[1] => stdClass Object
(
[id] => 2
[name] => cat2
[parent_id] => 0
)
[2] => stdClass Object
(
[id] => 5
[name] => cat5
[parent_id] => 0
)
[3] => stdClass Object
(
[id] => 6
[name] => cat6
[parent_id] => 0
)
[4] => stdClass Object
(
[id] => 3
[name] => cat3
[parent_id] => 2
)
[5] => stdClass Object
(
[id] => 4
[name] => cat4
[parent_id] => 2
)
)
and query for second table:
$this->db->table('post_category')
->select('category_id')
->where('post_id', $id)
->get()->getResultObject();
output data is:
Array
(
[0] => stdClass Object
(
[category_id] => 1
)
[1] => stdClass Object
(
[category_id] => 4
)
)
Now I need to add selected in array and add data(category_id) from table two in table one if (category_id = id) like this:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => cat1
[parent_id] => 0
[selected] => 1
)
[1] => stdClass Object
(
[id] => 2
[name] => cat2
[parent_id] => 0
[selected] =>
)
[2] => stdClass Object
(
[id] => 5
[name] => cat5
[parent_id] => 0
[selected] =>
)
[3] => stdClass Object
(
[id] => 6
[name] => cat6
[parent_id] => 0
[selected] =>
)
[4] => stdClass Object
(
[id] => 3
[name] => cat3
[parent_id] => 2
[selected] =>
)
[5] => stdClass Object
(
[id] => 4
[name] => cat4
[parent_id] => 2
[selected] => 1
)
)
How do can i add data from table two in table one(if category_id from table two = id from table one) like my last output?!
If you just want to populate the ['selected'] item of a subarray in the first array ("categories") with "1" based on whether there's a "post_category" with that value you can do this:
foreach ($categories as &$category) {
$filtered_post_categories = array_filter($post_categories, function($post_category) use ($category) {
return $post_category->category_id == $category->id;
});
if (count($filtered_post_categories) > 0) {
$category->selected = 1;
}
else {
$category->selected = null;
}
}

How to convert DB table with parent son relation to multi-dimensional array

How can I convert a database table like that:
into a multi-dimensional array like that ?
I tried a recursive loop like below but can't get it to display properly.
function cost_centres_format($items)
{
foreach ($items as $item) {
echo $item->name.' - '.$item->parent_id;
echo '<br/>';
$sons = $this->purchase_order_model->get_cost_centre_sons($item->internal_purchase_order_cost_centre_id);
if(count($sons)>0){
$this->cost_centres_format($sons);
}
}
}
Here is my solution for it:
function cost_centres_format($items,$parent_id,$array=array()) {
foreach($items as $item) {
if($item->parent_id == $parent_id) {
$array[] = $item;
if($item->internal_purchase_order_cost_centre_id>0) {
$array = cost_centres_format($items,$item->internal_purchase_order_cost_centre_id,$array);
}
}
}
return $array;
}
$array = cost_centres_format($items,0);
Diesel (id:5) will be below Vehicle Maintenance (id:4) because of its original order. You can do an additional sort by name but in your example Capital (id:3) was below Overheads (id:2).
The above code doesn't produce multidimensional array but instead one dimensional array like so :
Array
(
[0] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 1
[name] => Direct Expenses
[parent_id] => 0
)
[1] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 4
[name] => Vehicle Maintenance
[parent_id] => 1
)
[2] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 9
[name] => CN09 AKO
[parent_id] => 4
)
[3] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 10
[name] => DY52 BYO
[parent_id] => 4
)
[4] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 14
[name] => MX08 MVJ
[parent_id] => 4
)
[5] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 15
[name] => YJ55 TXA
[parent_id] => 4
)
[6] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 5
[name] => Diesel
[parent_id] => 1
)
[7] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 6
[name] => Vehicle Rent
[parent_id] => 1
)
[8] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 11
[name] => Vehicle Repair
[parent_id] => 1
)
[9] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 16
[name] => CN09 AKO
[parent_id] => 11
)
[10] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 17
[name] => DY52 BYO
[parent_id] => 11
)
[11] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 18
[name] => MX08 MVJ
[parent_id] => 11
)
[12] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 19
[name] => YJ55 TXA
[parent_id] => 11
)
[13] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 2
[name] => Overheads
[parent_id] => 0
)
[14] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 12
[name] => Internet Service
[parent_id] => 2
)
[15] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 13
[name] => Warehouse Rent
[parent_id] => 2
)
[16] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 3
[name] => Capital
[parent_id] => 0
)
[17] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 7
[name] => New Stock
[parent_id] => 3
)
[18] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 8
[name] => New Vehicle
[parent_id] => 3
)
)

PHP add arrays to an array on a specific element

I am trying to merge 2 arrays a single array on a multidimensional array where a given key-value = a value
the first array looks like this:
Array
(
[0] => Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
)
[1] => Array
(
[id] => 6
[category_order] => 0
[parent_id] => 4
[name] => Home Entertainment
)
[2] => Array
(
[id] => 7
[category_order] => 0
[parent_id] => 4
[name] => Photography
)
[3] => Array
(
[id] => 8
[category_order] => 0
[parent_id] => 4
[name] => Portable Audio
)
[4] => Array
(
[id] => 9
[category_order] => 0
[parent_id] => 4
[name] => Televisions
)
)
)
)
and the second like this:
Array
(
[0] => Array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
[1] => Array
(
[id] => 11
[parent_id] => 5
[name] => Audio Systems
)
[2] => Array
(
[id] => 12
[parent_id] => 5
[name] => Cassette Decks
)
[3] => Array
(
[id] => 13
[parent_id] => 5
[name] => CD Players
)
[4] => Array
(
[id] => 14
[parent_id] => 5
[name] => Radios
)
[5] => Array
(
[id] => 15
[parent_id] => 5
[name] => HiFi Speakers
)
)
What I want to do is add each of the second arrays to a sub array of the first multidimensional array where the parent_id of the second array = the id of the subcategories array of the first array so it will look like this:
array
(
[0]=> Array
(
[id] => 4
[subcategories] => Array
(
[0] => Array
(
[id] => 5
[category_order] => 0
[parent_id] => 4
[name] => Audio Equipment
[subsubcategories] = array
(
[id] => 10
[parent_id] => 5
[name] => Amplifiers & Receivers
)
)
Something like this should work just rename the array names because you didn't provide them. But I think you'll get the idea :) Mainly you loop through all subcategories with foreach loop or another you'll get the parent id and can access the main array with that parent id and save the sub categories info in there.
foreach( $sub_array as $item ) {
$main_array[ category_id ][ $item[ 'parent_id' ] ][ 'subsubcategories' ] = $item;
}

Children nodes moved to the root level on an hierarchy array

I have the following array on a hierarchy structure - It's basically an array with categories and its children under the 'child' key:
Array
(
[category_id] => 1
[parent_id] => 0
[name] => Commercial
[child] => Array
(
[0] => Array
(
[category_id] => 48
[parent_id] => 1
[name] => lights
[child] => Array
(
)
)
[1] => Array
(
[category_id] => 12
[parent_id] => 1
[name] => beacons
[child] => Array
(
[0] => Array
(
[category_id] => 91
[parent_id] => 12
[name] => blue beacons
[child] => Array
(
)
)
)
)
)
)
What I am trying to do is write a recursive function to reorganize this array as an ONE LEVEL array only. Instead of having its children inside the 'child' key, I want it to be part of the array root level. Like this:
[0] => Array
(
[category_id] => 1
[parent_id] => 0
[name] => Commercial
)
[1] => Array
(
[category_id] => 48
[parent_id] => 1
[name] => lights
)
[2] => Array
(
[category_id] => 12
[parent_id] => 1
[name] => beacons
)
[3] => Array
(
[category_id] => 91
[parent_id] => 12
[name] => blue beacons
)
Any ideas?
Thanks!
The following recursive function should serve your purpose:
function one_level_array($arr){
$ret = array();
$ret[] = array_diff_key($arr, array("child" => ""));
if(array_key_exists("child", $arr)){
foreach($arr["child"] as $child){
$ret = array_merge($ret, one_level_array($child));
}
}
return $ret;
}
DEMO

php codeigniter select results from foreach and save into an array

I have a php mysql query results as following...
Array
(
[0] => stdClass Object
(
[id] => 52
[sku] =>
[name] => stone product 52
)
[1] => stdClass Object
(
[id] => 53
[sku] =>
[name] => stone product 53
)
.
.
.
)
Array
(
[0] => stdClass Object
(
[id] => 12
[sku] =>
[name] => stone product 12
)
[1] => stdClass Object
(
[id] => 13
[sku] =>
[name] => stone product 13
)
.
.
.
)
And I want the results just like as below
Array(
[0] => stdClass Object
(
[id] => 52
[sku] =>
[name] => stone product 52
)
[1] => stdClass Object
(
[id] => 53
[sku] =>
[name] => stone product 53
)
[2] => stdClass Object
(
[id] => 12
[sku] =>
[name] => stone product 12
)
[3] => stdClass Object
(
[id] => 13
[sku] =>
[name] => stone product 13
)
in a single array
Please suggest me php code
You should not be joining these results in php. Use one query to get both sets of results, this will cut down on your db reads.
You can use array_merge function : http://php.net/manual/en/function.array-merge.php
if you are working on an array that has arrays inside you can merge them with a foreach call
$my_array=get_the_result_from_mysql();//Here how do you get the result.
$new_array=array();
foreach ($my_array as $row) {
$new_array=array_merge($new_array,$row);
}
var_dump($new_array);
I think this helps you.

Categories