Arrange an array recursively - php

I'm trying to arrange an array in levels. This is my array:
Array
(
[0] => Array(
[0] => Array(
[id] => 971249312[name] => Wolverine
)
[children] => Array(
[0] => Array(
[0] => Array(
[id] => 735327624[name] => Ciclop
)
[children] => Array()
)
)
)
[1] => Array(
[0] => Array(
[id] => 1926833684[name] => Gambit
)
[children] => Array()
)
[2] => Array(
[0] => Array(
[id] => 51194629[name] => Quicksilver
)
[children] => Array()
)
)
See that the first position of array has 3 elements - this must be the level 0. The first position of these elements must be the level 1. The children of these elements are the next level and so on.
I canĀ“t figure out how to arrange it.
the expected output:
Array
(
["level_1"] => Array
(
[0] => Array
(
[id] => 971249312
[name] => Wolverine
)
[1] => Array
(
[id] => 1926833684
[name] => Gambit
)
[2] => Array
(
[id] => 51194629
[name] => Quicksilver
)
)
["level_2"] => Array
(
[0] => Array
(
[id] => 735327624
[name] => Ciclop
)
)
)

Another recursive tree walk.
I scan the tree 'depth first' so I need to keep track of the current 'level'.
Demonstration at eval.in
Tree scan routine:
/**
* Recursive scan of the tree
*
* #node array Current Node to be processed
* #level integer Current depth of the tree
* output array reference to where to store the details
*
* #return void
*/
function scanNode($node, $level, &$output)
{
$outLevelIdx = 'level_'. $level;
foreach ($node as $idx => $info) {
$parent = current($info);
$output[$outLevelIdx][] = array('id' => $parent['id'], 'name' => $parent['name']);
if (!empty($info['children'])) { // go scan the children
scanNode($info['children'], $level + 1, $output);
}
}
}
Run the scan:
/*
* Output array in here - pass as a reference
*/
$output = array();
// scan the full tree
scanNode($source, 0, $output);
Sample output:
output
Array
(
[level_0] => Array
(
[0] => Array
(
[id] => 971249312
[name] => Wolverine
)
[1] => Array
(
[id] => 1926833684
[name] => Gambit
)
[2] => Array
(
[id] => 51194629
[name] => Quicksilver
)
)
[level_1] => Array
(
[0] => Array
(
[id] => 735327624
[name] => Ciclop
)
)
)

If your desired output is
Array
(
[0] => Array
(
[id] => 971249312
[name] => Wolverine
[children] => Array
(
)
)
[1] => Array
(
[id] => 971249312
[name] => Wolverine
[children] => Array
(
)
)
[2] => Array
(
[id] => 971249312
[name] => Wolverine
[children] => Array
(
)
)
)
Then your code should be
$newArray = [];
foreach ($givenArray as $key => $value) {
$newArray[$key]['id'] = $value[0]['id'];
$newArray[$key]['name'] = $value[0]['name'];
$newArray[$key]['children'] = $value['children'];
}
AS per your desired output
This function used to scan all the node and provide as per your requirment.
OUTPUT
$newArray = [];
myfunction($a, 0,$newArray);
function myfunction($loop, $level, &$newArray) {
$index = "level_".$level;
$i = 0;
foreach ($loop as $key => $value) {
foreach ($value as $key1 => $value1) {
if($key1 !== 'children'){
$newArray[$index][$i] = ['id' => $value1['id'], 'name' => $value1['name']];
$i++;
}
}
if (isset($value['children']) && !empty($value['children'])) {
myfunction($value['children'], $level + 1, $newArray);
}
}
}
print_r($newArray);exit;

Related

Multidimensional arrays, remove the duplicated array in next array

