Comparing two arrays php with a third array - php

I have two arrays with the year's results.
array A (
[a] => '150'
[b] => '200'
[c] => '300'
[d] => '1000'
[e] => '350'
[f] => '1000'
)
array B (
[a] => '500'
[b] => '400'
[d] => '1000'
[f] => '1000'
)
I need to compare the growth results between the two building another array, to show it in a html table. Ex:
[a] => 233%
[b] => 100%
...
I have a array identifying the indexes that are not present on array b.
array c = ('c', 'e');
The thing is, I need the row C and E to still be displayed on the table. But on the iteration, how can i just jump the line with this indexes that have 0 value avoiding calculation 300 by 0 and putting a message instead?

You can iterate the first array and check the next one values:
$arrayA = [
'a' => '150',
'b' => '200',
'c' => '300',
'd' => '1000',
'e' => '350',
'f' => '1000',
];
$arrayB = [
'a' => '500',
'b' => '400',
'd' => '1000',
'f' => '1000',
];
$result = [];
foreach ($arrayA as $key => $value) {
if(isset($arrayB[$key])) {
$result[$key] = round($arrayB[$key] * 100 / $value, 2);
} else {
$result[$key] = 'some value when empty';
}
}
var_dump($result);
Output:
array(6) {
["a"]=>
float(333.33)
["b"]=>
float(200)
["c"]=>
string(21) "some value when empty"
["d"]=>
float(100)
["e"]=>
string(21) "some value when empty"
["f"]=>
float(100)
}

You could loop through array A and check if the key of array A exists in array B using array_key_exists and then calculate the growth percentage:
$arrayA = [
'a' => '150',
'b' => '200',
'c' => '300',
'd' => '1000',
'e' => '350',
'f' => '1000'
];
$arrayB = [
'a' => '500',
'b' => '400',
'd' => '1000',
'f' => '1000'
];
$arrayC = [];
foreach($arrayA as $keyA => $valueA) {
if (array_key_exists($keyA, $arrayB)) {
$arrayC[$keyA] = floor((($arrayB[$keyA] - $valueA) / $valueA ) * 100) . "%";
continue;
}
$arrayC[$keyA] = "No match";
}
Result
Array
(
[a] => 233%
[b] => 100%
[c] => No match
[d] => 0%
[e] => No match
[f] => 0%
)
Demo

If you want a more flexible solution where you can check for both values of the two arrays (i.e. do not base the comparison keys on a single array) and have the possibility to expand it for more than two arrays (it may be useful to others who do not have your same goal).
Fetch the keys of the two array with array_keys and use array_unique in order to avoid duplicate keys values.
<?php
$array_a = [
'a' => 150,
'b' => 200,
'c' => 300,
'd' => 1000,
'e' => 350,
'f' => 1000
];
$array_b = [
'a' => 500,
'b' => 400,
'd' => 1000,
'f' => 1000
];
$keys_a = array_keys($array_a);
$keys_b = array_keys($array_b);
$keys = array_unique(array_merge($keys_a, $keys_b));
$result = [];
foreach ($keys as $key)
{
if (isset($array_a[$key]) && isset($array_b[$key]))
{
$result[$key] = round((($array_b[$key] - $array_a[$key]) / $array_a[$key]) * 100);
}
else
{
$result[$key] = "missing key in one of the two arrays";
}
}
Output:
Array
(
[a] => 233
[b] => 100
[c] => missing key in one of the two arrays
[d] => 0
[e] => missing key in one of the two arrays
[f] => 0
)

Related

Convert array values to an array

