PHP: Find elements in deep array, then modify each parent element - php

I have a varying level of deepness for a multidimensional array. I want to find the elements where UserId isset (see example array below), then add a key/value pair ([show] => true) to the matched element and each parent array. There may be multiple matches that will need to modify the parents.
I have this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I want this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[show] => true //***Added
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I have been messing with multiple recursion functions that have failed me miserably.

https://ideone.com/HoJPry
function hasUserId (&$el) {
if(isset($el['children'])) {
$ret = false;
foreach($el['children'] as &$child) {
if(hasUserId($child)) {
$ret=true;
}
}
if($ret) {
$el['show']=true;
return true;
}
} ;
if (isset($el['user_id'])) {
$el['show']=true;
return true;
}
}

foreach($top as $i => $t)
{
foreach($t['children'] as $n => $mid)
{
foreach($mid['children'] as $x => $bottom)
{
if($bottom['id'] !== $target_id)
continue;
$top[$i]['show'] = true;
$top[$i]['children'][$n]['show'] = true;
$top[$i]['children'][$n]['children'][$x]['show'] = true;
}
}
}

Related

PHP Subtract multidimensional arrays based on 2 values

I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);

how to make incremental value in hierarchal array in Php?

Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...

convert this array format to single array in php

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);

recursive function to transform multidimentional array

I have an array after an SQL query made with cake PHP which returns me a tree. I do not the number of dimension of my array.
I want transform it to use it with jstree. I'm fighting with a recursive function and I didn't success.
Can you help me.
My original array looks like this:
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
And I want have something like this :
Array
(
[0] => Array
(
[text] => 1
[id] => 815
[children] => Array
(
[0] => Array
(
[text] => 2
[id] => 816
[children] => Array
(
[0] => Array
(
[text] => 3
[id] => 817
[children] => Array
(
[0] => Array
(
[text] => 4
[id] => 818
)
)
)
[1] => Array
(
[text] => 5
[id] => 819
)
)
)
)
)
)
I have try with a recursive function like this but I don't success
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element);
}
}
return $branch;
}
Edit :
After test and remarks my function is now
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element['children']);
}
}
return $branch;
}
When I am debuging, I can see that I go to my function for each child (what I want). But I don't know how to make the new array()
this worked for me...
$array = buildTree($array);
print_r($array);
function buildTree(array $parent) {
$branch = array();
foreach ($parent as $index => $element){
$node = array();
$node['id'] = $element['Confsave']['id'];
$node['text'] = isset($element['Confsave']['Name']) ?
$element['Confsave']['Name'] : 'no name';
$node['children'] = buildTree($element['children']);
$branch[] = $node;
}
return $branch;
}
my initial test array....
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 999
[Name] => 999
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 777
[Name] => 777
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 888
[Name] => 888
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
my result array....
Array
(
[0] => Array
(
[id] => 999
[text] => 999
[children] => Array
(
[0] => Array
(
[id] => 777
[text] => 777
[children] => Array
(
)
)
[1] => Array
(
[id] => 888
[text] => 888
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 815
[text] => 1
[children] => Array
(
[0] => Array
(
[id] => 816
[text] => 2
[children] => Array
(
[0] => Array
(
[id] => 817
[text] => no name
[children] => Array
(
[0] => Array
(
[id] => 818
[text] => 4
[children] => Array
(
)
)
[1] => Array
(
[id] => 819
[text] => 5
[children] => Array
(
)
)
)
)
)
)
)
)
)

Multidimensional array..... nested dropdown?

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>';?>

Categories