My arrays result is like this one
Array
(
[0] => Array
(
[id] => Bank Transfer
[ec] => 1000
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
)
)
[1] => Array
(
[id] => Wired
[ec] => 1001
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
[2] => Array
(
[name] => Account ID
[value] =>
)
)
)
)
It's weird because 2nd array of accounts contains same value as first array.
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
How to prevent this duplicated so the 2nd array of accounts will only show
[0] => Array
(
[name] => Account ID
[value] =>
)
Here's my code
$arr = $arr_pay = array();
foreach($site_payment as $key => $value){
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
Array of $site_payment contains
[Bank Transfer] => Array
(
[id] => Bank Transfer
[ec] => 1000
[status] => 1
[auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
)
[Dana] => Array
(
[id] => Wired
[ec] => 1001
[status] => 1
[auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
)
I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array
Please help me to solve the problem
Duplication is due to the $arr is not being reset
$arr_pay = array();
foreach($site_payment as $key => $value){
$arr = array(); // Resetting
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}

Combine some php sub-arrays, with condition

$routes array:
[admin/login] => Array
(
[0] => POST|AJAX
[1] => admin/login
[2] => admin/login
)
[admin/logout] => Array
(
[0] => POST
[1] => admin/logout
[2] => admin/logout
)
[admin/dashboard] => Array
(
[0] => GET
[1] => admin/dashboard
[2] => admin/dashboard
)
[admin/products] => Array
(
[0] => GET
[1] => admin/products/[:pag]
[2] => admin/products/all
)
[admin/products/add] => Array
(
[0] => GET
[1] => admin/product/add
[2] => admin/products/add
)
[admin/products/insert] => Array
(
[0] => POST
[1] => admin/product/insert
[2] => admin/products/insert
)
[admin/products/show] => Array
(
[0] => GET
[1] => admin/product/[i:id]
[2] => admin/products/show
)
[admin/products/edit] => Array
(
[0] => GET
[1] => admin/product/[i:id]/edit
[2] => admin/products/edit
)
[admin/products/update] => Array
(
[0] => POST
[1] => admin/product/update
[2] => admin/products/update
)
[admin/products/delete] => Array
(
[0] => POST
[1] => admin/product/delete
[2] => admin/products/delete
)
[admin/categories] => Array
(
[0] => GET
[1] => admin/categories
[2] => admin/categories/all
)
[admin/categories/add] => Array
(
[0] => GET
[1] => admin/category/add
[2] => admin/categories/add
)
[admin/categories/insert] => Array
(
[0] => POST
[1] => admin/category/insert
[2] => admin/categories/insert
)
[admin/categories/show] => Array
(
[0] => GET
[1] => admin/category/[i:id]
[2] => admin/categories/show
)
[admin/categories/edit] => Array
(
[0] => GET
[1] => admin/category/[i:id]/edit
[2] => admin/categories/edit
)
[admin/categories/update] => Array
(
[0] => POST
[1] => admin/category/update
[2] => admin/categories/update
)
[admin/categories/delete] => Array
(
[0] => POST
[1] => admin/category/delete
[2] => admin/categories/delete
)
[admin/api] => Array
(
[0] => GET
[1] => admin/api
[2] => admin/api/all
)
[admin/filter/sessions] => Array
(
[0] => POST|AJAX
[1] => admin/filter/sessions
[2] => admin/filters/sessions
)
This is $admin_routes array (build from $routes):
[login] => Array
(
[0] => admin/login
)
[logout] => Array
(
[0] => admin/logout
)
[dashboard] => Array
(
[0] => admin/dashboard
)
[products] => Array
(
[0] => admin/products
[1] => admin/products/add
[2] => admin/products/insert
[3] => admin/products/show
[4] => admin/products/edit
[5] => admin/products/update
[6] => admin/products/delete
)
[api] => Array
(
[0] => admin/api
)
[filter] => Array
(
[0] => admin/filter/sessions
)
[categories] => Array
(
[0] => admin/categories
[1] => admin/categories/add
[2] => admin/categories/insert
[3] => admin/categories/show
[4] => admin/categories/edit
[5] => admin/categories/update
[6] => admin/categories/delete
)
I need some help from you guys about $admin_routes.
I want to combine all sub-arrays from $admin_routes, from same area, which has only one element, in a new sub-array with key name formed from their key names.
Also i want the new sub-array to have same 'position' in line.
My imperfect solution:
// creating $admin_routes
$admin_routes = [];
foreach ($routes as $name => $route) {
if (preg_match('/^admin\/(.*)$/', $name))
$admin_routes[explode('/', $name)[1]][] = $name;
}
// imperfect solution for what i want
$single = false;
$waiting = [];
foreach ($admin_routes as $section => $routes) {
if (count($routes) == 1) { // remember all sub-arrays, with one single element, from same area
$single = true;
$waiting[$section] = $routes;
}
else if ($single) { // if found all sub-arrays, with one single element, from same area
$name = '';
$subarray = [];
foreach ($waiting as $section => $routes) {
// creating only one sub-array
$name .= ($section . ' ');
$subarray = array_merge($subarray, $routes);
unset($admin_routes[$section]);
}
// the problem is, sub-array it's added at the end of $admin_routes
// (not where sub-array's elements were before)
$admin_routes[$name] = $subarray;
$single = false;
$waiting = [];
}
}
The problem is, the new sub-arrays are placed at the end.
If you have any idea, please help me. Thx.
Using your actual result $admin_routes, you could loop through them and rebuild the desired array:
$real_routes = []; // The new array
$singles_key = ''; // The key for combined sub-arrays
$singles_routes = []; // The routes for combined sub-arrays
foreach ($admin_routes as $name => $r) {
/* If this route array has a single entry,
add its name and value to our temp vars */
if (count($r) === 1) {
$singles_key .= $name . ' ';
$singles_routes = array_merge($singles_routes, $r);
} else {
/* If then a route array has multiple value,
save the combined singles and then save this one */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
$singles_key = '';
$singles_routes = [];
}
$real_routes[$name] = $r;
}
}
/* If the loop ends but the temp vars aren't empty,
save the lasts combined singles */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
}
// Then to output:
echo '<pre>' . print_r($real_routes, true) . '</pre>';

How can I create a function to display an array as a tree?

How can I create a function to display an array as a tree. For example I want to obtain a decision tree which I want to walk until I get to the leafs based on the branch's values. I create the tree like bellow:
$tree= new DS_Tree();
$node=array('name' => 'start');
$tree->insert_node($node);
$tree->goto_root();
$mytree = new id3();
$mytree->init($data_array_AttrList,$data_array_values,$data_class,$data_array_instances,$tree);
$mytree->run();
echo '<pre class="brush: php">';
print_r($mytree->tree->draw_tree());
echo '</pre>';
The function draw_tree() is:
public function draw_tree() {
return $this->nodes;
}
The function that creates my tree is:
private function make_tree($attr) {
foreach($this->Values[$attr] as $v) {
$subset = $this->get_subset($attr, $v);
if($the_class = $this->has_same_class($subset)) {
$node =array(
'name' => $attr,
'arc' => $v
);
$this->tree->insert_node($node);
$this->Instance = array_diff_key($this->Instance, $subset);
} else {
$node =array(
'name' => $this->Classa,
'arc' => $v
);
$unresolved = $this->tree->insert_node($node);
}
}
if (isset($unresolved)) {
$this->tree->goto_index($unresolved);
}
}
}
The result is:
Array
(
[0] => Array
(
[name] => Time
[parent] =>
[children] => Array
(
[0] => 1
)
)
[1] => Array
(
[name] => Focus
[arc] => Array
(
[0] => 2 day/week
[1] => 3 day/week
[2] => 4 day/week
[3] => 5 day/week
[4] => 6 day/week
)
[parent] => 0
[children] => Array
(
[0] => 2
)
)
[2] => Array
(
[name] => Dificulty
[arc] => Array
(
[0] => Weght loss
[1] => Mantain weight
[2] => Gain Mass
)
[parent] => 1
[children] => Array
(
[0] => 3
)
)
[3] => Array
(
[name] => Sex
[arc] => Array
(
[0] => Beginner
[1] => Intermediar
[2] => Advance
)
[parent] => 2
[children] => Array
(
[0] => 4
)
)
[4] => Array
(
[name] => Array
(
[Exercise] => Array
(
[0] => Array
(
[0] => Ex1
[1] => Ex2
[2] => Ex3
[3] => Ex4
[4] => Ex5
)
)
)
[arc] => Array
(
[0] => F
[1] => M
)
[parent] => 3
)
)
Just to display an array as a tree:
echo "<pre>";
var_dump($array);
echo "</pre>";
Here is a way to iterate through this data structure and look for a certain value:
function recurseFind($tree, $find, $path = "") {
if (!is_array($tree)) {
// it is a leaf:
if ($tree === $find) {
return $path; // return path where we found it
}
return false;
}
foreach($tree as $key => $value) {
$result = recurseFind($value, $find, $path . (is_numeric($key) ? "[$key]" : "['$key']"));
if ($result !== false) return $result;
}
return false;
}
For the sample input you provided, if you would call it like this:
echo recurseFind($tree, "Mantain weight", '$tree');
Outputs the "location" of that value (first match only) in a format that can be evaluated in PHP:
$tree[2]['arc'][1]

