multi-dimensional array hierarchy - php

I like to know how can I get the hierarchy by the identifier.
This is an example :
$inputArray = array(
array(
"text" => "Dir1",
"parent_id" => "",
"id" => "1",
"filesize" => "109"
),array(
"text" => "dir2",
"parent_id" => "",
"id" => "2",
"filesize" => "88",
"children" => array(
"text" => "Dir3",
"parent_id" => "2",
"id" => "3",
"filesize" => "",
"children" => array(
"text" => "dir4",
"parent_id" => "3",
"id" => "4",
"filesize" => "",
"children" => array(
"text" => "dir5",
"parent_id" => "4",
"id" => "4",
"filesize" => ""
)
)
)
));
looking for this example :
dir3/dir4/dir5

function getText($array) {
$save[] = $array['text'];
if (isset($array['children'])) {
$save = array_merge($save, getText($array['children']));
}
return $save;
}
foreach($inputArray as $x) {
echo implode('/', getText($x)) . "\n";
}
result
Dir1
dir2/Dir3/dir4/dir5
demo

Related

I want to add up the corresponding prices

I want to reassemble the following array for each management_code, class_type.
$varArrayA = array(
array(
"manegement_code" => "1",
"class_type" => array('A','B','C')
),
array(
"manegement_code" => "2",
"class_type" => array('A','B','C')
),
array(
"manegement_code" => "3",
"class_type" => array('A','B','C')
)
);
$varArrayB = array(
array(
"manegement_code" => "1",
"class_type" => "A",
"price" => "200"
),
array(
"manegement_code" => "1",
"class_type" => "B",
"price" => "300"
),
array(
"manegement_code" => "1",
"class_type" => "B",
"price" => "300"
),
array(
"manegement_code" => "2",
"class_type" => "B",
"price" => "300"
)
);
I want this result
$varArrayB = array(
array(
"manegement_code" => "1",
"class_type" => "A",
"price" => "200"
),
array(
"manegement_code" => "1",
"class_type" => "B",
"price" => "600"
),
array(
"manegement_code" => "2",
"class_type" => "B",
"price" => "300"
)
);
I made it as follows.
I'm confused about incorporating conditions in class_type.
I would appreciate it if you could be help me.
foreach ($varArrayA as $i => $varValueA) {
foreach ($varArrayB as $rowow ){
if($rowow['manegement_code']==$varValueA['manegement_code']){
$varArrayC[$i]['manegement_code'] = $rowow['manegement_code'];
$varArrayC[$i]['class_type'] = $rowow['class_type'];
$num = $num+$rowow['price'];
$varArrayC[$i]['price'] = $num;
}
}
}
if(($rowow['manegement_code']==$varValueA['manegement_code']) && ($rowow['class_type']==$varValueA['class_type'])){
$varArrayC[$i]['manegement_code'] = $rowow['manegement_code'];
$varArrayC[$i]['class_type'] = $rowow['class_type'];
$num = $num+$rowow['price'];
$varArrayC[$i]['price'] = $num;
}
Just ask if the class_type is the same as well.

Add a dynamic key => value options in 3 level array option

