Lets say, I have the following array:
$array = array(
array(
"id" => 1,
"name" => "Europe",
"path" => "/"
),
array(
"id" => 2,
"name" => "Germany",
"path" => "/1/"
),
array(
"id" => 3,
"name" => "France",
"path" => "/1/"
),
array(
"id" => 4,
"name" => "Berlin",
"path" => "/1/2/"
),
array(
"id" => 5,
"name" => "Munich",
"path" => "/1/2/"
)
);
As you can see, its a multidimensional array with 3 properites in earch 2nd level array: id, name and path. The path is a path structure based on the parent-id of its parent. For example, Germany (id=2) has belongs to Europe, so the path is "/1/" (ID 1 = Europe) and Berlin in Germany has the path "/1/2/" which means "/Europe/Germany/"
Now, I am trying to create a tree-array out of this, which should somehow look like:
$result = array(
1 => array(
"id" => 1,
"name" => "Europe",
"path" => "/",
"childs" => array(
2 => array(
"id" => 2,
"name" => "Germany",
"path" => "/1/",
"childs" => array(
4 => array(
"id" => 4,
"name" => "Berlin",
"path" => "/1/2/"
),
5 => array(
"id" => 5,
"name" => "Munich",
"path" => "/1/2/"
)
)
),
3 => array(
"id" => 3,
"name" => "France",
"path" => "/1/"
)
)
)
);
I have already tried to create a function with internal references, but this didn't works for me:
public static function pathToTree($items) {
$array = array();
$result = array();
foreach($items AS $res) {
$ids = explode("/", ltrim($res["path"] . $res["id"], "/"));
$count = count($ids);
$tmp = &$result;
foreach( $ids AS $id) {
if($count == 1) {
$tmp = $res;
$tmp["childs"] = array();
$tmp = &$tmp["childs"];
}
else {
$tmp[$id] = array(
"childs" => array()
);
$tmp = &$tmp[$id]["childs"];
}
$count--;
}
}
return $array;
}
Ok, I think I just found a solution:
function pathToTree($array){
$tree = array();
foreach($array AS $item) {
$pathIds = explode("/", ltrim($item["path"], "/") . $item["id"]);
$current = &$tree;
foreach($pathIds AS $id) {
if(!isset($current["childs"][$id])) $current["childs"][$id] = array();
$current = &$current["childs"][$id];
if($id == $item["id"]) {
$current = $item;
}
}
}
return $tree["childs"];
}
This is a dynamice solution for 1-n depth. Look at my example at http://ideone.com/gn0XLp . Here I tested it with some level:
Continent
Country
City
City-District
City-Subdistrict
City Sub-Sub-District
I've created a recursive function:
// The array
$array = array(
array(
"id" => 1,
"name" => "Europe",
"path" => "/"
),
array(
"id" => 2,
"name" => "Germany",
"path" => "/1/"
),
array(
"id" => 3,
"name" => "France",
"path" => "/1/"
),
array(
"id" => 4,
"name" => "Berlin",
"path" => "/1/2/"
),
array(
"id" => 5,
"name" => "Munich",
"path" => "/1/2/"
),
array(
"id" => 6,
"name" => "Asia",
"path" => "/"
),
array(
"id" => 7,
"name" => "India",
"path" => "/6/"
),
array(
"id" => 7,
"name" => "Mumbai",
"path" => "/6/7"
),
array(
"id" => 8,
"name" => "Delhi",
"path" => "/6/7"
),
);
// The recursive function
function createTree($input, &$result = array(), $key = null) {
if ($key == "id") {
$result["temp"]["id"] = $input;
}
if ($key == "name") {
$result["temp"]["name"] = $input;
}
if ($key == "path") {
$result["temp"]["path"] = $input;
$levels = is_string($input) ? array_values(array_filter(explode('/', $input))) : null;
if ($input == "/") {
$result[$result["temp"]["id"]] = $result["temp"];
}
if (count($levels) == 1) {
$result[$levels[0]]["childs"][$result["temp"]["id"]] = $result["temp"];
}
if (count($levels) == 2) {
$result[$levels[0]]["childs"][$levels[1]]["childs"][$result["temp"]["id"]] = $result["temp"];
}
unset($result["temp"]);
}
if (is_array($input)) {
foreach($input as $key => $value) {
createTree($value, $result, $key);
}
}
return $result;
}
// The result
array (
1 =>
array (
'id' => 1,
'name' => 'Europe',
'path' => '/',
'childs' =>
array (
2 =>
array (
'id' => 2,
'name' => 'Germany',
'path' => '/1/',
'childs' =>
array (
4 =>
array (
'id' => 4,
'name' => 'Berlin',
'path' => '/1/2/',
),
5 =>
array (
'id' => 5,
'name' => 'Munich',
'path' => '/1/2/',
),
),
),
3 =>
array (
'id' => 3,
'name' => 'France',
'path' => '/1/',
),
),
),
6 =>
array (
'id' => 6,
'name' => 'Asia',
'path' => '/',
'childs' =>
array (
7 =>
array (
'id' => 7,
'name' => 'India',
'path' => '/6/',
'childs' =>
array (
7 =>
array (
'id' => 7,
'name' => 'Mumbai',
'path' => '/6/7',
),
8 =>
array (
'id' => 8,
'name' => 'Delhi',
'path' => '/6/7',
),
),
),
),
),
)
As I said in the comment, you need at least three loops to achieve your goals. Here's the function:
function pathToTree($array){
$tree = Array();
for($i=0; $i < count($array); $i++){
if(substr_count($array[$i]["path"], '/') == 1)
$tree[$array[$i]["id"]] = $array[$i];
}
for($i=0; $i < count($array); $i++){
if(substr_count($array[$i]["path"], '/') == 2){
$num = (int)str_replace("/","",$array[$i]["path"]);
$tree[$num]["childs"][$array[$i]["id"]] = $array[$i];
}
}
for($i=0; $i < count($array); $i++){
if(substr_count($array[$i]["path"], '/') == 3){
$num = explode("/", $array[$i]["path"]);
$tree[$num[1]]["childs"][$num[2]]["childs"][$array[$i]["id"]] = $array[$i];
}
}
return $tree;
}
Example:
Consider this array:
$array = array(
array(
"id" => 1,
"name" => "Europe",
"path" => "/"
),
array(
"id" => 2,
"name" => "Germany",
"path" => "/1/"
),
array(
"id" => 3,
"name" => "France",
"path" => "/1/"
),
array(
"id" => 4,
"name" => "Berlin",
"path" => "/1/2/"
),
array(
"id" => 5,
"name" => "Munich",
"path" => "/1/2/"
),
array(
"id" => 6,
"name" => "Asia",
"path" => "/"
),
array(
"id" => 7,
"name" => "China",
"path" => "/6/"
),
array(
"id" => 8,
"name" => "Bangladesh",
"path" => "/6/"
),
array(
"id" => 9,
"name" => "Beijing",
"path" => "/6/7/"
),
array(
"id" => 10,
"name" => "Dhaka",
"path" => "/6/8/"
)
);
if I ran this code:
print_r(pathToTree($array));
the output will be:
Array
(
[1] => Array
(
[id] => 1
[name] => Europe
[path] => /
[childs] => Array
(
[2] => Array
(
[id] => 2
[name] => Germany
[path] => /1/
[childs] => Array
(
[4] => Array
(
[id] => 4
[name] => Berlin
[path] => /1/2/
)
[5] => Array
(
[id] => 5
[name] => Munich
[path] => /1/2/
)
)
)
[3] => Array
(
[id] => 3
[name] => France
[path] => /1/
)
)
)
[6] => Array
(
[id] => 6
[name] => Asia
[path] => /
[childs] => Array
(
[7] => Array
(
[id] => 7
[name] => China
[path] => /6/
[childs] => Array
(
[9] => Array
(
[id] => 9
[name] => Beijing
[path] => /6/7/
)
)
)
[8] => Array
(
[id] => 8
[name] => Bangladesh
[path] => /6/
[childs] => Array
(
[10] => Array
(
[id] => 10
[name] => Dhaka
[path] => /6/8/
)
)
)
)
)
)
Here's the phpfiddle link in case you might try it yourself.
Related
I have $tree like array which made by function from the $array I need a function to make it $tree to $array back
so I need a function to make it back.
lets call it $list this time.
I will share $tree , $array and the function in below
tree like array ;
Array
(
[0] => Array
(
[id] => 1
[name] => id1
[children] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[name] => id2
[children] => Array
(
[0] => Array
(
[id] => 5
[parent_id] => 2
[name] => id5
)
)
)
[1] => Array
(
[id] => 3
[parent_id] => 1
[name] => id3
[children] => Array
(
[0] => Array
(
[id] => 6
[parent_id] => 3
[name] => id6
)
[1] => Array
(
[id] => 8
[parent_id] => 3
[name] => id8
)
)
)
)
)
[1] => Array
(
[id] => 4
[name] => id4
[children] => Array
(
[0] => Array
(
[id] => 9
[parent_id] => 4
[name] => id9
[children] => Array
(
[0] => Array
(
[id] => 10
[parent_id] => 9
[name] => id10
)
)
)
)
)
[2] => Array
(
[id] => 7
[name] => id7
[children] => Array
(
)
)
)
Which made by a function from this array
$array = [
['id'=> 1, 'parent_id' => 0, 'name' => 'id1'],
['id' => 2, 'parent_id' => 1, 'name'=> 'id2'],
['id' => 3, 'parent_id' => 1, 'name'=> 'id3'],
['id' => 4, 'parent_id' => 0, 'name'=> 'id4'],
['id' => 5,'parent_id' => 2, 'name'=> 'id5'],
['id' => 6, 'parent_id' => 3, 'name'=> 'id6'],
['id' => 7, 'parent_id' => 0, 'name'=> 'id7'],
['id' => 8, 'parent_id' => 3, 'name'=> 'id8'],
['id' => 9, 'parent_id' => 4, 'name'=> 'id9'],
['id' => 10, 'parent_id' => 9, 'name'=> 'id10'],
];
function(making $array in to $tree)
$tree = [];
function buildTree (array $infos, int $parent_Id = null): array
{
$branch = [];
foreach ($infos as $info)
if($info['parent_id'] === $parent_Id){
$children = buildTree($infos , $info['id']);
if ($children){
$info['children'] = $children;
}
$branch[] = $info;
}
return $branch;
}
foreach ($array as $info){
if($info['parent_id']=== 0){
$tree[] = [
'id' => $info['id'],
'name' => $info['name'],
'children' => buildTree($array , $info['id']),
];
}
}
print_r($tree);
any explanation would be appreciated.
Result of this code can be found here: http://sandbox.onlinephpfunctions.com/code/38a091db5ace63900fa0bf69ddde17412118513c
function flatMergeArray(array $array, int $parentId = 0, array &$result = []): array
{
$subResult = [];
foreach ($array as $key => $sub) {
$parentId = $array['parent_id'] ?? 0;
if (is_array($sub)) {
flatMergeArray($sub, $parentId, $result);
} else {
$subResult[$key] = $sub;
}
}
if (!empty($subResult)) {
if (!isset($subResult['parent_id'])) {
$subResult['parent_id'] = 0;
}
$result[] = $subResult;
}
return $result;
}
function flatTree(array $tree): array
{
$array = flatMergeArray($tree);
usort($array, static function (array $node1, array $node2) {
return ($node1['id'] < $node2['id']) ? -1 : 1;
});
return array_values($array);
}
$tree = [
[
"id" => 1,
"name" => "id1",
"children" => [
[
"id" => 2,
"parent_id" => 1,
"name" => "id2",
"children" => [
[
"id" => 5,
"parent_id" => 2,
"name" => "id5"
]
]
],
[
"id" => 3,
"parent_id" => 1,
"name" => "id3",
"children" => [
[
"id" => 6,
"parent_id" => 3,
"name" => "id6"
],
[
"id" => 8,
"parent_id" => 3,
"name" => "id8"
]
]
]
]
],
[
"id" => 4,
"name" => "id4",
"children" => [
[
"id" => 9,
"parent_id" => 4,
"name" => "id9",
"children" => [
[
"id" => 10,
"parent_id" => 9,
"name" => "id10"
]
]
]
]
],
[
"id" => 7,
"name" => "id7",
"children" => [
]
]
];
$array = flatTree($tree);
print_r($array);
UPDATE var_export version of the array HERE
I have the following array which I would like to group the elements/children by their UNILEVEL value:
array(
0 => array(
"member_id" => 3,
"unilevel" => 1,
"children" => array(
0 => array(
"member_id" => 4,
"unilevel" => 2,
"children" => array(
0 => array(
"member_id" => 6,
"unilevel" => 3,
"children" => array(
0 => array(
"member_id" => 7,
"unilevel" => 4 ) ) ) ) )
1 => array(
"member_id" => 9
"unilevel" => 2 ) ) )
1 => array(
"member_id" => 5,
"unilevel" => 1,
"children" => array(
0 => array(
"member_id" => 8,
"unilevel" => 2,
"children" => array(
0 => array(
"member_id" => 10,
"unilevel" => 3 ) ) ) ) ) )
The Controller below has a function named, groupPerlevel which does the grouping, but it only groups the first parent right now, it's already a recursive function, I am not sure why it's not putting the second parent's elements on the unilevel groupings.
class TestController extends Controller {
private $group = array();
private function setGroup($value) {
$this->group = $this->group + $value;
}
private function getGroup() {
return $this->group;
}
public function create()
{
$this->groupPerlevel($tree);
dd($this->getGroup());
}
private function groupPerlevel(array $items)
{
var_dump($items);
$grouparr = $this->getGroup();
$newkey = 0;
$templevel = 1;
foreach($items as $key => $val) {
if($templevel == $val->unilevel) {
$grouparr[$templevel][$newkey] = $val;
$this->setGroup($grouparr);
} else {
if(isset($grouparr[$val->unilevel][$newkey])) {
$count = count($grouparr[$val->unilevel]);
$grouparr[$val->unilevel][$count] = $val;
$this->setGroup($grouparr);
} else {
$grouparr[$val->unilevel][$newkey] = $val;
}
}
if(isset($val->children)) {
$children = $val->children;
unset($val->children);
$this->groupPerlevel($children);
}
$newkey++;
}
$this->setGroup($grouparr);
}
}
The following array would be my desired output. array(4) { [1] is the UNILEVEL (group) so all elements/children should be in their proper unilevel group based on their unilevel field value. But it only does that for the first parent, the second parent doesn't group.
array(
1 => array(
0 => array(
"member_id" => 3,
"unilevel" => 1 ) )
4 => array(
0 => array(
"member_id" => 7,
"unilevel" => 4 ) )
3 => array(
0 => array (
"member_id" => 6,
"unilevel" => 3 ) )
2 => array(
0 => array(
"member_id" => 4,
"unilevel" => 2
1 => array(
"member_id" => 9
"unilevel" => 2 )))
Sorry for late response try this
$arr=array("shiva" => array("member_id" => "3","unilevel" => "1","children" => array("0" => array("member_id" => "4","unilevel" => "2","children" => array("0" => array("member_id" => "6", "unilevel" => "3", "children" => array( "0" => array( "member_id" => "7", "unilevel" => "4" ) ) ) ) ), "1" => array( "member_id" => "9", "unilevel" => "2" ))),"1" => array("member_id" => "5","unilevel" => "1","children" => array("0" => array("member_id" => "8","unilevel" => "2", "children" => array( "0" => array( "member_id" => "10", "unilevel" => "3" ) ) ) ) ) );
function check($arr){
if(is_array($arr)){
foreach($arr as $arr1){
foreach($arr1 as $arr2){
if(is_array($arr2)){
return true;
}
}
}
}
else{
return false;
}
return false;
}
$i=0;
while(check($arr)){
foreach($arr as $arr1key=>$arr1val){
foreach($arr1val as $arr2key=>$arr2val){
if(is_array($arr2val)){
$arr[]=$arr[$arr1key][$arr2key];
unset($arr[$arr1key][$arr2key]);
}
}
}
}
foreach($arr as $arr1key=>$arr1val)
{
if(count($arr1val)==0){
unset($arr[$arr1key]);
}
}
//print_r($arr);
$result=array();
$values=array();
foreach($arr as $arr1key=>$arr1val)
{
if(((array_key_exists("unilevel", $arr1val)) || (array_key_exists("member_id", $arr1val)))==true)
{
$key=$arr1val['unilevel'];
$result[0][$key][]=array("unilevel"=>$arr1val["unilevel"],"member_id"=>$arr1val["member_id"]);
}
}
print_r($result);
Result:
Array ( [0] => Array ( [1] => Array ( [0] => Array ( [unilevel] => 1 [member_id] => 3 ) [1] => Array ( [unilevel] => 1 [member_id] => 5 ) ) [2] => Array ( [0] => Array ( [unilevel] => 2 [member_id] => 4 ) [1] => Array ( [unilevel] => 2 [member_id] => 9 ) [2] => Array ( [unilevel] => 2 [member_id] => 8 ) ) [3] => Array ( [0] => Array ( [unilevel] => 3 [member_id] => 6 ) [1] => Array ( [unilevel] => 3 [member_id] => 10 ) ) [4] => Array ( [0] => Array ( [unilevel] => 4 [member_id] => 7 ) ) ) )
$student_data = array(
array(
"id" => 1,
"student_name" => "Kasimir Clark",
"country" => "Chile"
),
array(
"id" => 2,
"student_name" => "Kyle Myers",
"country" => "Cook Islands"
),
array(
"id" => 3,
"student_name" => "Merrill Velez",
"country" => "Israel"
),
array(
"id" => 4,
"student_name" => "Kadeem Graham",
"country" => "Christmas Island"
),
);
usort($student_data, function($a, $b)
{
return $a["student_name"] - $b["student_name"];
});
I need to sort the multi-dimentional array in PHP. Is it possible to sort the country then by the name? I tried to achieve in usort but I can only sort by one condition such as either name or country.
I would like to achieve something like what MySQL database is doing.
Something like
SELECT * FROM STUDENT order by country,name
Thank you for your help.
Use array_multisort, I found an implementation in the comments.
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
$student_data = array(
array(
"id" => 1,
"student_name" => "Kasimir Clark",
"country" => "Chile"
),
array(
"id" => 2,
"student_name" => "Kyle Myers",
"country" => "Cook Islands"
),
array(
"id" => 3,
"student_name" => "Merrill Velez",
"country" => "Israel"
),
array(
"id" => 4,
"student_name" => "Kadeem Graham",
"country" => "Christmas Island"
)
);
$sorted = array_orderby($student_data, 'country', SORT_ASC, 'student_name', SORT_ASC);
print_r($sorted);
This print:
Array
(
[0] => Array
(
[id] => 1
[student_name] => Kasimir Clark
[country] => Chile
)
[1] => Array
(
[id] => 4
[student_name] => Kadeem Graham
[country] => Christmas Island
)
[2] => Array
(
[id] => 2
[student_name] => Kyle Myers
[country] => Cook Islands
)
[3] => Array
(
[id] => 3
[student_name] => Merrill Velez
[country] => Israel
)
)
I have array
$data = array(
0 => array( "id" => 2, "status" => 1),
1 => array( "name" => "you", "class" => "expert"),
);
I want result
$result = array(
"id" => 2,
"status" => 1,
"name" => "you",
"class" => "expert"
);
How to solved it ?
Here is a simple solution for this case:
$data = array(
0 => array( "id" => 2, "status" => 1),
1 => array( "name" => "you", "class" => "expert"),
);
$result = array_merge($data[0], $data[1]);
print_r($result);
Result:
Array
(
[id] => 2
[status] => 1
[name] => you
[class] => expert
)
You can simply use call_user_func_array function of PHP as
$result_array = call_user_func_array('array_merge', $data);
print_r($result_array);
Does any on know how can I convert this
$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);
into this
$events_processed = array(
"off-site" => array(
array(
"title" => "aaa",
"nid" => "11"
),
array(
"title" => "bbb",
"nid" => "22"
)
),
"installation" => array(
array(
"title" => "ccc",
"nid" => "33"
)
),
"opening" => array(
array(
"title" => "ddd",
"nid" => "44"
),
array(
"title" => "eee",
"nid" => "55"
),
array(
"title" => "fff",
"nid" => "66"
)
)
);
using php?
I've already tried to apply different methods from different posts here but with no success.
I need the array to be nested so I can reorder the array by "type".Hi
You can the code below, please note that your current output is not valid because you missed the array index ...
Example 1
$events_processed = array();
foreach($events as $options)
{
$events_processed[$options['type']][] = array("title"=>$options['title'],"nid"=>$options['nid']);
}
var_dump($events_processed);
OR
Example 2 (#dleiftah suggestion)
$defautKey = "type" ;
foreach($events as $options)
{
$type = $options[$defautKey] ;
unset($options[$defautKey]);
$events_processed[$type][] = $options;
}
var_dump($events_processed);
Both Result Would be Like this but number 2 is more flexible
array
'off-site' =>
array
0 =>
array
'title' => string 'aaa' (length=3)
'nid' => string '11' (length=2)
1 =>
array
'title' => string 'bbb' (length=3)
'nid' => string '22' (length=2)
'installation' =>
array
0 =>
array
'title' => string 'ccc' (length=3)
'nid' => string '33' (length=2)
'opening' =>
array
0 =>
array
'title' => string 'ddd' (length=3)
'nid' => string '44' (length=2)
1 =>
array
'title' => string 'eee' (length=3)
'nid' => string '55' (length=2)
2 =>
array
'title' => string 'fff' (length=3)
'nid' => string '66' (length=2)
Just loop through and reorganize ...
$result = array();
foreach($events as $event){
$key = $event['type'];
unset($event['type']);
$result[$key][] = $event;
}
print_r($result);
Here's what I did:
$array_processed = array();
foreach( $events as $evt){
$array_processed[$evt['type']][] = array('title'=>$evt['title'], 'nid'=>$evt['nid']);
}
print_r($array_processed);
There is no built in function that will do this. You will have to loop through this array and make a new array.
$events=array(contents-of-array);
$proc_events=array(
'off-site' => array(),
'installation' => array(),
'opening' => array()
);
foreach($events as $key => $value)
{
switch($value['type'])
{
case 'off-site':
$proc_events['offsite']=array('title' => $value['title'], 'nid' => $value['nid']);
break;
case 'installation':
$proc_events['installation']=array('title' => $value['title'], 'nid' => $value['nid']);
break;
case 'opening':
$proc_events['opening']=array('title' => $value['title'], 'nid' => $value['nid']);
break;
}
}
The above should work.
The shortest code to do this should be:
foreach ($events as $event)
$events_processed[$event['type']][] = array('title' => $event['title'], 'nid' => $event['nid']);
Here is what I was able to do:
<?php
$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);
foreach ($events as $event) {
$events_processed[$event['type']][] = array('title' => $event['title'],
'nid' => $event['nid']
);
}
echo '<pre>';
print_r($events_processed);
?>
This prints the following:
Array
(
[off-site] => Array
(
[0] => Array
(
[title] => aaa
[nid] => 11
)
[1] => Array
(
[title] => bbb
[nid] => 22
)
)
[installation] => Array
(
[0] => Array
(
[title] => ccc
[nid] => 33
)
)
[opening] => Array
(
[0] => Array
(
[title] => ddd
[nid] => 44
)
[1] => Array
(
[title] => eee
[nid] => 55
)
[2] => Array
(
[title] => fff
[nid] => 66
)
)
)