I have an array like below:
$cats = array();
$cats[1] = array('id' => 1,'parent' => 0, 'title' => 'Tutorials');
$cats[2] = array('id' => 2,'parent' => 1, 'title' => 'PHP');
$cats[3] = array('id' => 3,'parent' => 2, 'title' => 'OOP');
$cats[4] = array('id' => 4,'parent' => 2, 'title' => 'Tips');
$cats[5] = array('id' => 5,'parent' => 1, 'title' => 'JavaScript');
$cats[6] = array('id' => 6,'parent' => 5, 'title' => 'Basics');
$cats[7] = array('id' => 7,'parent' => 5, 'title' => 'Frameworks');
$cats[8] = array('id' => 8,'parent' => 7, 'title' => 'jQuery');
$cats[9] = array('id' => 9,'parent' => 7, 'title' => 'MooTools');
$cats[10] = array('id' => 10,'parent' => 0, 'title' => 'News');
$cats[11] = array('id' => 11,'parent' => 10, 'title' => 'PHP');
$cats[12] = array('id' => 12,'parent' => 10, 'title' => 'Wordpress');
$cats[13] = array('id' => 13,'parent' => 0, 'title' => 'New');
and want to show it in a PHP function like this that call an function and give this array,
for example
$id=1;
builder_tree($id);
and give me bellow array ,please help me
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Tutorials
[children] => Array
(
[0] => Array
(
[id] => 2
[parent] => 1
[title] => PHP
[children] => Array
(
)
)
[1] => Array
(
[id] => 3
[parent] => 1
[title] => PHP
[children] => Array
(
)
)
[2] => Array
(
[id] => 5
[parent] => 1
[title] => JavaScript
[children] => Array
(
[0] => Array
(
[id] => 6
[parent] => 5
[title] => PHP
[children] => Array
(
)
)
[1] => Array
(
[id] => 7
[parent] => 5
[title] => PHP
[children] => Array
(
[0] => Array
(
[id] => 8
[parent] => 7
[title] => jQuery
[children] => Array
(
)
)
[1] => Array
(
[id] => 9
[parent] => 7
[title] => MooTools
[children] => Array
(
)
)
)
)
)
)
)
)
I realize this is an old question, but I was looking for something similar to what the original poster needed and came up with the following.
Hope this helps any future Googlers.
The build_tree_array() basically takes what the original poster posted above and coverts it to a hierarchical array.
function folder_has_children($rows, $id) {
foreach ($rows as $row) {
if ($row['child_of'] == $id)
return true;
}
return false;
}
function build_tree_array(&$rows, $parent = 0) {
foreach ($rows as $row) {
if ($row['child_of'] == $parent) {
$result = $row;
if ($this->folder_has_children($rows, $row['id'])) {
$result['children'] = $this->build_tree_array($rows, $row['id']);
}
$out[] = $result;
}
}
return $out;
}
Related
i have problem to combine values based on id.
I have data like this :
Array(
[0] => Array(
[id] => 1,
[id_name] => a
[id_vales] => 5
)
[1] => Array(
[id] => 1
[id_name] => a
[id_vales] => 4
)
[2] => Array(
[id] => 3
[id_name] => b
[id_vales] => 4
)
[3] => Array(
[id] => 3
[id_name] => b
[id_vales] => 3
)
)
then, i want combine [id_values] based on id, so i can get data like this in php
Array(
[0] => Array(
[id] => 1
[id_name] => a
[id_vales] => 5, 4
)
[1] => Array(
[id] => 3
[id_name] => b
[id_vales] => 4, 3
)
)
You can use the following example to merge your array
<?php
$mainArray = array(array('id' => 1, 'id_name' => 'a', 'id_vales' => 5),
array('id' => 1,'id_name' => 'a','id_vales' => 4),
array('id' => 3, 'id_name' => 'b','id_vales' => 4),
array('id' => 3,'id_name' => 'b','id_vales' => 3)
);
$result = array();
$tempArray = array();
foreach($mainArray as $key => $value)
{
if(isset($tempArray[$value['id']]))
{
$tempArray[$value['id']] .= ", ".$value['id_vales'];
$result[] = array('id' => $value['id'], 'id_name' => $value['id_name'], 'id_vales' => $tempArray[$value['id']]);
}
else
{
$tempArray[$value['id']] = "".$value['id_vales'];
}
}
echo "<pre>";
print_r($result);
?>
You can find running example here https://paiza.io/projects/3sS3GXH7GHqoipH8k-YtBQ
Output:
Array
(
[0] => Array
(
[id] => 1
[id_name] => a
[id_vales] => 5, 4
)
[1] => Array
(
[id] => 3
[id_name] => b
[id_vales] => 4, 3
)
)
I have created an array for you. from this array you can easily create your array and get the result.
$data = array();
foreach($array as $key=>$value){
$data[$value['id']]['id'] = $value['id'];
$data[$value['id']]['id_vales'][] = $value['id_vales'];
$data[$value['id']]['id_name'] = $value['id_name'];
}
I'm losing my hair on this one... I have an array structure that print_r's like this (I've hidden unnecessary fields):
[0] => Array
(
[id] => 14
[name] => Foo Directory
)
[1] => Array
(
[id] => 16
[name] => Bar Project
[parent] => Array
(
[id] => 14
[name] => Foo Directory
)
)
[2] => Array
(
[id] => 20
[name] => Baz Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
)
[3] => Array
(
[id] => 10
[name] => Qux Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
And I need it to be nested like this:
[0] => Array
(
[id] => 14
[name] => Foo Directory
[children] => Array
(
[id] => 16
[name] => Bar Project
[children] => Array
(
[id] => 20
[name] => Baz Project
)
(
[id] => 10
[name] => Qux Project
)
)
)
What I've tried so far
$projTree = array();
foreach ($projects as $project) {
if (isset($project['parent']))
array_push($projTree['children'], $project);
$projTree['id'] = $project['parent']['id'];
}
But that overwrites the previous inserted element. I also tried to walk it recursively, but couldn't figure out the correct callback for that, since it only operates on the leafs of the tree and I need to fully walk it.
How can I do this? Thanks!
Please try like below:-
<?php
$array = Array(
'0' => Array
(
'id' => 14,
'name' => 'Foo Directory'
),
'1' => Array
(
'id' => 16,
'name' => 'Bar Project',
'parent' => Array
(
'id' => 14,
'name' => 'Foo Directory'
)
),
'2' => Array
(
'id' => 20,
'name' => 'Baz Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
),
'3' => Array
(
'id' => 10,
'name' => 'Qux Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
)
);
$new_array = array();
$i = 0;
$j = 0;
foreach ($array as $key=>$val){
if(array_key_exists('parent',$val)){
foreach($new_array as $key1=>$val1){
if($val['parent']['name'] === $val1['name']){
$new_array[$key1]['children'][$i]['id'] = $val['id'];
$new_array[$key1]['children'][$i]['name'] = $val['name'];
}else{
foreach ($val1['children'] as $key3=>$val3){
if($val['parent']['name'] === $val3['name']){
$new_array[$key1]['children'][$key3]['children'][$j]['id'] = $val['id'];
$new_array[$key1]['children'][$key3]['children'][$j]['name'] = $val['name'];
}
$j++;
}
$i++;
}
}
}else{
$new_array[$key] = $val;
}
}
echo "<pre/>";print_r($new_array);
Output:- https://eval.in/419591
I'm needing to rearrange Array 1 by the order of how the [id] (oper-%%%%%%%%%%) appears within Array 2, and I'm not quite sure as to how to approach this.
Array 1
Array ( [0] => Array ( [id] => oper-4c597644-402490ee [amount] => 17498.5 ) [1] => Array ( [id] => oper-4f30019a-27f27473 [amount] => 10383.5 ) [2] => Array ( [id] => oper-4bceffd1-21e0af5b [amount] => 6277 ) [3] => Array ( [id] => oper-4f300d33-de9592e3 [amount] => 11382 ) [4] => Array ( [id] => oper-4c236420-0b11e945 [amount] => 14759 ) [5] => Array ( [id] => oper-50f6e4ad-9effbec7 [amount] => 3058 ) [6] => Array ( [id] => oper-4f05a90b-03b379f9 [amount] => 12112.5 ) [7] => Array ( [id] => oper-qtgjvw8y-1uqtw058 [amount] => 10023 ) [8] => Array ( [id] => oper-52657816-3d6516e2 [amount] => 3495 ) )
Array 2
Array ( [0] => Array ( [0] => Bob [1] => oper-4bceffd1-21e0af5b ) [1] => Array ( [0] => Bob [1] => oper-4c236420-0b11e945 ) [2] => Array ( [0] => Bob [1] => oper-4c597644-402490ee ) [3] => Array ( [0] => Bob [1] => oper-4f05a90b-03b379f9 ) [4] => Array ( [0] => Bob [1] => oper-4f30019a-27f27473 ) [5] => Array ( [0] => Bob [1] => oper-4f300d33-de9592e3 ) [6] => Array ( [0] => Bob [1] => oper-50f6e4ad-9effbec7 ) [7] => Array ( [0] => Bob [1] => oper-52657816-3d6516e2 ) [8] => Array ( [0] => Bob [1] => oper-qtgjvw8y-1uqtw058 ) [9] => Array ( [0] => Empty [1] => ) [10] => Array ( [0] => Upg. [1] => ) [11] => Array ( [0] => Ren. [1] => ) )
Bob is just an example "name"
Here is my code so far (And I know this isn't clean because this isn't done with PDO, I'm just trying to get this to work at the moment):
$result = mysql_query("SELECT * FROM tblOperatorGoals WHERE MonthlyGoal LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$opernameArray[] = $row['OperatorName'];
$operIDArray[] = $row['OperatorID'];
$monthlyGoal[] = substr($row['MonthlyGoal'], 8);
$operArray[] = array('name' => $row['OperatorName'], 'id' => $row['OperatorID']);
}
$result = mysql_query("SELECT * FROM tblUserPayments WHERE ChargeAmount IS NOT NULL AND PaymentStatus='OK' AND PaymentDate LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
foreach ($operearnedArray as $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums1)) {
$operSums1[$value['id']] += $value['amount'];
} else {
$operSums1[$value['id']] = $value['amount'];
}
}
foreach ($operSums1 as $id => $value) {
if (in_array($id,$operIDArray)) {
$operSums[] = array(
'id' => $id,
'amount' => $value);
}
}
Any help is greatly appreciated. Associative arrays are just not my cup of tea.
Basically I'm looking for an Array that looks like this:
Array ( [name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
Crude but should work:
$toSort = array(
array ('id' => 'oper-4c597644-402490ee', 'amount' => 17498.5,),
array ('id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5,),
array ('id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277,),
array ('id' => 'oper-4f300d33-de9592e3', 'amount' => 11382,),
array ('id' => 'oper-4c236420-0b11e945', 'amount' => 14759,),
array ('id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058,),
array ('id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5,),
array ('id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023,),
array ('id' => 'oper-52657816-3d6516e2', 'amount' => 3495,),
);
$assigned = array (
array ('Bob','oper-4bceffd1-21e0af5b'),
array ('Bob','oper-4c236420-0b11e945'),
array ('Bob','oper-4c597644-402490ee'),
array ('Bob','oper-4f05a90b-03b379f9'),
array ('Bob','oper-4f30019a-27f27473'),
array ('Bob','oper-4f300d33-de9592e3'),
array ('Bob','oper-50f6e4ad-9effbec7'),
array ('Bob','oper-52657816-3d6516e2'),
array ('Bob','oper-qtgjvw8y-1uqtw058'),
);
$sorted = assignData($toSort, $assigned);
var_dump($sorted);
function assignData($raw, $order)
{
$returnArray = array();
foreach($order as $entry):
foreach($raw as $rawEntry):
if ($rawEntry['id'] == $entry[1]):
$temp = array(
'name' => $entry[0],
'oper' => $rawEntry['id'],
'amount' => $rawEntry['amount'],
);
$returnArray[] = $temp;
endif;
endforeach;
endforeach;
return $returnArray;
}
Try this.
<?php
$data = [
['id' => 'oper-4c597644-402490ee', 'amount' => 17498.5],
['id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5],
['id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277],
['id' => 'oper-4f300d33-de9592e3', 'amount' => 11382],
['id' => 'oper-4c236420-0b11e945', 'amount' => 14759],
['id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058],
['id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5],
['id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023],
['id' => 'oper-52657816-3d6516e2', 'amount' => 3495]
];
$names = [
['BobA', 'oper-4bceffd1-21e0af5b'],
['BobB', 'oper-4c236420-0b11e945'],
['BobC', 'oper-4c597644-402490ee'],
['BobD', 'oper-4f05a90b-03b379f9'],
['BobE', 'oper-4f30019a-27f27473'],
['BobF', 'oper-4f300d33-de9592e3'],
['BobG', 'oper-50f6e4ad-9effbec7'],
['BobH', 'oper-52657816-3d6516e2'],
['BobI', 'oper-qtgjvw8y-1uqtw058'],
['Empty', ''],
['Upg.', ''],
['Ren.', '']
];
$idMappedNames = array_column($names, 0, 1);
$summary = array_reduce($data, function($carry, $item) use(&$idMappedNames) {
if (!array_key_exists($item['id'], $carry)) {
$carry[$item['id']] = [
'name' => $idMappedNames[$item['id']],
'amount' => 0
];
}
$carry[$item['id']]['amount'] += $item['amount'];
return $carry;
}, []);
You can connect these information it in just single SQL Query
$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal
FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID
WHERE a.ChargeAmount IS NOT NULL
AND a.PaymentStatus='OK'
AND a.PaymentDate LIKE '$currentDate%'
AND u.MonthlyGoal LIKE '$currentDate%' " );
while ($row = mysql_fetch_assoc($result)) {
$wantedArray[] = array(
'OperatorName' => $row['OperatorName'],
'MonthlyGoal'=>$row['MonthlyGoal'],
'ChargeAmount' => $row['ChargeAmount'],
'OperatorID' => $row['OperatorID']);
}
But as the question originally asked goes:
$desiredArray = array();
$id_to_amount = array();
foreach($array1 as $x)
$id_to_amount[$x['id']] = $x['amount'];
foreach($array2 as $x)
{
$id = $x[1];
$name = $x[0];
$amount = -1;
if(isset($id_to_amount[$id]))
$amount = $id_to_amount[$id];
$desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount );
}
I'm needing to rearrange Array 1 by the order of how the [id] (oper-%%%%%%%%%%) appears within Array 2, and I'm not quite sure as to how to approach this.
Array 1
Array ( [0] => Array ( [id] => oper-4c597644-402490ee [amount] => 17498.5 ) [1] => Array ( [id] => oper-4f30019a-27f27473 [amount] => 10383.5 ) [2] => Array ( [id] => oper-4bceffd1-21e0af5b [amount] => 6277 ) [3] => Array ( [id] => oper-4f300d33-de9592e3 [amount] => 11382 ) [4] => Array ( [id] => oper-4c236420-0b11e945 [amount] => 14759 ) [5] => Array ( [id] => oper-50f6e4ad-9effbec7 [amount] => 3058 ) [6] => Array ( [id] => oper-4f05a90b-03b379f9 [amount] => 12112.5 ) [7] => Array ( [id] => oper-qtgjvw8y-1uqtw058 [amount] => 10023 ) [8] => Array ( [id] => oper-52657816-3d6516e2 [amount] => 3495 ) )
Array 2
Array ( [0] => Array ( [0] => Bob [1] => oper-4bceffd1-21e0af5b ) [1] => Array ( [0] => Bob [1] => oper-4c236420-0b11e945 ) [2] => Array ( [0] => Bob [1] => oper-4c597644-402490ee ) [3] => Array ( [0] => Bob [1] => oper-4f05a90b-03b379f9 ) [4] => Array ( [0] => Bob [1] => oper-4f30019a-27f27473 ) [5] => Array ( [0] => Bob [1] => oper-4f300d33-de9592e3 ) [6] => Array ( [0] => Bob [1] => oper-50f6e4ad-9effbec7 ) [7] => Array ( [0] => Bob [1] => oper-52657816-3d6516e2 ) [8] => Array ( [0] => Bob [1] => oper-qtgjvw8y-1uqtw058 ) [9] => Array ( [0] => Empty [1] => ) [10] => Array ( [0] => Upg. [1] => ) [11] => Array ( [0] => Ren. [1] => ) )
Bob is just an example "name"
Here is my code so far (And I know this isn't clean because this isn't done with PDO, I'm just trying to get this to work at the moment):
$result = mysql_query("SELECT * FROM tblOperatorGoals WHERE MonthlyGoal LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$opernameArray[] = $row['OperatorName'];
$operIDArray[] = $row['OperatorID'];
$monthlyGoal[] = substr($row['MonthlyGoal'], 8);
$operArray[] = array('name' => $row['OperatorName'], 'id' => $row['OperatorID']);
}
$result = mysql_query("SELECT * FROM tblUserPayments WHERE ChargeAmount IS NOT NULL AND PaymentStatus='OK' AND PaymentDate LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
foreach ($operearnedArray as $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums1)) {
$operSums1[$value['id']] += $value['amount'];
} else {
$operSums1[$value['id']] = $value['amount'];
}
}
foreach ($operSums1 as $id => $value) {
if (in_array($id,$operIDArray)) {
$operSums[] = array(
'id' => $id,
'amount' => $value);
}
}
Any help is greatly appreciated. Associative arrays are just not my cup of tea.
Basically I'm looking for an Array that looks like this:
Array ( [name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
Crude but should work:
$toSort = array(
array ('id' => 'oper-4c597644-402490ee', 'amount' => 17498.5,),
array ('id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5,),
array ('id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277,),
array ('id' => 'oper-4f300d33-de9592e3', 'amount' => 11382,),
array ('id' => 'oper-4c236420-0b11e945', 'amount' => 14759,),
array ('id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058,),
array ('id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5,),
array ('id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023,),
array ('id' => 'oper-52657816-3d6516e2', 'amount' => 3495,),
);
$assigned = array (
array ('Bob','oper-4bceffd1-21e0af5b'),
array ('Bob','oper-4c236420-0b11e945'),
array ('Bob','oper-4c597644-402490ee'),
array ('Bob','oper-4f05a90b-03b379f9'),
array ('Bob','oper-4f30019a-27f27473'),
array ('Bob','oper-4f300d33-de9592e3'),
array ('Bob','oper-50f6e4ad-9effbec7'),
array ('Bob','oper-52657816-3d6516e2'),
array ('Bob','oper-qtgjvw8y-1uqtw058'),
);
$sorted = assignData($toSort, $assigned);
var_dump($sorted);
function assignData($raw, $order)
{
$returnArray = array();
foreach($order as $entry):
foreach($raw as $rawEntry):
if ($rawEntry['id'] == $entry[1]):
$temp = array(
'name' => $entry[0],
'oper' => $rawEntry['id'],
'amount' => $rawEntry['amount'],
);
$returnArray[] = $temp;
endif;
endforeach;
endforeach;
return $returnArray;
}
Try this.
<?php
$data = [
['id' => 'oper-4c597644-402490ee', 'amount' => 17498.5],
['id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5],
['id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277],
['id' => 'oper-4f300d33-de9592e3', 'amount' => 11382],
['id' => 'oper-4c236420-0b11e945', 'amount' => 14759],
['id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058],
['id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5],
['id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023],
['id' => 'oper-52657816-3d6516e2', 'amount' => 3495]
];
$names = [
['BobA', 'oper-4bceffd1-21e0af5b'],
['BobB', 'oper-4c236420-0b11e945'],
['BobC', 'oper-4c597644-402490ee'],
['BobD', 'oper-4f05a90b-03b379f9'],
['BobE', 'oper-4f30019a-27f27473'],
['BobF', 'oper-4f300d33-de9592e3'],
['BobG', 'oper-50f6e4ad-9effbec7'],
['BobH', 'oper-52657816-3d6516e2'],
['BobI', 'oper-qtgjvw8y-1uqtw058'],
['Empty', ''],
['Upg.', ''],
['Ren.', '']
];
$idMappedNames = array_column($names, 0, 1);
$summary = array_reduce($data, function($carry, $item) use(&$idMappedNames) {
if (!array_key_exists($item['id'], $carry)) {
$carry[$item['id']] = [
'name' => $idMappedNames[$item['id']],
'amount' => 0
];
}
$carry[$item['id']]['amount'] += $item['amount'];
return $carry;
}, []);
You can connect these information it in just single SQL Query
$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal
FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID
WHERE a.ChargeAmount IS NOT NULL
AND a.PaymentStatus='OK'
AND a.PaymentDate LIKE '$currentDate%'
AND u.MonthlyGoal LIKE '$currentDate%' " );
while ($row = mysql_fetch_assoc($result)) {
$wantedArray[] = array(
'OperatorName' => $row['OperatorName'],
'MonthlyGoal'=>$row['MonthlyGoal'],
'ChargeAmount' => $row['ChargeAmount'],
'OperatorID' => $row['OperatorID']);
}
But as the question originally asked goes:
$desiredArray = array();
$id_to_amount = array();
foreach($array1 as $x)
$id_to_amount[$x['id']] = $x['amount'];
foreach($array2 as $x)
{
$id = $x[1];
$name = $x[0];
$amount = -1;
if(isset($id_to_amount[$id]))
$amount = $id_to_amount[$id];
$desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount );
}
i've a similar array:
Array
(
[0] => Array
(
[id] => 1
[name] => Mario
[type] => Doctor
[operations] => brain
[balance] => 3.00
)
[1] => Array
(
[id] => 3
[name] => Luca
[type] => Doctor
[operations] => hearth
[balance] => 6.00
)
[2] => Array
(
[id] => 3
[name] => Luca
[type] => Doctor
[operations] => hands
[balance] => 4.00
)
[3] => Array
(
[id] => 3
[name] => Luca
[type] => Doctor
[operations] => foots
[balance] => 1.00
)
)
I must to merge it for id, so obtain only 2 elements for array (0 and 1) and Luca (id 3) must be unified for operations, in a new array, similar to this, so in future print more clearly operations unified and not divided.
[...]
)
[1] => Array
(
[id] => 3
[name] => luca
[type] => doctore
[operations] => Array (6.00 hearts,
4.00 hands,
1.00 foots)
)
I can not figure how solve my trouble... Someone could help me please? Thank you very much to all!
<?php
$input = array(
array('id' => 1, 'name' => 'Mario', 'type' => 'Doctor', 'operations' => 'brain', 'balance' => 3.00),
array('id' => 3, 'name' => 'Luca', 'type' => 'Doctor', 'operations' => 'hearth', 'balance' => 6.00),
array('id' => 3, 'name' => 'Luca', 'type' => 'Doctor', 'operations' => 'hands', 'balance' => 4.00),
array('id' => 3, 'name' => 'Luca', 'type' => 'Doctor', 'operations' => 'foots', 'balance' => 1.00),
);
$output = array();
foreach ($input as $person)
{
// Add the person to the output, if needed.
if (array_key_exists($person['id'], $output) === false)
{
$output[$person['id']] =
array(
'id' => $person['id'],
'name' => $person['name'],
'type' => $person['type'],
'operations' => array(),
);
}
// Add the operation to the array of operations.
$output[$person['id']]['operations'][] =
array(
'type' => $person['operations'],
'balance' => $person['balance'],
));
}
foreach ($output as $person)
{
if (count($person['operations']) === 1)
{
// If the array contains only 1 item, pull it out of the array.
$output[$person['id']]['operations'] = $person['operations'][0];
}
}
print_r($output);