How to merge array field values in php?

My array is like this:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord
)
[1] => Array
(
[id] => 2
[name] => b
[hardware_type] => mouse
)
[2] => Array
(
[id] => 1
[name] => a
[hardware_type] => mouse
)
[3] => Array
(
[id] => 1
[name] => a
[hardware_type] => moniter
)
[4] => Array
(
[id] => 2
[name] =>b
[hardware_type] => keyboad
)
)
required out put like this i want only merge hardware type
Array(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord, mouse, moniter
)
[1] => Array
(
[id] => 1
[name] => b
[hardware_type] => keyboard, mouse
)
)
Where $array is the input array you described, where $newarray is the output array you desire, and assuming every value of id has the same name as in your example input:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
if (empty($newarray[$item['id']]['hardware_type']))
$temp[$item['id']]['hardware_type'] = $item['hardware_type'];
else
$temp[$item['id']]['hardware_type'] .= ', ' . $item['hardware_type'];
}
$newarray = array_values($temp);
If you want the hardware_type to be an array instead of a comma-separated list of strings, do this instead:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
$temp[$item['id']]['hardware_type'][] = $item['hardware_type'];
}
$newarray = array_values($temp);

traversing array, which can be of multiple depth

I have following array pattern.
Array
(
[0] => Array
(
[label] => 2011
[subtable] => Array
(
[0] => Array
(
[label] => 05
[subtable] => Array
(
[0] => Array
(
[label] => /hello-world.html
[url] => http://example.com/2011/05/hello-world.html
)
[1] => Array
(
[label] => /test-test-test.html
[url] => http://example.com/2011/05/test-test-test.html
)
)
)
[1] => Array
(
[label] => 04
[subtable] => Array
(
[0] => Array
(
[label] => /i-have-some-problem-with-gfc-updates.html
[url] => http://example.com/2011/04/i-have-some-problem-with-gfc-updates.html
)
[1] => Array
(
[label] => /testing-for-google-frineds-update.html
[url] => http://example.com/2011/04/testing-for-google-frineds-update.html
)
)
)
[2] => Array
(
[label] => /05
[url] => http://example.com/2011/05
)
[3] => Array
(
[label] => /04
[url] => http://example.com/2011/04
)
)
)
[1] => Array
(
[label] => /index
[url] => http://example.com/
)
)
I want to get "label" and "url" into another array like
array(
[0] = array([label] => a [url] => http://example.com/a/)
[1] = array([label] => b [url] => http://example.com/b/)
)
How can I achieve it using recursion, or by passing it to a function?
You may use array_walk_recursive for this:
$parsed_array = array();
function parse_array($item, $key) {
global $parsed_array;
if(is_array($item) && isset($item['url'])) {
$parsed_array[] = $item;
}
}
array_walk_recursive($your_array, 'parse_array');
Or you may implement it using a recursive function:
function parse_array($array) {
$result = array();
foreach($array as $item) {
if(isset($item['subtable'])) {
$result = array_merge($result, parse_array($item['subtable']));
}else{
$result[] = $item;
}
}
return $result;
}
$parsed_array = parse_array($your_array);

Categories