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;
}
}
Related
I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);
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
)
)
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;
}
I have the following array structure:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
)
[1] => Array
(
[id] => 4
[user_id] => 1
[created] => 2014-11-09 13:52:02
[content] => 456789
[status] => 1
[parent] => 0
[project_id] => 1
)
)
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
[2] => Array
(
[id] => 5
[user_id] => 1
[created] => 2014-11-09 13:52:17
[content] => 8765432
[status] => 1
[parent] => 4
[project_id] => 1
)
)
I would like to merge them into a parent/child relationship so it looks like follows:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
)
)
)
How could I go about doing that with PHP?
Assuming that all ids are unique in your parents array, you can do this:
// build a new array using parent's ID values as the key,
// to simplify searching for parent IDs.
foreach ($parents as $key => $value){
$merged[$value['id']] = $value;
}
foreach ($children as $child){
$parentID = $child['parent'];
if (array_key_exists( $parentID, $merged )) {
// add the child array to its parent's ['children'] value
$merged[$parentID]['children'][] = $child;
}
}
In the resulting array $merged, the key of each parent item will be set to its id, and all children will be nested under their corresponding parents.
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Parent 1
[children] => Array
(
[0] => Array
(
[id] => 2
[parent] => 1
[title] => Child 1
[children] => Array
(
[0] => Array
(
[id] => 3
[parent] => 2
[title] => child 2
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 9
[parent] => 1
[title] => parent of one
[children] => Array
(
)
)
[2] => Array
(
[id] => 19
[parent] => 1
[title] => df
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4
[parent] => 0
[title] => parent 2
[children] => Array
(
[0] => Array
(
[id] => 5
[parent] => 4
[title] => child 4
[children] => Array
(
[0] => Array
(
[id] => 6
[parent] => 5
[title] => child 5
[children] => Array
(
[0] => Array
(
[id] => 7
[parent] => 6
[title] => child 7
[children] => Array
(
)
)
[1] => Array
(
[id] => 8
[parent] => 6
[title] => child 8
[children] => Array
(
[0] => Array
(
[id] => 12
[parent] => 8
[title] => child 9
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 17
[parent] => 5
[title] => child unknown
[children] => Array
(
[0] => Array
(
[id] => 18
[parent] => 17
[title] => asdasd
[children] => Array
(
)
)
)
)
)
)
)
)
[2] => Array
(
[id] => 13
[parent] => 0
[title] => parent 3
[children] => Array
(
)
)
[3] => Array
(
[id] => 14
[parent] => 0
[title] => parent 4
[children] => Array
(
[0] => Array
(
[id] => 21
[parent] => 14
[title] => sad
[children] => Array
(
[0] => Array
(
[id] => 22
[parent] => 21
[title] => sdfsaf
[children] => Array
(
[0] => Array
(
[id] => 23
[parent] => 22
[title] => test
[children] => Array
(
[0] => Array
(
[id] => 24
[parent] => 23
[title] => tester
[children] => Array
(
[0] => Array
(
[id] => 25
[parent] => 24
[title] => tested
[children] => Array
(
[0] => Array
(
[id] => 26
[parent] => 25
[title] => example
[children] => Array
(
[0] => Array
(
[id] => 27
[parent] => 26
[title] => examples
[children] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
[4] => Array
(
[id] => 15
[parent] => 0
[title] => parent 5
[children] => Array
(
)
)
[5] => Array
(
[id] => 16
[parent] => 0
[title] => parent 6
[children] => Array
(
)
)
[6] => Array
(
[id] => 20
[parent] => 0
[title] => aaa
[children] => Array
(
)
)
)
any suggestions on how to make a nested dropdown out of this array ?
i have no idea where to start.....
$HOST="localhost";
$DB="db_dir";
$USER="root";
$PASS="";
mysql_connect($HOST,$USER,$PASS);
mysql_select_db($DB);
function RecursiveCat($pid)
{
static $level=0;
static $strid="";
static $strname="";
$sql=mysql_query("select * from categories where cat_parent =".$pid." ");
//var_dump($sql);
while($row=mysql_fetch_assoc($sql))
{
$id=$row['cat_id'];
$level--;
$pad="";
for($p=1;$p<($level*-1);$p++) $pad.=" > ";
$strname.='<option value="'.$row['cat_id'].'">'.$pad.$row['cat_title'].'</option>';
$rid=RecursiveCat($id);
$strid[]=$row['cat_id'];
$level++;
}
return $strname;
}
<?php echo '<select name="parent">'.'<option value="0">None</option>';
echo RecursiveCat(0);echo '</select>';?>