Associative Array Needed [duplicate] - php

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

Related

Convert 2d array by specified 2d format

I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);

PHP array merge based on key & value

I have one array and try to change some key and value for example if sku are same than i need to merge image. Below array i have
Array
(
[0] => Array
(
[sku] => h-eldora
[name] => H ELDORA
[image] => s/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054
)
[1] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
)
[2] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[3] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
)
[4] => Array
(
[sku] => hl-dracy
[name] =>
[image] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
)
[5] => Array
(
[sku] => hl-dracy
[name] =>
[image] =>s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
I need to merge array like this
Array
(
[0] => Array
(
[sku] => h-eldora
[name] =>
[image1] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
[image2] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
[image3] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[1] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image1] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
[image2] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
[image3] => s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
If any php function is there than please let me know or any code suggestion
Using simple PHP:
<?php
$arr1 = array(
0 => array(
'sku' => 'h-eldora',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054'
),
1 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221'
),
2 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598'
),
3 => array(
'sku' => 'hl-dracy',
'name' => 'HL DRACY',
'image' => 's/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222'
),
4 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223'
),
5 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793'
)
);
$newArr = $imgIndex = array();
foreach($arr1 as $a){
if( !array_key_exists($a['sku'],$newArr) ){
$newArr[$a['sku']] = array(
'sku' => $a['sku'],
'name' => $a['name'],
'image1' => $a['image']
);
$imgFound[$a['sku']] = 1;
}else{
$imgFound[$a['sku']]++;
$newArr[$a['sku']]['image'.$imgFound[$a['sku']]] = $a['image'];
}
}
unset($imgFound);
echo '<pre>'; print_r($newArr); echo '</pre>';
?>
This could work for you:
$data = // your input array
$uniqueSKUs = Array();
$newArray = Array();
$currentIndex = -1;
foreach ($data as $item) {
if (!in_array($item['sku'], $uniqueSKUs)) {
$currentIndex++;
$uniqueSKUs[] = $item['sku'];
$newArray[$currentIndex] = Array(
'sku' => $item['sku'],
'name' => $item['name']
);
}
$newArray[$currentIndex]['images'][] = $item['image'];
}
echo "<pre>";
var_dump($newArray);
echo "</pre>";

Associative Arrays

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

How to use array keys as new values for a derived array

I have the following array which I would like to edit:
Array
(
[qty_black_34] =>
[qty_black_36] => 2
[qty_black_38] =>
[qty_black_40] =>
[qty_black_42] =>
[qty_black_44] =>
[qty_black_48] =>
[qty_powder_34] =>
[qty_powder_36] =>
[qty_powder_38] =>
[qty_powder_40] =>
[qty_powder_42] => 1
[qty_powder_44] =>
[qty_powder_48] =>
[qty_red_34] =>
[qty_red_36] =>
[qty_red_38] => 2
[qty_red_40] =>
[qty_red_42] =>
[qty_red_44] =>
[qty_red_48] => )
What I want to do is to build another array to hold only the elements with a value.
The new array must look like this"
Array
(
[0] => Array
(
[color] => black
[size] => 36
[quantity] => 2
)
[1] => Array
(
[color] => powder
[size] => 42
[quantity] => 1
)
[2] => Array
(
[color] => red
[size] => 38
[quantity] => 2
)
)
PHP is the language I'm using.
loop through array and take elements, which have set a value. for the additional key/values in your final array split the string used as key in original array
$new_array = array();
foreach ($old_array as $key => value) {
if ($value) {
$key_split = explode('_', $key);
$new_array[] = array('color' => $key_split[1], 'size' => $key_split[2], 'quantity' => $value);
}
}
$products = array();
foreach($quantities as $key => $quantity){
if($quantity != '') {
list($q, $color, $size) = explode('_', $key);
$products[] = array(
'color' => $color,
'size' => $size,
'quantity' => $quantity
);
}
}
Demo

show hierarchical select in a php function

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

Categories