Sum of Dynamic Multi-Dimension array in PHP - php

I searched for few in-built methods and tried few logics from here and there but none can make me satisfied. I have created my own logic but is limited to a certain dimension of the array. I have managed to solve my problem for the project but as I said I am not satisfied.
Consider the array below:
$data = array
(
'0' => array
(
'0' => array
(
'make_ready' => '',
'ready_time' => '0.55',
'rate' => '46',
'no_of_run' => '',
'fixed_cost' => '',
'variable_cost' => '25.3',
'0' => array(
'kg' => 2.66,
'rate' => 11.4,
'fixed_cost' => '',
'variable_cost' => 30.32,
'0' => array(
'kg' => 2.66,
'rate' => 11.4,
'fixed_cost' => '',
'variable_cost' => 30.32,
),
),
),
),
'1' => array
(
'0' => array
(
'make_ready' => '1',
'ready_time' => '1.16',
'rate' => '36.47',
'no_of_run' => '',
'fixed_cost' => '36.47',
'variable_cost' => '42.31',
),
),
'2' => array
(
'make_ready' => '2',
'ready_time' => '0.29',
'rate' => '360',
'no_of_run' => '',
'fixed_cost' => '720',
'variable_cost' => '104.4',
),
'size' => '1000 X 1200 X 1190',
'up' => '3 X 4 / 17',
'unit' => '4',
'rate' => 16.32,
'fixed_cost' => '',
'variable_cost' => 65.28,
);
Problem:
I want to sum up all the values of array element where key is 'variable_cost', no matter how deep (in terms of dimension) is the array. Basically like a loop that can scan all the elements and do 'sum' if key matches.
I would like someone to help/suggest any logic where the calculations can be done till nth dimension.
For example like the movie Inception, in which they can go
dream->with in a dream->to nth number of dream.
and come out with the sum of 'variable_cost'. Hope you guys can understand the question here.
Thank you.

You will need to use a recursive function to sum all the variable_cost values:
function sum_variable_cost( $data, $total=0) {
foreach ($data as $key => $item) {
if (is_array($item)) {
$total = sum_variable_cost($item, $total);
} else if ($key=='variable_cost') {
//echo $key . " " . $item . "\n";
$total += $item;
}
}
return $total;
}
echo sum_variable_cost( $data );
Basically it loops through the array and sees if there are other arrays and only adds values that contain the key variable_cost.
If you uncomment the echo line it will show this output:
variable_cost 25.3
variable_cost 30.32
variable_cost 30.32
variable_cost 42.31
variable_cost 104.4
variable_cost 65.28
297.93

Related

php insert key/value into associative array

I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}

Optimizing algorithm to avoid exaggerated script execution time

