I have an array like this:
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
Now I want to take $r and get multiple arrays back, differenced by supplierId. So I am looking for this result:
$r30 = ['name' => 'Test5', 'supplierId' => 30];
$r32 = [
['name' => 'Test3', 'supplierId' => 32],
['name' => 'Test6', 'supplierId' => 32]
];
I tried it with, but here I do not have access to $sup in array_filter.
$supplier = array(30, 31, 32, 34);
$finalArray = [];
foreach ($supplier as $sup) {
$finalArray[] = array_filter($r, function($value, $sup) {
echo $sup;
if ($value['supplierId'] == $sup) {
return true;
}
});
}//foreach
Any idea how I can solve it? Is there no native function that accomplishes this - something like create_array_based_on('supplierId');?
Thanks
You can pass $sup to your anonymous function:
foreach ($supplier as $sup) {
$finalArray[] = array_filter($r, function($value) use ($sup) {
^^^^^^^^^^ you need to pass
it like this
echo $sup;
if ($value['supplierId'] == $sup) {
return true;
}
});
}//foreach
But personally I would probably just loop over the original array and use the supplier ID as a key.
Something like:
$results = [];
foreach ($r as $value) {
$results[$value['supplierId']][] = $value;
}
Try this out
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
$supplier = array(30, 31, 32, 34);
$finalArray = [];
$i=0;
foreach ($supplier as $sup) {
$value =$r[$i]['supplierId'];
if($value==$sup)
{
$finalArray[] = $value;
}
$i++;
}//foreach
$finalArray is a new array with all values you need
You can combine array_filter and in_array as following:
$finalArray[] = array_filter($r, function($value) use ($supplier) {
return in_array($value['supplierId'], $supplier);
});
Without foreach loop.
It would be much easier, instead of wanting new variables for each of them, like $r32, $r35, etc... if you just used the array keys to store the supplierid, e.g.
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
$newArray = array();
foreach($r as $key => $element)
{
if (!isset($newArray[$element['supplierId']])){
$newArray[$element['supplierId']] = array();
}
$newArray[$element['supplierId']][] = $element;
}
That would give you an output of:
<pre>Array
(
[34] => Array
(
[0] => Array
(
[name] => Test
[supplierId] => 34
)
[1] => Array
(
[name] => Test4
[supplierId] => 34
)
)
[31] => Array
(
[0] => Array
(
[name] => Test2
[supplierId] => 31
)
)
[32] => Array
(
[0] => Array
(
[name] => Test3
[supplierId] => 32
)
[1] => Array
(
[name] => Test6
[supplierId] => 32
)
)
[30] => Array
(
[0] => Array
(
[name] => Test5
[supplierId] => 30
)
)
)
</pre>
It's about simple grouping nested arrays by some key value:
$finalArr = [];
foreach ($r as $arr) {
$finalArr[$arr['supplierId']][] = $arr;
}
print_r($finalArr);
DEMO link
Related
i want to combine the values of array having same index name in foreach loop..
i tried array_combine but it returns the single array.
$data = $_POST['variable']; //it contain the values in an array
$result=array();
foreach ($data as $mycat){
$result = array_merge($result, $mycat);
}
echo "<pre>";print_r($result);echo "</pre>";
it returns only data in single array
Array
(
[vendor] => 1-Open Market
[priority] => 2
[demand_for_id] => 9
[ims_allocation_details_id] => 148
[temp_demand_id] => 1
)
as shown in attached picture item names are same, so when item names are same i want to combine the total values in foreach and insert only one record into database instead to two
enter image description here
the contents of $_POST['variable']; are
Array
(
[2] => Array
(
[vendor] => 1-Open Market
[temp_demand_id] => 6
[priority] => 1
[item_name] => BAJRA MOTI
[amount] => 1000
[demand_for_id] => 9
[ims_allocation_details_id] => 153
)
[1] => Array
(
[vendor] => 1-Open Market
[temp_demand_id] => 1
[priority] => 2
[item_name] => BAJRA MOTI
[amount] => 2500
[demand_for_id] => 9
[ims_allocation_details_id] => 148
)
)
You should replace
$result = array_merge($result, $mycat);
with
$result[] = array_merge($result, $mycat);
and it will not be single
UPDATE
$result = array();
foreach ($data as $mycat) {
if(!isset($result[$mycat['item_name']])) {
$result[$mycat['item_name']] = $mycat;
} else {
//do if needed
}
}
echo "<pre>";print_r($result);echo "</pre>";
You can create a custom function to solve your problem.
Example:
<?php
$array = [
[
'vendor' => '1-Open Market',
'temp_demand_id' => 6,
'priority' => 1,
'item_name' => 'BAJRA MOTI',
'amount' => 1000,
'demand_for_id' => 9,
'ims_allocation_details_id' => 153,
],
[
'vendor' => '1-Open Market',
'temp_demand_id' => 1,
'priority' => 2,
'item_name' => 'BAJRA MOTI',
'amount' => 2500,
'demand_for_id' => 9,
'ims_allocation_details_id' => 148,
],
[
'vendor' => '1-Open Market',
'temp_demand_id' => 5,
'priority' => 3,
'item_name' => 'BAJRA MOTI',
'amount' => 1000,
'demand_for_id' => 11,
'ims_allocation_details_id' => 200,
],
];
function array_merge_recursive_custom($array) {
$processed = null;
foreach ($array as &$subArray) {
if (empty($processed)) {
$processed = $subArray;
continue;
}
foreach ($subArray as $key => $value) {
if (is_numeric($value)) {
$subArray[$key] += $processed[$key];
}
}
$processed = $subArray;
}
return end($array);
}
var_dump(array_merge_recursive_custom($array));
Supposing I have this array in PHP:
Array
(
[0] => Array
(
[name] => Banana
[quantity] => 124
)
[1] => Array
(
[name] => Cherry
[quantity] => 24
)
[2] => Array
(
[name] => Apple
[quantity] => 224
)
)
How can I sum the number with the key quantity ?
Thanks.
Please, always share with us what you have tried.
It help us a lot.
You can use:
$arr = [['name' => "Banana", 'quantity' => 124], ['name' => "Cherry", 'quantity' => 24], ['name' => "Apple", 'quantity' => 224]];
$sum = 0;
foreach ($arr as $item) {
$sum += $item['quantity'];
}
Or (PHP 5.5+):
$sum = array_sum(array_column($arr, 'quantity'));
/*
Receives a Multidemensional Array (Matrix) and returns the sum of quantity.
Returns -1 on fail.
*/
function SumKeyOfArray($Matrix)
{
if(!empty($Matrix))
{
$sum = 0;
foreach($Matrix as $array)
{
if(isset($array['quantity']))
$sum = $sum + $array['quantity'];
}
return $sum;
}
return -1;
}
Another option is to use the reduce function:
$arr= [['name' => "Banana", 'quantity' => 124], ['name' => "Cherry", 'quantity' => 24], ['name' => "Apple", 'quantity' => 224]];
echo array_reduce($arr, function($sum, $elem) {
return $sum += $elem["quantity"];
});
This question already has answers here:
Group multidimensional array data based on two column values and sum values of one column in each group
(5 answers)
Closed 7 months ago.
I have the following array, in which I want to sum up the total_volume for all entries where the source and the target are the same.
Array (
[0] => Array
(
[source] => ABC
[target] => DEF
[total_volume] => 10
)
[1] => Array
(
[source] => ABC
[target] => GHI
[total_volume] => 5
)
[2] => Array
(
[source] => ABC
[target] => DEF
[total_volume] => 5
)
)
The resulting array should look like this:
ResultArray (
[0] => Array
(
[source] => ABC
[target] => DEF
[total_volume] => 15
)
[1] => Array
(
[source] => ABC
[target] => GHI
[total_volume] => 5
)
)
My first thought would be to llop through the existing array and check via a nested loop over the ResultArray whether an entry with the matching source-target-pair already exists.
Is there an other way using array_walk() or a similar method?
Thanks in advance for your help!
not pretty but heres how I would do it with a nested foreach;
$aStartingArray = array();
$aStartingArray[] = array('source'=>'ABC', 'target' => 'DEF', 'total_volume' => 10);
$aStartingArray[] = array('source'=>'ABC', 'target' => 'GHI', 'total_volume' => 5);
$aStartingArray[] = array('source'=>'ABC', 'target' => 'DEF', 'total_volume' => 5);
$aSortedArray = array();
foreach ($aStartingArray as $aArray) {
$bSet = false;
foreach ($aSortedArray as $iPos => $aTempSortedArray) {
if(
$aTempSortedArray['source'] == $aArray['source'] &&
$aTempSortedArray['target'] == $aArray['target']){
$aSortedArray[$iPos]['total_volume'] += $aArray['total_volume'];
$bSet = true;
}
}
if(!$bSet) {
$aSortedArray[] = array(
'source' => $aArray['source'],
'target' => $aArray['target'],
'total_volume' => $aArray['total_volume']
);
}
}
var_dump($aSortedArray);
This is a fast try (sorry, it uses twice array_walk)
<?php
$a = [
['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 10 ],
['source' => 'ABC', 'target' => 'GHI', 'total_volume' => 5 ],
['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 5 ],
];
$tmp = [];
array_walk($a, function (&$item, $key) use (&$tmp) {
$resKey = $item['source'].'_'.$item['target'];
if (!isset($result[$resKey])) {
$result[$resKey] = 0;
}
$tmp[$resKey] += $item['total_volume'];
});
$result = [];
array_walk($tmp, function (&$item, $key) use (&$result) {
list ($source, $target) = explode('_', $key);
$result[] = ['source' => $source, 'target' => $target, 'total_volume' => $item];
});
var_dump($result);
How about this? A bit briefer I think.
$a = [
['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 10 ],
['source' => 'ABC', 'target' => 'GHI', 'total_volume' => 5 ],
['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 5 ],
];
$result = $result2 = [];
array_walk($a, function(&$item, $key) use(&$result) {
if(! isset($result[$item['source']]))
$result[$item['source']] = [];
if(! isset($result[$item['source']][$item['target']]))
$result[$item['source']][$item['target']] = 0;
$result[$item['source']][$item['target']] += $item['total_volume'];
});
foreach($result as $key => $val) {
foreach($val as $k => $v) {
$result2[] = [$key,$k,$v];
}
}
print_r($result2);
Another alternative. Not best but will work.
$arr = [
['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 10 ],
['source' => 'ABC', 'target' => 'GHI', 'total_volume' => 5 ],
['source' => 'ABC', 'target' => 'DEF', 'total_volume' => 5 ],
];
$res = $resulrArr = [];
foreach($arr as $record){
$record = array_values($record);
$res[$record[0].'_'.$record[1]] = !empty($res[$record[0].'_'.$record[1]]) ? $res[$record[0].'_'.$record[1]] + $record[2] : $record[2];
}
$resulrArr = array_map(function($v,$k){
$data = explode('_',$k);
$resulrArr['source'] = $data[0];
$resulrArr['target'] = $data[1];
$resulrArr['total_volume'] = $v;
return $resulrArr;
},$res,array_keys($res));
echo '<pre>'; print_r($resulrArr);
I have an array with the output (print_r):
Array
(
[a] => 706
[b] => 194
[c] => 164
[d] => 44
[e] => 42
[f] => 41
[g] => 40
[h] => 38
[i] => 13
)
I need to convert it to a json of this format:
[{"name":"a","value":706},{"name":"b","value":194},{"name":"c","value":164},{"name":"d","value":44},{"name":"e","value":42},{"name":"f","value":41},{"name":"g","value":40},{"name":"h","value":38},{"name":"i","value":13}]
This is what I've tried but I'm just guessing and not getting the right output :
echo '[';
foreach($sortedArray as $key => $value) {
echo $dataTable = json_encode(array(
'name' => $key,
'value' => $value
), JSON_NUMERIC_CHECK);
};
echo ']';
$keyPair = array();
foreach($sortedArray as $key => $value) {
$keyPair[] = array('name' => $key, 'value' => $value);
}
echo json_encode($keyPair);
http://codepad.org/xApWlo7j
Try this:
$data = array(
'a' => 234, 'b' => 2343, 'c' => 23423, /* etc. */
);
$new_data = array();
foreach ($data as $k => $v) {
$new_data[] = array(
'name' => $k, 'value' => (number)$v
);
}
print json_encode($new_data);
Code
$arr = array(
'a' => 706,
'b' => 194,
'c' => 164,
'd' => 44,
'e' => 42,
'f' => 41,
'g' => 40,
'h' => 38,
'i' => 13);
$map_arr = array();
foreach ($arr as $k => $v)
$map_arr[] = array('name' => $k, 'value' => intval($v));
print json_encode($map_arr);
Result
[{"name":"a","value":706},{"name":"b","value":194},{"name":"c","value":164},
{"name":"d","value":44},{"name":"e","value":42},{"name":"f","value":41},
{"name":"g","value":40},{"name":"h","value":38},{"name":"i","value":13}]
I tried to link my example here: http://sandbox.onlinephpfunctions.com/, but the share CAPTCHA is disabled...
How can I group row data from a two-dimensional array and only create a subarray of another column if the respective group contains more than one value?
In other words, I need to group rows by id and conditionally restructure an array of arrays to have a variable depth of either 2 or 3 levels.
Input:
[
['id' => 567, 'value' => 780],
['id' => 676, 'value' => 743],
['id' => 676, 'value' => 721],
['id' => 234, 'value' => 766],
['id' => 234, 'value' => 680]
]
Desired output:
[
['id' => 567, 'value' => 780],
['id' => 676, 'value' => [743, 721]],
['id' => 234, 'value' => [766, 680]]
]
Are you sure you want to have the value as an integer when there is one value and an array when there are more?
<?php
$array = array(
array('id' => 567, 'value' => 780),
array('id' => 676, 'value' => 743),
array('id' => 676, 'value' => 721),
array('id' => 234, 'value' => 766),
array('id' => 234, 'value' => 680)
);
foreach ($array as $item) {
$result[$item['id']][] = $item['value'];
}
foreach ($result as $id => $value) {
if (count($value) > 1) {
$output[] = array(
'id' => $id,
'value' => $value
);
} else {
$output[] = array(
'id' => $id,
'value' => $value[0]
);
}
}
echo '<pre>';
print_r($output);
echo '</pre>';
?>
If not
<?php
$array = array(
array('id' => 567, 'value' => 780),
array('id' => 676, 'value' => 743),
array('id' => 676, 'value' => 721),
array('id' => 234, 'value' => 766),
array('id' => 234, 'value' => 680)
);
foreach ($array as $item) {
$result[$item['id']][] = $item['value'];
}
foreach ($result as $id => $value) {
$output[] = array(
'id' => $id,
'value' => $value
);
}
echo '<pre>';
print_r($output);
echo '</pre>';
?>
try this one
$array['key1'] = array(
0=>array('id'=>567, 'value'=>780),
1=>array('id'=>676, 'value'=>743),
2=>array('id'=>676, 'value'=>721),
3=>array('id'=>234, 'value'=>766),
4=>array('id'=>234, 'value'=>780)
);
foreach($array['key1'] as $subarray){
$group_id = $subarray['id'];
if(!isset($return[$group_id]))
$return[$group_id] = $subarray;
else{
if(is_array($return[$group_id]['value']))
array_push($return[$group_id]['value'], $subarray['value']);
else
$return[$group_id]['value'] = array($subarray['value'], $return[$group_id]['value']);
}
}
// reset grouping keys into 0,1...
$return = array_values($return);
print_r($return);
There, all the work done for you. How easy is that!
//create the array as you have now
$array[0] = ['id'=>567, 'value'=>780];
$array[1] = ['id'=>676, 'value'=>743];
$array[2] = ['id'=>676, 'value'=>721];
$array[3] = ['id'=>234, 'value'=>766];
$array[4] = ['id'=>234, 'value'=>780];
print_r($array);
print chr(10).chr(10);
//create a new array with the values combined on key
$concat = array();
foreach($array as $val) {
$i = $val['id'];
$v = $val['value'];
if (!is_array($concat[$i]))
$concat[$i] = array();
$concat[$i][] = $v;
}
print_r($concat);
print chr(10).chr(10);
//create a new array to show the data as you want.
$newarray = array();
foreach($concat as $key=>$val) {
$t = array();
$t['id'] = $key;
if (count($val)==1)
$t['value'] = $val[0];
else {
$t['value'] = array();
foreach($val as $v)
$t['value'][] = $v;
}
$newarray[] = $t;
}
print_r($newarray);
print chr(10).chr(10);
Result:
Array
(
[0] => Array
(
[id] => 567
[value] => 780
)
[1] => Array
(
[id] => 676
[value] => 743
)
[2] => Array
(
[id] => 676
[value] => 721
)
[3] => Array
(
[id] => 234
[value] => 766
)
[4] => Array
(
[id] => 234
[value] => 780
)
)
Array
(
[567] => Array
(
[0] => 780
)
[676] => Array
(
[0] => 743
[1] => 721
)
[234] => Array
(
[0] => 766
[1] => 780
)
)
Array
(
[0] => Array
(
[id] => 567
[value] => 780
)
[1] => Array
(
[id] => 676
[value] => Array
(
[0] => 743
[1] => 721
)
)
[2] => Array
(
[id] => 234
[value] => Array
(
[0] => 766
[1] => 780
)
)
)
<?php
$arr['key1'] = array(
array(
'id' => 567,
'value' => 780,
),
array(
'id' => 676,
'value' => 743,
),
array(
'id' => 676,
'value' => 721,
),
array(
'id' => 234,
'value' => 766,
),
array(
'id' => 234,
'value' => 780,
),
);
/* handle element merge */
function array_internal_merge_func($a, $b) {
if ( is_array($a['value']) )
$a['value'][] = $b['value'];
else
$a['value'] = array($a['value'], $b['value']);
return $a;
}
/* handle array merge */
function array_internal_merge($array, $key, $merge_func) {
$hashed = array();
$result = array();
foreach ( $array as $idx => $ele )
$hashed[$ele[$key]][] = $idx;
foreach ( $hashed as $key => $idxies ) {
reset($idxies);
$idx0 = current($idxies);
$result[$idx0] = $array[$idx0];
while ( $idx = next($idxies) )
$result[$idx0] = $merge_func($result[$idx0], $array[$idx]);
}
return $result;
}
print_r(array_internal_merge($arr['key1'], 'id', 'array_internal_merge_func'));
This task can certainly be done concisely with one loop.
Use id column values as temporary first level keys. This makes identifying duplicates most efficient and easy.
While iterating:
if a row's id is new to the result array, then push the whole row (in its original, flat structure) into the result array; or
if a row's id was encountered before, then cast the stored row's value element as an array before pushing the current row's value into that subarray.
If you do not want your result to have id values as the first level keys, then call array_values() to re-index the result.
The special action done implemented below is that when casting a scalar or null data type to an array, the value becomes the lone element of the newly formed array. If an array is explicitly cast as an array, then there is no effect on the data structure at all. This is why (array) can be unconditionally applied in the else branch.
Code: (Demo)
foreach ($array as $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['value'] = array_merge(
(array) $result[$row['id']]['value'],
[$row['value']]
);
}
}
var_export(array_values($result));
An alternative without array_merge(): (Demo)
foreach ($array as $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['value'] = (array) $result[$row['id']]['value'];
$result[$row['id']]['value'][] = $row['value'];
}
}
var_export(array_values($result));
An alternative with array_reduce(): (Demo)
var_export(
array_values(
array_reduce(
$array,
function($result, $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['value'] = (array) $result[$row['id']]['value'];
$result[$row['id']]['value'][] = $row['value'];
}
return $result;
}
)
)
);