I have an array as main options in my code.
In case, I want to add dynamic key => values into specific options array key.
This is my main options array:
$configarray1 = array(
"name" => "Addon",
"description" => "module for whmcs",
"version" => "1.1",
"author" => "Me",
"language" => "english",
"fields" => array(
"sender" => array (
"FriendlyName" => "Sender",
"Type" => "dropdown",
"Options" => strtolower($GatewaysIM),
"Description" => $getBalance,
"Default" => $Defaultsender,
),
"validateday" => array (
"FriendlyName" => "Days for Re-validation",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "90",
),
)
);
I want to add this sender array options in configarray1 fields key:
if($sender == 'sender1'){
$configarray2['fields'] = array(
"username" => array (
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"password" => array (
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
)
);
} elseif($sender == 'sender2'){
$configarray2['fields'] = array(
"line" => array (
"FriendlyName" => "line",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
)
);
}
Output array must be like this below when sender is sender1:
$configarray = array(
"name" => "Addon",
"description" => "module for whmcs",
"version" => "1.1",
"author" => "Me",
"language" => "english",
"fields" => array(
"sender" => array (
"FriendlyName" => "Sender",
"Type" => "dropdown",
"Options" => strtolower($GatewaysIM),
"Description" => $getBalance,
"Default" => $Defaultsender,
),
"username" => array (
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"password" => array (
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"validateday" => array (
"FriendlyName" => "Days for Re-validation",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "90",
),
)
);
I tested array push but this adds a key in arrays first place and not in 'fields' key, my code was this $configarray = array_push($configarray1,$configarray2); but this not works !
I also tested sum of two arrays ($configarray = $configarray1 + $configarray2) but this is the same as array_push and returns wrong output for me.
How can i resolve this problem ?!
You should just declare it. You don't need a new variable.
Assuming you have the original array named as $configarray1, just:
if($sender == 'sender1'){
$configarray1['fields']['username'] = array(
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
);
$configarray1['fields']['password'] => array(
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
);
} elseif($sender == 'sender2'){
$configarray1['fields']['line'] = array (
"FriendlyName" => "line",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
);
}

Array to repeat

$a = array(
array(
"name" => "jack",
"data" => "123",
"link_uid" => "1",
),
array(
"name" => "jack",
"data" => "134",
"link_uid" => "2",
),
array(
"name" => "tom",
"data" => "567",
"link_uid" => "3",
),
array(
"name" => "tom",
"data" => "098",
"link_uid" => "4",
)
);
to
$a = array(
array(
"name" => "jack",
"data" => "123",
"link_uid" => "1",
),
array(
"name" => " ",
"data" => "134",
"link_uid" => "2",
),
array(
"name" => "tom",
"data" => "567",
"link_uid" => "3",
),
array(
"name" => " ",
"data" => "098",
"link_uid" => "4",
)
);
Do you want just repeated name to blank
<?php
$temp = array();
foreach($a as $key=>$value){
if(in_array($value["name"],$temp)){
$a[$key]["name"] = "";
}else {
$temp[] = $value["name"];
}
}
print_r($a);
?>
Live demo : https://eval.in/855975

How to consolidate duplicate elements of this array in PHP?

I have an array like this:
$array = array(
0 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "memory"),
1 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "cpu"),
2 => array("ordernumber" => "1", "name" => "John", "product" => "desktop", "component" => "cpu"),
3 => array("ordernumber" => "2", "name" => "Pete", "product" => "monitor", "component" => "")
);
It contains data from different orders, but as you can see an order can contain multiple purchased products, and each product can contain different 'components'. There's alot of duplicate data in this array, so I would like to turn it into this:
$array = array(
0 => array(
"order" => array(
"ordernumber" => "1", "name" => "John"
),
"products" => array(
0 => array(
"name" => "laptop",
"components" => array("memory", "cpu")
),
1 => array(
"name" => "desktop",
"components" => array("cpu")
)
)
),
1 => array(
"order" => array(
"ordernumber" => "2", "name" => "Pete"
),
"products" => array(
0 => array(
"name" => "monitor",
"components" => array()
)
)
)
);
What would be a good way to do this?
Please use below code to make the solution what you want
<?php
$array = array(
0 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "memory"),
1 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "cpu"),
2 => array("ordernumber" => "1", "name" => "John", "product" => "desktop", "component" => "cpu"),
3 => array("ordernumber" => "2", "name" => "Pete", "product" => "monitor", "component" => "")
);
$final_array = [];
foreach($array as $k=>$v){
$final_array[$v['ordernumber']]['order']['ordernumber'] = $v['ordernumber'];
$final_array[$v['ordernumber']]['order']['name'] = $v['name'];
$final_array[$v['ordernumber']]['products'][$v['product']]['name'] = $v['product'];
$final_array[$v['ordernumber']]['products'][$v['product']]['components'][] = $v['component'];
}
// You can skip this foreach if there will not metter of KEY of an array in your code!
$final_array = array_values($final_array);
foreach($final_array as $k=>$v){
$final_array[$k]['products'] = array_values($final_array[$k]['products']);
}
echo "<pre>";
print_r($final_array);
?>
its should work!!