I can't figure out how to effectively convert the values of this array from a string to array. I really appreciate any suggestion.
array(6) {
["A"]=>
string(31) "['B' => 3, 'C' => 5, 'D' => 9],"
["B"]=>
string(41) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],"
["C"]=>
string(51) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],"
["D"]=>
string(51) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],"
["E"]=>
string(41) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],"
["F"]=>
string(31) "['C' => 3, 'D' => 2, 'E' => 5],"
}
Desired output:
$graph = [
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
];
Seems like you're trying to convert array string to an array.
You can repeat through loop or make function to get your desired output.
I'm using regular expression with preg_match_all
Code
$rawArray = array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
"B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
"C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
"D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
"E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
"F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
);
foreach($rawArray as $k => $v){
preg_match_all("/\'(.)\'/", $v, $key);
preg_match_all("/=> (\d)/", $v, $val);
$graph[$k] = array_combine($key[1], $val[1]);
}
print_r($graph);
Output
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
Live demo
Explanation:
$rawArray is associate array, each of it's element contain string similar to php array.
We're looping through array and converting that string to array by using preg_match_all and building $graph multidimension array.
When loop execute first time $k is equal to A and $v is equal to ['B' => 3, 'C' => 5, 'D' => 9],
First preg_match_all make array of keys from $v (['B' => 3, 'C' => 5, 'D' => 9],), and assign it to $key[1]. Now $key[1] is array ['B', 'C', 'D'].
Second preg_match_all make array of values from $v (['B' => 3, 'C' => 5, 'D' => 9],), and assign it to $val[1]. Now $val[1] is array [2, 5, 9].
We're combining$key[1]as keys and $val[1] as values by using array_combine to the $graph[$k] where $k is A.
How preg_match_all works?
preg_match_all($pattern, $string, $out);
It's matches pattern from string and then assign result to the $out as array.
Learn more about.
preg_match_all
regex pattern cheat sheet
Note: We're using non-capturing pattern so, it's return both exact match and desired match... So our desired record found in$key[1].
This is how you can do it,
<?php
$graph = array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
"B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
"C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
"D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
"E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
"F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
);
foreach ($graph as $key => $value) {
$val = str_replace("[","{",$value);
$val = str_replace("]","}",$val);
$val = str_replace("'",'"',$val);
$val = str_replace("=>",":",$val);
$val = rtrim($val, ',');
$graph[$key] = json_decode($val, true);
}
echo "<pre>";
print_r($graph);
echo "</pre>";
Output
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
A little ugly but I think this finally does the trick.
I downloaded your file and ran this locally so that the source is exactly as you stated. Then I proceeded to parse it and convert the string value to an actual array
Here's how it looks now:
// Parse graph.json file
$json = json_decode(file_get_contents('graph.json'), true);
foreach ($json as $key => $value) {
foreach ($value as $k => $val) {
$str = str_replace(['[', ']'], '', $val);
$str = str_replace(' => ', ',', $str);
$str = str_replace("'", "", $str);
$str = explode(',', $str);
for ($x = 0; $x < count($str); $x = $x + 2) {
$graph[$k][trim($str[$x])] = $str[$x+1];
}
}
}
// Result
echo "<pre>";
print_r($graph);
// Proof it is an array now (result 3)
// echo '<pre>';
// print_r($graph['A']['B']);
Final Result:
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
If you run the below which is your expected output example and then compare the output to my output it is identical:
$graph = [
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
];
echo '<pre>';
print_r($graph);
The proper answer is: don't create such a strange array ;) But since you did, this should do the trick:
//or $graph instead of $result
$result = array_map(function($value) {
//use eval to directly evaluate the string
//we just need to remove the trailing comma
//and add a semicolon
eval('$ret = '.rtrim($value,',').';');
return($ret);
}, $array); // replace $array with the var name of your array!
But remember: eval is evil. If you don't trust the input you need to write your own parser.
Temporary edit for clarification. This is what I get when I run your github example trough json_decode(...,true):
array(6) {
[0]=>
array(1) {
["A"]=>
string(30) "['B' => 3, 'C' => 5, 'D' => 9]"
}
[1]=>
array(1) {
["B"]=>
string(40) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7]"
}
[2]=>
array(1) {
["C"]=>
string(50) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3]"
}
[3]=>
array(1) {
["D"]=>
string(50) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2]"
}
[4]=>
array(1) {
["E"]=>
string(40) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5]"
}
[5]=>
array(1) {
["F"]=>
string(30) "['C' => 3, 'D' => 2, 'E' => 5]"
}
}
This differs from your question.

PHP array sorting not working as expected