Yesterday I ask here Append array values to another array by keys comparison and I have tried a few things without success. I have two arrays $result and $result1. count($result) is 204640 and count($result1) is 129849 so they are huge. The arrays is the result of a PDO statement execution as shown in code below. This is an example for $result array:
'00180000015oGSWAA2' =>
array (
'accountId' => '00180000015oGSWAA2',
'npi' => '1053576223',
'firstname' => 'Jack',
'lastname' => 'Cheng',
'title' => '',
'accountLastModifiedDate' => '2014-09-09 17:37:15',
'suffix' => '',
'fax' => '',
'address1' => '853 N CHURCH ST',
'city' => 'SPARTANBURG',
'stateLicensedId' => '31191',
'state' => 'SC',
'phone' => '',
'zip' => '29303',
'address2' => '',
'addressLastModifiedDate' => '2014-09-04 08:44:17',
),
'00180000015oGeXAAU' =>
array (
'accountId' => '00180000015oGeXAAU',
'npi' => '1629067301',
'firstname' => 'Fred',
'lastname' => 'Thaler',
'title' => '',
'accountLastModifiedDate' => '2014-09-09 17:36:41',
'suffix' => '',
'fax' => '',
'address1' => '1 PEARL ST',
'city' => 'BROCKTON',
'stateLicensedId' => '58249',
'state' => 'MA',
'phone' => '',
'zip' => '2301',
'address2' => '',
'addressLastModifiedDate' => '2014-09-04 04:25:44',
)
And this is an example for $result1 array:
'001S000000nBvryIAC' =>
array (
'tid' => '04T800000008zySEAQ',
'acsLastModifiedDate' => '2015-01-06 17:19:48',
),
'00180000015oGeXAAU' =>
array (
'tid' => '04T800000008zzgEAA',
'acsLastModifiedDate' => '2015-01-07 04:06:40',
),
'001S000000nYWcYIAW' =>
array (
'tid' => '04T800000008zySEAQ',
'acsLastModifiedDate' => '2015-02-25 15:45:01',
),
As you can see $result[1] and $result1[1] shares the same keys, right? Ok, then what I need is to push the content of $result1[1] on the end of $result[1] and getting something like:
'00180000015oGeXAAU' =>
array (
'accountId' => '00180000015oGeXAAU',
'npi' => '1629067301',
'firstname' => 'Fred',
'lastname' => 'Thaler',
'title' => '',
'accountLastModifiedDate' => '2014-09-09 17:36:41',
'suffix' => '',
'fax' => '',
'address1' => '1 PEARL ST',
'city' => 'BROCKTON',
'stateLicensedId' => '58249',
'state' => 'MA',
'phone' => '',
'zip' => '2301',
'address2' => '',
'addressLastModifiedDate' => '2014-09-04 04:25:44',
'tid' => '04T800000008zzgEAA',
'acsLastModifiedDate' => '2015-01-07 04:06:40',
)
Meaning when keys are equal in both arrays then merge or append values from the second one into the first one. Right now my best approach is the following:
// PDO statement
$result = $stmt->fetchAll();
// PDO statement
$result1 = $stmt->fetchAll();
foreach ($result as $key => $row) {
foreach ($result1 as $key1 => $row1) {
if ($key === $key1) {
array_push($row, array_shift($row1));
}
}
}
var_dump($row);
But it takes an eternity due to arrays length, so any advice in how to speedup this?
UPDATE: almost a solution
Based on #decese solution I have rewrite this a bit so my apologies in first and take a look to this. Now I called arrays as $oneArr and $twoArr and also I write a small output examples (don't kill me just yet):
// var_export($oneArr)
'00180000015oGSWAA2' =>
array (
'target_id' => '00180000015oGSWAA2',
'firstname' => 'Jack',
),
'00180000015oGeXAAU' =>
array (
'target_id' => '00180000015oGeXAAU',
'firstname' => 'Fred',
)
// var_export($twoArr)
'001S000000nBvryIAC' =>
array (
'tid' => '04T800000008zySEAQ',
'acsLastModifiedDate' => '2015-01-06 17:19:48',
),
'00180000015oGeXAAU' =>
array (
'tid' => '04T800000008zzgEAA',
'acsLastModifiedDate' => '2015-01-07 04:06:40',
)
This is the ouput I want to achieve, meaning when keys are equal in both arrays then merge or append values from the second one into the first one:
'00180000015oGeXAAU' =>
array (
'target_id' => '00180000015oGeXAAU',
'firstname' => 'Fred',
'tid' => '04T800000008zzgEAA',
'acsLastModifiedDate' => '2015-01-07 04:06:40',
)
I have this done, again based on first answer which I thanks a lot for clear a bit things to me, with the following code:
// Fetch results from first SQL query
$result = $stmt->fetchAll();
// Fetch only two keys from the entire and put them on $oneArr
foreach ($result as $row) {
$oneArr[$row['target_id']] = [
'target_id' => $row['target_id'],
'firstname' => $row['firstname']
];
}
// Fetch results from second SQL query
// yes, var names are the same not matter, I will only use one time
$result = $stmt->fetchAll();
// Fetch only two keys from the entire and put them on $twoArr
foreach ($result as $row) {
$twoArr[$row['target_id']] = [
'tid' => $row['tid'],
'acslmd' => $row['acslmd']
];
}
$i = 0;
foreach ($oneArr as $keyOneArr => $valueOneArr) {
if (array_key_exists($keyOneArr, $twoArr)) {
array_push($oneArr[$keyOneArr], $twoArr[$keyOneArr]);
$i++;
}
}
var_export($oneArr);
But result is not the same as I want since I got this:
'00180000015oGeXAAU' =>
array (
'target_id' => '00180000015oGeXAAU',
'firstname' => 'Fred',
0 =>
array (
'tid' => '04T800000008zzgEAA',
'acslmd' => '2015-01-07 04:06:40',
),
),
How I can avoid the extra array on the result array?
PS: Times looks good now: Call time was 00:01:34
Looks like your arrays are actually indexed by the ids, so do this:
foreach ($result1 as $key => $value) {
if (isset($result[$key])) {
$result[$key] += $value;
}
}
Of course, doing all this work in the database using a JOIN would make the most sense to begin with, if possible.
You could use array_merge_recursive
See my working example: http://3v4l.org/hQERW
Note: in the provided data, only 00180000015oGeXAAU is common to both arrays, so it only merges the values for that key
Basically, just call
$result = array_merge_recursive($result, $result1);
PHP.net says:
array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

Merge two Arrays by Values