Array multisort not working for an array

$artists = [
0 => [
"id" => "3",
"plan_id" => "1",
"name" => "Artist-A",
"views" => "1189189",
"soundcloud" => "42",
"facebook" => "59881948",
"twitter" => "21760757",
"youtube" => 0,
"instagram" => "3429017"
],
1 => [
"id" => "10",
"plan_id" => "1",
"name" => "Artist-B",
"views" => "1",
"soundcloud" => 0,
"facebook" => 0,
"twitter" => 0,
"youtube" => 0,
"instagram" => 0
],
2 => [
"id" => "2",
"plan_id" => "1",
"name" => "Artist-C",
"views" => "1629",
"soundcloud" => "20",
"facebook" => "5025158",
"twitter" => "582899",
"youtube" => 0,
"instagram" => "112127"
],
3 => [
"id" => "4",
"plan_id" => "2",
"name" => "Artist-D",
"views" => "484353",
"soundcloud" => "7",
"facebook" => "104449606",
"twitter" => "36820201",
"youtube" => 0,
"instagram" => "16483226"
],
4 => [
"id" => "5",
"plan_id" => "2",
"name" => "Artist-E",
"views" => "98765432",
"soundcloud" => "13",
"facebook" => "59551072",
"twitter" => "38995648",
"youtube" => 0,
"instagram" => "64997436"
]
]
foreach ($remaining_artists as $key => $value) {
$soundcloud[$key] = $value['soundcloud'];
}
array_multisort($soundcloud, SORT_ASC, $artists);
I use array_multisort to sort array. It's working perfectly fine. But there is an error 'array_multisort(): Array sizes are inconsistent' for the above array. I really can't figure out what's the problem here and its solution.
Your array_multisort() parameters were out of order.
http://php.net/manual/en/function.array-multisort.php
Also, syntax problems.
This works
<?php
$artists = [
0 => [
"id" => "3",
"plan_id" => "1",
"name" => "Artist-A",
"views" => "1189189",
"soundcloud" => "42",
"facebook" => "59881948",
"twitter" => "21760757",
"youtube" => 0,
"instagram" => "3429017"
],
1 => [
"id" => "10",
"plan_id" => "1",
"name" => "Artist-B",
"views" => "1",
"soundcloud" => 0,
"facebook" => 0,
"twitter" => 0,
"youtube" => 0,
"instagram" => 0
],
2 => [
"id" => "2",
"plan_id" => "1",
"name" => "Artist-C",
"views" => "1629",
"soundcloud" => "20",
"facebook" => "5025158",
"twitter" => "582899",
"youtube" => 0,
"instagram" => "112127"
],
3 => [
"id" => "4",
"plan_id" => "2",
"name" => "Artist-D",
"views" => "484353",
"soundcloud" => "7",
"facebook" => "104449606",
"twitter" => "36820201",
"youtube" => 0,
"instagram" => "16483226"
],
4 => [
"id" => "5",
"plan_id" => "2",
"name" => "Artist-E",
"views" => "98765432",
"soundcloud" => "13",
"facebook" => "59551072",
"twitter" => "38995648",
"youtube" => 0,
"instagram" => "64997436"
]
];
$soundcloud = [];
foreach ($artists as $key => $value) {
$soundcloud[$key] = $value['soundcloud'];
}
array_multisort($soundcloud, $artists, SORT_ASC);
print_r($artists);

Categories