Below is an output of my array
$array1 = Array ( [d] => 5 [e] => 1 [a] => 3 [b] => 3 [c] => 3 [f] => 3 )
I want to sort it like...
Array ( [d] => 5 [a] => 3 [b] => 3 [c] => 3 [f] => 3 [e] => 1)
I am using arsort($array1)
which results in var_dump($array1)
array (size=6)
'd' => int 5
'f' => int 3
'c' => int 3
'a' => int 3
'b' => int 3
'e' => int 1
anyways to fix this?
Try this :
$array1 = [
'd' => 5,
'e' => 1,
'a' => 3,
'b' => 3,
'c' => 3,
'f' => 3
];
array_multisort(array_values($array1), SORT_DESC, array_keys($array1), SORT_ASC, $array1);
print_r($array1);
Here first array_values($array1), SORT_DESC will sort the values in descending order and then array_keys($array1), SORT_ASC will sort the keys into ascending order and finally both the thing applies to the main array i.e. $array1.
O/P - Array ( [d] => 5 [a] => 3 [b] => 3 [c] => 3 [f] => 3 [e] => 1 )
I hope this time I get what you want. Finger crossed !!!
you can work like this its working.
<?php
$array1 = array( "[d]" => 5,"[e]" => 1,"[a]" => 3,"[b]" => 3,"[c]" => 3,"[f]" => 3 );
$a = arsort($array1);
foreach($array1 as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
output:
Key=[d], Value=5
Key=[f], Value=3
Key=[c], Value=3
Key=[a], Value=3
Key=[b], Value=3
Key=[e], Value=1
You can use uasort for this.
$array = array('d' => 5, 'e' => 1, 'a' => 3, 'b' => 3, 'c' => 3, 'f' => 3);
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
uasort($array, 'cmp');
print_r($array);
I tested the code and surely it will work for you.

Pushing multidimensional arrays as keys on another array

I have an empty array (as an example) and a number of multidimensional arrays I would like to push onto this array. However I would like the keys of each multidimensional array to become a key of the empty array
Using
$myEmptyArray[] = $arrayOne;
$myEmptyArray[] = $arrayTwo;
$myEmptyArray[] = $arrayThree;
I get
[
0 => ['one' => ['a' => 'stuff']],
1 => ['two' => ['b' => 'stuff']],
2 => ['three' => ['c' => 'stuff']]
]
I would prefer to have
[
'one' => ['a' => 'stuff'],
'two' => ['b' => 'stuff'],
'three' => ['c' => 'stuff']
]
What is a neat and compact way (one liner or native php function) to do this without having to read the array key with a foreach and then assign this key explicitly to the empty array with the value like
foreach ($arrayOne as $key => $value) {
$myEmptyArray[$key] = $value
}
As I will want to use this in many places in my code
You can use the + (union) operator which "returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored".
$array = [];
$array += ['one' => ['a' => 'stuff']];
$array += ['two' => ['b' => 'stuff']];
$array += ['three' => ['b' => 'stuff']];
var_dump($array);
/*
array(3) {
'one' =>
array(1) {
'a' =>
string(5) "stuff"
}
'two' =>
array(1) {
'b' =>
string(5) "stuff"
}
'three' =>
array(1) {
'b' =>
string(5) "stuff"
}
}
*/
You can simply use + to combine those array in the way you want:
$arrayOne = array('one' => array('a' => 'stuff'));
$arrayTwo = array('two' => array('b' => 'stuff'));
$arrayThree = array('three' => array('c' => 'stuff'));
$myEmptyArray = array();
$myEmptyArray += $arrayOne;
$myEmptyArray += $arrayTwo;
$myEmptyArray += $arrayThree;
echo "<pre>"; print_r($myEmptyArray); echo "</pre>";
OUTPUT:
Array
(
[one] => Array
(
[a] => stuff
)
[two] => Array
(
[b] => stuff
)
[three] => Array
(
[c] => stuff
)
)

Multi-dimensional array, remove array where key and value match another array

I want to remove duplicates where either measurement or altunit matches another array, but ignoring if they're blank.
Array
(
[0] => Array
(
[id] => 2
[altunit] => %
[measurement] =>
)
[1] => Array
(
[id] => 3
[altunit] =>
[measurement] => 6
)
[2] => Array
(
[id] => 4
[altunit] => %
[measurement] =>
)
[3] => Array
(
[id] => 5
[altunit] =>
[measurement] => 6
)
[4] => Array
(
[id] => 6
[altunit] =>
[measurement] => 6
)
)
Becomes
Array
(
[0] => Array
(
[id] => 2
[altunit] => %
[measurement] =>
)
[1] => Array
(
[id] => 3
[altunit] =>
[measurement] => 6
)
)
Best I can come up with is:
$test = array ( 0 => array ( 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ), 1 => array ( 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ), 2 => array ( 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ), 3 => array ( 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ), 4 => array ( 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ), );
$num = [];
foreach($test as $k => $v) $num[] = $v['measurement'];
But this only works for measurement, and removes the id and altunit keys.
Humm,
Make an array of 'knowed value' for measurement and altunit and then check it it exist on the rest of the values.
something like:
$knowed_altunit=array();
$knowed_measurement=array();
foreach($test as $k=>$v){
if(in_array($v['altunit'],$knowed_altunit)
|| in_array($v['mesurement'],$knowed_measurement)){
//if the value of altunit or measurement is already knowed then remove the entry from the array,
unset($test[$k]);
}else{
//if it never been seen, add it so further entry can be checked agaisnt the knowed value
$knowed_altunit[]=$v['altunit'];
$knowed_measurement[]=$v['mesurement'];
}
}
Sorry if any typo but thins might help you wrap your head around the solution to your problem.
You can try this code:
<?php
/* before you need to check that $test variable is declarated and have all items to check */
$values_altunit = array();
$values_measurement = array();
$result = array();
foreach($test as $key => $value) {
/* check if exist altunit and measurement values in blacklist arrays */
if (!in_array($value['altunit'], $values_altunit) && !in_array($value['measurement'], $values_measurement)) {
/* if not exist, add the item to $result array */
$result[$key] = $value;
/* and add altunit and measurement values to blacklist arrays */
$values_altunit[] = $value['altunit'];
$values_measurement[] = $value['measurement'];
}
}
/* print result items */
var_dump($result);
?>
This is a short code that can give the result for unique measurements
<?php
$arr = array(
"0"=> array (
"id" => 2,
"altunit" => "%",
"measurement" => "",
),
"1"=> array (
"id" => 3,
"altunit" => "",
"measurement" => 6,
),
"2"=> array (
"id" => 4,
"altunit" => "%",
"measurement" => "",
),
"3"=> array (
"id" => 5,
"altunit" => "",
"measurement" => 6,
),
"4"=> array (
"id" => 6,
"altunit" => "",
"measurement" => 6,
)
);
$unique_measure = $new_array = array();
foreach($arr as $sup_key => $sup_val){
foreach($sup_val as $sub_key => $sub_val){
if(!in_array($sup_val['measurement'], $unique_measure)){
array_push($unique_measure, $sup_val['measurement']);
array_push($new_array,$sup_val);
}
}
}
print_r($new_array);
?>
Output :
Array
(
[0] => Array
(
[id] => 2
[altunit] => %
[measurement] =>
)
[1] => Array
(
[id] => 3
[altunit] =>
[measurement] => 6
)
)
try this code. it may do the trick.
One More approach to your problems solution can be removing that particular key from the main array using unset(your_array_key) will do the trick in the same code.
Try, this may help you
function remove_dup($array, $keys )
{
$out = array();
foreach($array as $sub)
{
if(empty($out))
{
$out[] = $sub;
continue;
}
foreach($keys as $key)
{
if($flag=in_array( $sub[$key],array_map(function($e) use($key){ return $e[$key];}, $out)) )
break;
}
if(!$flag)
$out[] = $sub;
}
return $out;
}
// Usage
print_r( remove_dup($array, array('altunit','measurement') ) );
Test
[akshay#localhost tmp]$ cat test.php
<?php
function remove_dup($array, $keys )
{
$out = array();
foreach($array as $sub)
{
if(empty($out))
{
$out[] = $sub;
continue;
}
foreach($keys as $key)
{
if($flag=in_array( $sub[$key],array_map(function($e) use($key){ return $e[$key];}, $out)) )
break;
}
if(!$flag)
$out[] = $sub;
}
return $out;
}
$array = array(
"0"=> array (
"id" => 2,
"altunit" => "%",
"measurement" => "",
),
"1"=> array (
"id" => 3,
"altunit" => "",
"measurement" => 6,
),
"2"=> array (
"id" => 4,
"altunit" => "%",
"measurement" => "",
),
"3"=> array (
"id" => 5,
"altunit" => "",
"measurement" => 4,
),
"4"=> array (
"id" => 6,
"altunit" => "",
"measurement" => 6,
)
);
print_r( remove_dup($array, array('altunit','measurement') ) );
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => Array
(
[id] => 2
[altunit] => %
[measurement] =>
)
[1] => Array
(
[id] => 3
[altunit] =>
[measurement] => 6
)
)
What you really want is a set, not an array. So if you can't fix the way you're building the array in the first place (my guess is this came from an SQL query, which would be a lot less code to rectify) you have two options for creating mapped sets in PHP.
You could use SplObjectStorage
You could use an Array with the key as a serialized representation of the set
The first approach would look something like this...
$set = new SplObjectStorage();
$arr = [
0 => [
'id' => '2',
'altunit' => '%',
'measurement' => NULL,
],
1 => [
'id' => '3',
'altunit' => NULL,
'measurement' => '6',
],
2 => [
'id' => '4',
'altunit' => NULL,
'measurement' => '6',
],
3 => [
'id' => '5',
'altunit' => NULL,
'measurement' => '6',
],
4 => [
'id' => '6',
'altunit' => NULL,
'measurement' => '6',
],
];
foreach($arr as $part) {
if (isset($part['altunit'])) { // ignore if blank
$key = (object) $part;
$set->attach($key); // attach the set
}
}
// Now you have...
foreach($set as $value) {
var_dump($value);
}
This would give you...
object(stdClass)#2 (3) {
["id"]=>
string(1) "2"
["altunit"]=>
string(1) "%"
["measurement"]=>
NULL
}
The second approach, using an array, where the array key represents a serialized version of the set would look something like this...
$set = [];
$arr = [
0 => [
'id' => '2',
'altunit' => '%',
'measurement' => NULL,
],
1 => [
'id' => '3',
'altunit' => NULL,
'measurement' => '6',
],
2 => [
'id' => '4',
'altunit' => NULL,
'measurement' => '6',
],
3 => [
'id' => '5',
'altunit' => NULL,
'measurement' => '6',
],
4 => [
'id' => '6',
'altunit' => NULL,
'measurement' => '6',
],
];
foreach($arr as $part) {
if (isset($part['altunit'])) { // ignore if blank
$key = "{$part['altunit']}\0{$part['measurement']}";
$set[$key] = $part; // attach the set
}
}
// Now you have...
foreach($set as $value) {
var_dump($value);
}
And that would give you...
array(3) {
["id"]=>
string(1) "2"
["altunit"]=>
string(1) "%"
["measurement"]=>
NULL
}
N.B
But, if this is a result set from an SQL query it's quite possible you could just more effectively eliminate the de-duplication process entirely by modifying the query to use WHERE NOT NULL and GROUP BY clauses instead.
You can try this also
<?php
$keys = array_map(function ($i) { return $i; }, $array);
$dumparr = array_combine($keys, $array);
$result = array_values($deduped);
print_r($result);
?>

