I want to format array with Tree structure like below in PHP
TITLE 1
-- SUB TITLE 1-1
---- SUB TITLE 1-2
TITLE 2
-- SUB TITLE 2-1
Array structure :
Array
(
[0] => Array
(
[id] => 1
[sub_id] => 1
[title] => TITLE 1
[url] => www.title1.com
)
[1] => Array
(
[id] => 4
[sub_id] => 4
[title] => TITLE 2
[url] => www.title2.com
)
[2] => Array
(
[id] => 2
[sub_id] => 1
[title] => SUB TITLE 1-1
[url] => www.subtitle1.com
)
[3] => Array
(
[id] => 3
[sub_id] => 2
[title] => SUB TITLE 1-2
[url] => www.subtitle2.com
)
[4] => Array
(
[id] => 5
[sub_id] => 4
[title] => SUB TITLE 2-1
[url] => www.subtitle2.com
)
)
I have no idea how it should be done.
Here you are my solution:
<?php
$data=Array
(
0 => Array
(
'id' => 1,
'sub_id' => 1,
'title' => 'TITLE 1',
'url' => 'www.title1.com',
),
1 => Array
(
'id' => 4,
'sub_id' => 4,
'title' => 'TITLE 2',
'url' => 'www.title2.com',
),
2 => Array
(
'id' => 2,
'sub_id' => 1,
'title' => 'SUB TITLE 1-1',
'url' => 'www.subtitle1.com',
),
3 => Array
(
'id' => 3,
'sub_id' => 2,
'title' => 'SUB TITLE 1-2',
'url' => 'www.subtitle2.com',
),
4 => Array
(
'id' => 5,
'sub_id' => 4,
'title' => 'SUB TITLE 2-1',
'url' => 'www.subtitle2.com',
),
);
function find_index(&$arr, $ind){
if(empty($arr))
return false;
else {
foreach($arr as &$aa){
if($aa['id']==$ind['sub_id']) {
$key=count($aa['sub_ar']);
$aa['sub_ar'][$key]=$ind;
$aa['sub_ar'][$key]['sub_ar']=array();
return true;
}
else{
$res=find_index($aa['sub_ar'],$ind);
if($res)
return true;
}
}
return false;
}
}
$res=array();
foreach($data as $key=>$dat){
if($dat['sub_id']==$dat['id']){
$res[$key]=$dat;
$res[$key]['sub_ar']=array();
}
else {
$ret=find_index($res,$dat);
}
}
?>
<pre><?php print_r($res); ?></pre>
Related
I am trying to change the following array to an almost flat array. So id 4 would be in the first level of the array, as would id 6 and 5, but still have their own index so I can tell which page is which. But with the same order as they have now. I presume that the solution would be some sort of recursive PHP function but I haven't a clue how to do this.
Array
(
[0] => Array
(
[id] => 2
[identifier] => External URL
[parent] => 0
[sortOrder] => 1
[depth] => 0
)
[1] => Array
(
[id] => 3
[identifier] => First Team
[parent] => 0
[sortOrder] => 2
[depth] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[identifier] => League tables
[parent] => 3
[sortOrder] => 0
[depth] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[identifier] => British and Irish Cup Tables
[parent] => 4
[sortOrder] => 24
[depth] => 2
)
[1] => Array
(
[id] => 5
[identifier] => Greene King IPA Championship
[parent] => 4
[sortOrder] => 25
[depth] => 2
)
)
)
)
)
[2] => Array
(
[id] => 1
[identifier] => Home
[parent] => 0
[sortOrder] => 25
[depth] => 0
)
)
<?php
$data = [
[
'id' => 1,
'name' => 'one',
'children' =>
[
[
'id' => 2,
'name' => 'two',
'children' =>
[
[
'id' => 4,
'name' => 'four'
]
]
],
[
'id' => 3,
'name' => 'three',
'children' =>
[
[
'id' => 5,
'name' => 'five'
]
]
]
]
],
[
'id' => 6,
'name' => 'six'
]
];
$stanley = [];
$flatten = function(array $data) use (&$flatten, &$stanley) {
foreach($data as $k => $v) {
if(isset($v['children'])) {
$flatten($v['children']);
unset($v['children']);
}
$stanley[] = $v;
}
};
$flatten($data);
var_export($stanley);
Output:
array (
0 =>
array (
'id' => 4,
'name' => 'four',
),
1 =>
array (
'id' => 2,
'name' => 'two',
),
2 =>
array (
'id' => 5,
'name' => 'five',
),
3 =>
array (
'id' => 3,
'name' => 'three',
),
4 =>
array (
'id' => 1,
'name' => 'one',
),
5 =>
array (
'id' => 6,
'name' => 'six',
),
)
I have found the solution! I built a recursive PHP function which utilised the depth index to track which level each of the items are while still keeping the array flat (ish).
function dropdownNavigationTree($array) {
$response = [];
foreach($array as $page) {
if (!is_array($page['children'])) {
$response[$page['id']] = ($page['depth'] > 0 ? str_repeat("-", $page['depth']).' ' : FALSE).$page['identifier'];
} else {
$response[$page['id']] = ($page['depth'] > 0 ? str_repeat("-", $page['depth']).' ' : FALSE).$page['identifier'];
$children = dropdownNavigationTree($page['children']);
$response = $response + $children;
}
}
return $response;
}
I have an array that looks like
$firstArray
[0] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
)
[1] => Array(
[ID] => 2
[Fruit] => Orange
[State] => Hawaii
)
[2] => Array(
[ID] => 3
[Fruit] => Orange
[State] => Vermont
)
And another array which looks like
$secondArray
[0] => Array(
[ID] => 1
[description] => This is sample description
[Price] => 20
)
[1] => Array(
[ID] => 1
[Fruit] => This is sample description 2
[Price] => 15
)
[2] => Array(
[ID] => 2
[Fruit] => This is the second description
[Price] => 100
)
[3] => Array(
[ID] => 2
[Fruit] => This is the second description 2
[Price] => 50
)
[4] => Array(
[ID] => 3
[Fruit] => This is the third description
[Price] => 99
)
I have used
$newArray = array_merge_recursive($firstArray, $secondArray);
This has only appended entries from the second array to the end of the first array ideally i would want the new array to look like this
[0] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
[description]
Array(
[0] => This is sample description
[1] => This is sample description 2
)
[price]
Array(
[0] => 20
[1] => 15
)
[1] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
[description]
Array(
[0] => This is the second description
[1] => This is the second description 2
)
[price]
Array(
[0] => 100
[1] => 50
)
Essentially the new array should be combined on the ID and create nested arrays from the second array.
See if this makes sense. https://iconoun.com/demo/temp_jumpman.php
<?php // demo/temp_jumpman.php
/**
* Dealing with PHP nested arrays
*
* https://stackoverflow.com/questions/45145282/merge-two-arrays-by-specific-fields
*/
error_reporting(E_ALL);
echo '<pre>';
// TEST DATA CREATED FROM THE POST AT STACK - ID, Fruit, State
$first = Array(
'0' => [
'ID' => '1',
'Fruit' => 'Apple',
'State' => 'Ohio',
],
'1' => [
'ID' => '2',
'Fruit' => 'Orange',
'State' => 'Hawaii',
],
'2' => [
'ID' => '3',
'Fruit' => 'Orange',
'State' => 'Vermont',
],
);
// TEST DATA CREATED FROM THE POST AT STACK - ID, description, Price
$second = Array(
'0' => [
'ID' => '1',
'description' => 'This is sample description',
'Price' => '20',
],
'1' => [
'ID' => '1',
'description' => 'This is sample description 2',
'Price' => '15',
],
'2' => [
'ID' => '2',
'description' => 'This is the second description',
'Price' => '100',
],
'3' => [
'ID' => '2',
'description' => 'This is the second description 2',
'Price' => '50',
],
'4' => [
'ID' => '3',
'description' => 'This is the third description',
'Price' => '99',
],
);
$out = [];
foreach ($first as $arr)
{
// PLACEHOLDERS FOR THE FOUND DATA FROM THE SECOND ARRAY
$arr['description'] = [];
$arr['Price'] = [];
$id = $arr['ID']; // THE ID WE WANT IN THE SECOND ARRAY
foreach ($second as $idp)
{
if ($idp['ID'] == $arr['ID'])
{
$arr['description'][] = $idp['description'];
$arr['Price'][] = $idp['Price'];
}
}
$out[] = $arr;
}
print_r($out);
I have one array. I want to find unique value of fk_section_id and other fields(section_name & fk_section_id) associated with it. I also want to sort this array by fk_section_id. How to get this array from the original one?
Array
Array
(
[0] => Array
(
[fk_field_id] => 1
[fk_section_id] => 1
[section_order] => 1
[field_order] => 1
[field_name] => Title
[section_name] => Your Detail
[fk_field_type_id] => 4
)
[1] => Array
(
[fk_field_id] => 2
[fk_section_id] => 1
[section_order] => 1
[field_order] => 2
[field_name] => Name
[section_name] => Your Detail
[fk_field_type_id] => 1
)
[2] => Array
(
[fk_field_id] => 3
[fk_section_id] => 2
[section_order] => 2
[field_order] => 1
[field_name] => Road
[section_name] => Address For Correspondence
[fk_field_type_id] => 1
)
)
Expected Output
Array
(
[0] => Array
(
[fk_section_id] => 1
[section_order] => 1
[section_name] => Your Detail
)
[1] => Array
(
[fk_section_id] => 2
[section_order] => 2
[section_name] => Address For Correspondence
)
)
I tried below
$i=0;
foreach($formFields as $val){
$i++;
$allSections[$i]['section_name'] = $val['section_name'];
$allSections[$i]['fk_section_id'] = $val['fk_section_id'];
$allSections[$i]['section_order'] = $val['section_order'];
}
And it gives
Array
(
[0] => Array
(
[section_name] => Your Detail
[fk_section_id] => 1
[section_order] => 1
)
[1] => Array
(
[section_name] => Your Detail
[fk_section_id] => 1
[section_order] => 1
)
[2] => Array
(
[section_name] => Address For Correspondence
[fk_section_id] => 2
[section_order] => 2
)
)
You can try this:
$data = array(
array(
'fk_field_id' => 1,
'fk_section_id' => 1,
'section_order' => 1,
'field_order' => 1,
'field_name' => 'Title',
'section_name' => 'Your Detail',
'fk_field_type_id' => 4,
),
array(
'fk_field_id' => 2,
'fk_section_id' => 1,
'section_order' => 1,
'field_order' => 2,
'field_name' => 'Name',
'section_name' => 'Your Detail',
'fk_field_type_id' => 1,
),
);
$temp = array();
foreach($data as $key => $value) { //Make an new array using fk_field_id as key
if (!in_array($value['fk_field_id'], $temp)) {
$temp[$value['fk_field_id']] = $value;
}
}
ksort($temp); //Sort the array by key
var_dump($temp);
Result:
array (size=2)
1 =>
array (size=7)
'fk_field_id' => int 1
'fk_section_id' => int 1
'section_order' => int 1
'field_order' => int 1
'field_name' => string 'Title' (length=5)
'section_name' => string 'Your Detail' (length=11)
'fk_field_type_id' => int 4
2 =>
array (size=7)
'fk_field_id' => int 2
'fk_section_id' => int 1
'section_order' => int 1
'field_order' => int 2
'field_name' => string 'Name' (length=4)
'section_name' => string 'Your Detail' (length=11)
'fk_field_type_id' => int 1
Also now you can use:
echo $temp[1]['field_name'];
Result:
Title
Looking to create a function which arrangesmy nested set model into an array based on levels (categories[0], sub_categories[1], types[2]).
categories[0]
Alcohol |
| sub_categories[1]
|—————Beer
|
|—————Spirts
|
|—————Cider
|
|—————Wine
| |
| | types[2]
| |—————White wine
| |
| |—————Red wine
|
Bvrages |
| sub_categories[1]
|————— Soft Drinks
I was having a look at an older post and it got me close but only transferred the title and not the whole array How do I format Nested Set Model data into an array?.
What i want to achieve is a similar structure like above. Just remember i want to move all data not just the title.
My current output from my query is as follows:
Array
(
[0] => Array
(
[id] => 1
[title] => alcohol
[level] => 0
[uri] => alcohol
[count] => 100
)
[1] => Array
(
[id] => 2
[title] => beer
[level] => 1
[uri] => beer
[count] => 50
)
[2] => Array
(
[id] => 3
[title] => cider
[level] => 1
[uri] => cider
[count] => 20
)
[3] => Array
(
[id] => 4
[title] => wine
[level] => 1
[uri] => wine
[count] => 20
)
[4] => Array
(
[id] => 6
[title] => white wine
[level] => 2
[uri] => white-wine
[count] => 5
)
[5] => Array
(
[id] => 7
[title] => red wine
[level] => 2
[uri] => red-wine
[count] => 15
)
[6] => Array
(
[id] => 8
[title] => spirits
[level] => 1
[uri] => spirits
[count] => 5
)
[7] => Array
(
[id] => 9
[title] => Beverages
[level] => 0
[uri] => beverages
[count] => 50
)
[8] => Array
(
[id] => 10
[title] => soft drinks
[level] => 1
[uri] => soft-drink
[count] => 10
)
)
Any thoughts?
Try to use this code:
<?php
//Represent your array like this (you may include other data to the each item)
//array must be ordered by 'left' field:
$data = array(
array("left" => 1, "right" => 10, "name" => "P0"),
array("left" => 2, "right" => 7, "name" => "P1"),
array("left" => 3, "right" => 4, "name" => "P11"),
array("left" => 5, "right" => 6, "name" => "P12"),
array("left" => 8, "right" => 9, "name" => "P2")
);
//Converter function gets nested sets array and returns nested php array
function nest($arrData){
$stack = array();
$arraySet = array();
foreach( $arrData as $intKey=>$arrValues) {
$stackSize = count($stack);
while($stackSize > 0 && $stack[$stackSize-1]['right'] < $arrValues['left']) {
array_pop($stack);
$stackSize--;
}
$link =& $arraySet;
for($i=0;$i<$stackSize;$i++) {
$link =& $link[$stack[$i]['id']]["children"]; //navigate to the proper children array
}
$tmp = array_push($link, array ('item'=>$arrValues,'children'=>array()));
array_push($stack, array('id' => $tmp-1, 'right' => $arrValues['right']));
}
return $arraySet;
}
//Print result
printArray(nest($data));
function printArray($array){
echo "<ul>";
foreach ($array as $row){
$children = $row['children'];
echo "<li>";
echo $row['item']['name'];
if (!empty($children)) printArray($children);
echo "</li>";
}
echo "</ul>";
}
?>
If I understood you right, then try this solution:
<?php
$data=Array(
0 => Array(
'id' => 1,
'title' => 'alcohol',
'level' => 0,
'uri' => 'alcohol',
'count' => 100,
),
1 => Array(
'id' => 2,
'title' => 'beer',
'level' => 1,
'uri' => 'beer',
'count' => 50,
),
2 => Array(
'id' => 3,
'title' => 'cider',
'level' => 1,
'uri' => 'cider',
'count' => 20,
),
3 => Array(
'id' => 4,
'title' => 'wine,',
'level' => 1,
'uri' => 'wine',
'count' => 20,
),
4 => Array(
'id' => 6,
'title' => 'white wine',
'level' => 2,
'uri' => 'white-wine',
'count' => 5,
),
5 => Array(
'id' => 7,
'title' => 'red wine',
'level' => 2,
'uri' => 'red-wine',
'count' => 15,
),
6 => Array(
'id' => 8,
'title' => 'spirits',
'level' => 1,
'uri' => 'spirits',
'count' => 5,
),
7 => Array(
'id' => 9,
'title' => 'Beverages',
'level' => 0,
'uri' => 'beverages',
'count' => 50,
),
8 => Array(
'id' => 10,
'title' => 'soft drinks',
'level' => 1,
'uri' => 'soft-drink',
'count' => 10,
)
);
$cats=array();
$prev_level=1;
$index0='';
$index1=0;
foreach($data as $dat){
switch($dat['level']){
case 0:
$cats[$dat['uri']]=array(
'name'=>$dat['title'],
'sub_categories'=>array()
);
$index0=$dat['uri'];
break;
case 1:
if($prev_level<1)
$index1=0;
else
$index1++;
$cats[$index0]['sub_categories'][$index1]=array(
'name'=>$dat['title']
);
break;
case 2:
if($prev_level<2){
$cats[$index0]['sub_categories'][$index1]['types']=array();
}
$cats[$index0]['sub_categories'][$index1]['types'][]=$dat['title'];
break;
}
$prev_level=$dat['level'];
}
print_r($cats);
?>
My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....