I have this Array:
$mergedItems = array(
0 => array(
'id_item' => 'AZ-110'
'amount' => 12
),
1 => array(
'id_item' => 'BZ-110',
'amount' => 13
),
2 => array(
'id_item' => 'BZ-210',
'amount' => 28
),
3 => array(
'id_item' => 'CZ-291',
'amount' => 11
)
);
AND this Array:
$items = array(
0 => array(
'number' => 'AZ-110'
),
1 => array(
'number' => 'BZ-110'
),
2 => array(
'number' => 'CZ-291'
),
3 => array(
'number' => 'BZ-210'
)
);
Now what i want is to order the first array by the id_item Value to match the same order than the 2nd one by its values.
The resulting array has to include all values of the 2nd array AND the belonging amount-value of the first array. The Keys must not be kept!
I can't use array_merge since the 2nd Array has a dynamic amount of more items, so i only want all items from the second Array that are set in the first one.
Does anyone get what i mean? I am searching for a quick and non-dirty way to get this result as expected.
/Edit:
Expected Array:
$detailedItems = array(
0 => array(
'number' => 'AZ-110',
'amount' => 12
),
1 => array(
'number' => 'BZ-110',
'amount' => 13
),
2 => array(
'number' => 'CZ-291',
'amount' => 11
),
3 => array(
'number' => 'BZ-210',
'amount' => 28
)
);
A PHP 5.5 solution:
$itemMap = array_flip(array_column($mergedItems, 'id_item'));
$result = array_map(
function($i) use($itemMap, $mergedItems) {
return $mergedItems[$itemMap[$i['number']]];
},
$items);
print_r($result);
For 5.3 <= PHP < 5.5 you can simply substitute array_map for array_column:
$itemMap = array_flip(array_map(
function($i) { return $i['id_item']; },
$mergedItems));
How it works
The idea is to create a map of item numbers to indexes inside $mergedItems, ie.
[
'AZ-100' => 0,
'BZ-110' => 1,
'BZ-210' => 2,
// etc
]
With this information at hand it's very easy to iterate over $items (so that the result will be ordered based on that array) and pick the appropriate element from $mergedItems to append to the result each time.
$temp = $items;
foreach($temp as &$val)
{
foreach($mergedItems as $item)
{
if($item['id_item'] == $val['number'])
{
$val['amount'] = $item['amount'];
break;
}
}
}
print_r($temp);
There isn't really a "non-dirty" (meaning single line) way to do this as far as I know, but this function should work:
$out = array();
foreach ($mergedItems as $key => $value) {
if (array_key_exists($key, $detailedItems)) { // Make sure it exists to prevent errors
$out[$key] = $detailedItems[$key] + array('amount' => $value['amount']);
} else {
$out[$key] = $value['amount'];
}
}
print_r($out);
You can try following codes:
foreach ($mergedItems as $item) {
$merged[$item['id_item']] = array('amount' => $item['amount']);
}
foreach ($items as $item)
{
$detailedItems[] = array_merge($item, $merged[$item['number']]);
}
Output
var_dump($detailedItems);
array (size=4)
0 =>
array (size=2)
'number' => string 'AZ-110' (length=6)
'amount' => int 12
1 =>
array (size=2)
'number' => string 'BZ-110' (length=6)
'amount' => int 13
2 =>
array (size=2)
'number' => string 'CZ-291' (length=6)
'amount' => int 11
3 =>
array (size=2)
'number' => string 'BZ-210' (length=6)
'amount' => int 28

How do I get the index of a sub-document retrieved from a query?

I have a code:
'items' =>
array (
0 =>
array (
'name' => 'paste',
'qty' => 5,
'price' => 2.5,
),
1 =>
array (
'name' => 'soap',
'qty' => 5,
'price' => 2.5,
),
)
I am trying to write a query to find out the index of the value where the sub-document items has a data entry of name as "soap". Can anyone help me with the working solution?
I think you're after something along the lines of
foreach($items as $key => $value){
if($value['name'] == "soap") $output = $key;
}
Where $output would be the key of the array with item set to soap

PHP unserialize function get any number from 0-9

I need to get product id with the unserialize php funcion. I have this text
a:1:{i:4;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}
and I get the product_id with this PHP code:
$rslt = unserialize($data);
echo $rslt[4]["product_id"]);
So my question is there a way to do something like echo $rslt[x]["product_id"];where x is any number between 0-9
Also tried this but doesn't work
$i=0;
while($rslt[$i]["product_id"]!="")
{
echo $i;
//echo $rslt[4]["product_id"];
echo $rslt[$i]["product_id"];
$i++;
}
Once you unserialize your input you have a good old PHP array, equivalent to:
$rslt = array (
4 =>
array (
'quantity' => 1,
'product_id' => 5196,
'category_id' => '209',
'price' => 1,
'tax' => '18.00',
'tax_id' => '1',
'description' => '',
'product_name' => 'test',
'thumb_image' => '',
'ean' => '',
'attributes' => 'a:0:{}',
'attributes_value' =>
array (
),
'weight' => '0.0000',
'vendor_id' => '0',
'files' => 'a:0:{}',
'freeattributes' => 'a:0:{}',
'dependent_attr_serrialize' => 'a:0:{}',
),
);
To grab the first element, just call current() as with any other array:
$first_item = current($rslt);
print_r($first_item['product_id']);

Categories