Converting table-like array to a tree-like array

Consider the following example:
function tableToTree($array, $parents)
{
$result = [];
$p = $parents;
foreach ($array as $k => $row) {
$result[$row[$p[0]]][$row[$p[1]]]= $row[$p[2]]; // **
}
return $result;
}
$foo = [
['x' => 'a', 'y' => 'n','z'=>'AA'],
['x' => 'a', 'y' => 'm','z'=>'BB'],
['x' => 'b', 'y' => 'v','z'=>'CC'],
['x' => 'b', 'y' => 'w','z'=>'DD'],
];
print_r(tableToTree($foo, ['x', 'y','z']));
Which yields:
Array
(
[a] => Array
(
[n] => AA
[m] => BB
)
[b] => Array
(
[v] => CC
[w] => DD
)
)
The above code only works when there are only two parents. How to rewrite the line denoted by ** in a way that it work with arbitrary number of parents.
function tableToTree($array, $parent)
{
$result = array();
foreach ($array as $row) {
$r = &$result; // pointer to result
$p = $parent; // temporary copy of parents
while(count($p) > 2) { // until last key and value
$i = array_shift($p); // next key
if(!isset($r[$row[$i]])) $r[$row[$i]] = null; // add level
$r = &$r[$row[$i]]; // shift pointer to new level
}
$r[$row[array_shift($p)]] = $row[array_shift($p)]; // set value
}
return $result;
}
$foo = array(
array('N' => 2, 'x' => 'a', 'y' => 'n','z'=>'AA'),
array('N' => 1, 'x' => 'a', 'y' => 'm','z'=>'BB'),
array('N' => 2, 'x' => 'b', 'y' => 'v','z'=>'CC'),
array('N' => 1, 'x' => 'b', 'y' => 'w','z'=>'DD'),
array('N' => 1, 'x' => 'c', 'y' => 'w','z'=>'DD'));
print_r(tableToTree($foo, array('N','x', 'y','z')));
result
array (
2 => array (
'a' => array ( 'n' => 'AA',),
'b' => array ( 'v' => 'CC',),
),
1 => array (
'a' => array ( 'm' => 'BB', ),
'b' => array ( 'w' => 'DD', ),
'c' => array ( 'w' => 'DD', ),
),
)

Categories