From the following array I am trying to multiple the quantity and the price on the condition that itemType is Zo.
(
[customer] => 4
[itemNo] => Array
(
[0] => 1
[1] => 2
)
[itemName] => Array
(
[0] => Type A
[1] => Type B
)
[itemType] => Array
(
[0] => Zo
[1] => Ram
)
[quantity] => Array
(
[0] => 2
[1] => 3
)
[price] => Array
(
[0] => 500
[1] => 2000
)
)
Here is what I have tried so far but with no success.
$lq = 0;
$total =0;
for ($i=0;$i<count($_REQUEST['price']);$i++) {
if(in_array("Ram", $_REQUEST['itemType'])){
$total += $_REQUEST['price'][$i] * $_REQUEST['quantity'][$i];
}else{
$lq += $_REQUEST['quantity'][$i];
}
}
echo ($total).'<br>';
echo ($lq);
My expected output is:
$total = 1000;//Quantity x Price
$lq = 3//Quantity only
You're not checking itemType of the same item that you're adding to the total. You're just checking whether any of the items have that itemType. You're also looking for Ram, not Zo.
if ($_REQUEST['itemType'][$i] == 'Zo') {
$total += $_REQUEST['price'][$i] * $_REQUEST['quantity'][$i];
} else {
$lq += $_REQUEST['quantity'][$i];
}
Try:
$data = $_REQUEST;
$key = array_search('Zo',$data['itemType']);
$total = $data['quantity'][$key] * $data['price'][$key];//Zo price*qty
$lq = array_sum($data['quantity']) - $data['quantity'][$key];//non-Zo qty
Live demo
Related
I have this array in PHP:
[data] => Array
(
[BOO_item_quantity] => Array
(
[0] => 1
[1] => 6
)
[BOO_item_id] => Array
(
[0] => 18
[1] => 13
)
[BOO_item_price] => Array
(
[0] => 3
[1] => 0
)
)
How is it possible to loop into this to get something like this please ?
Quantity = 1 - Item_Id = 18 - Price = 3
Quantity = 6 - Item_Id = 13 - Price = 0
Thanks.
You can use a foreach statement to loop over the elements the first sub-array:
$data = array(
'BOO_item_quantity' => array(1,6),
'BOO_item_id' => array(18,13),
'BOO_item_price' =>array(3,0)
);
foreach($data['BOO_item_quantity'] as $index=>$value) {
print 'Quantity = '.$value.' - Item_Id = '.$data['BOO_item_id'][$index].' - Price = '.$data['BOO_item_price'][$index].'<br />';
}
See it in action at phpfiddle.
I am trying to create a calculation script from a JSON array (can be more or less entries) where the amount of weeks and seasons have points, and all together it should output a total, but not larger than 50.
Somewhere i make a mistake as my result is 0. As i am quite new to PHP this script already took me some time :-) Eager to learn.. can someone point me what i am doing wrong?
This is the array:
Array
(
[experiences] => Array
(
[0] => Array
(
[quantity] => 1
[unit] => seasons
[description] => skischool 1
)
[1] => Array
(
[quantity] => 5
[unit] => weeks
[description] => skischool 2
)
[2] => Array
(
[quantity] => 3
[unit] => seasons
[description] => skischool 3
)
[3] => Array
(
[quantity] => 2
[unit] => weeks
[description] => skischool 4
)
)
)
And here is my idea of the script:
$incoming = json_decode($text, true);
$experiences = Sanitize::getVar($incoming, 'experiences');
$total = 0;
$weeks_points = 0.5;
$seasons = 5;
if(!empty($experiences['experiences'])) {
foreach($experiences['experiences'] as $experience) {
if($experience['unit'] == 'seasons') {
$total = $total + ($experience['quantity'] * $sessons);
} else if($experience['unit'] == 'weeks') {
$total = $total + ($experience['quantity'] * $weeks_points);
}
}
}
$total = round($total);
echo $total = $total > 50 ? 50 : $total;
I may be wrong, but shouldn't the if and the loop look like this?
if(!empty($experiences)) {
foreach($experiences as $experience) {
The first step is to create an new array with zeros. This is the code:
$amounts = [];
$row = [];
for($a = 0; $a < count($receipts_with_total); $a++){
for($b = 0; $b < count($taxes); $b++){
$row[$b] = 0;
}
$amounts[] = $row;
}
Then, i proceede to fill the array with values. The problem is, for some reason i don't know, it adds some indexs.
The code to fill the array is the next one:
//We calculate all the taxes amounts
for($i = 0; $i < count($receipts_with_total); $i++){
$this_receipt = $receipts_with_total[$i];
//We get all the taxes for each receipt
$taxes = $this_receipt->taxes;
for($j = 0; $j < count($taxes); $j++){
$this_tax = $taxes[$j];
if($this_tax->name == "IVA 21%"){
$amounts[$i][$j] = round((($this_tax->value * $total[$i]) / 100), 2);
}
elseif($this_tax->name == "IVA 10.5%"){
$amounts[$i][$j+1] = round((($this_tax->value * $total[$i]) / 100), 2);
}
else {
$amounts[$i][$j+2] = round((($this_tax->value * $total[$i]) / 100), 2);
}
}
}
And the outputs are:
Creacion
Array ( [0] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [1] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [2] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [3] => Array ( [0] => 0 [1] => 0 [2] => 0 ) )
Modelo
Array ( [0] => Array ( [0] => 0 [1] => 257.46 [2] => 61.3 ) [1] => Array ( [0] => 0 [1] => 40.36 [2] => 9.61 ) [2] => Array ( [0] => 80.73 [1] => 40.36 [2] => 9.61 ) [3] => Array ( [0] => 211.05 [1] => 105.53 [2] => 0 ) )
Lleno
Array ( [0] => Array ( [0] => 0 [1] => 257.46 [2] => 0 [3] => 61.3 ) [1] => Array ( [0] => 0 [1] => 40.37 [2] => 0 [3] => 9.61 ) [2] => Array ( [0] => 80.73 [1] => 0 [2] => 40.37 [4] => 9.61 ) [3] => Array ( [0] => 211.05 [1] => 0 [2] => 105.53 ) )
The first output is the new array with zeros. The second one is an example of as should be the final array with the calculate numbers. The last one is the array really i get.
As you can see, the index in bold represent the errors. For example, the value "61.3" is in fourth position in the first array, instead of third, it would be the correct.
Thanks!
Remove the +1 and +2 from the code.
Just
$amounts[$i][$j]=...
in all cases.
Because if i.e.
$j=2;
it may be become 3 in your code $j+1
My answer just pick that part of your question:
The problem is, for some reason i don't know, it adds some indexs.
I guess you want to show the "IVA 21" always in the 0 index in subarray and "IVA 10.5" always in the 1 index in sub array, and so on...? So you don't have to +1 or +2 in the index...cuz $j has already been increment in the for loop...
Or if you don't know which comes first or maybe you will have more option later, do not use a for loop. Use php foreach and keep +1 manually
$j = 0;
foreach ($taxes as $$this_tax) {
if ($this_tax->name == 'IVA 21%') {
$amounts[$i][$j] = round((($this_tax->value * $total[$i]) / 100), 2);
} elseif ($this_tax->name == 'IVA 10.5%') {
$amounts[$i][$j + 1] = round((($this_tax->value * $total[$i]) / 100), 2);
} else {
$amounts[$i][$j + 2] = round((($this_tax->value * $total[$i]) / 100), 2);
}
//myabe +3 later...
}
Or why not just use a static number like 0,1,2 if you always know the length of $taxes and where you gonna put for your results. You can even create conts like:
define('IVA21', 0); // const IVA21 = 0;
define('IVA105', 1);
// ... more define
//for loop starts
if ($this_tax->name == 'IVA 21%') {
$amounts[$i][IVA21] = round((($this_tax->value * $total[$i]) / 100), 2);
}
I've a series of arrays with values that goes from 1 to 5. Almost every array has missing values, some even dont have any values. My objective is to fill the missing values with 0. All those arrays are stored into a multidimensional array.
My array looks like:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 5
)
[3] => Array
(
[0] => (this array has no values)
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
etc...
)
How it should be:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0
[4] => 5
)
[3] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
etc...
)
Any help would be appriciated!
For each of your subarrays loop through the numbers 1 to 5, and if that value exists set its key to be one less than its value:
$newarray = array();
foreach($arr as $key => $subarr) {
for ($i = 1; $i <= 5; $i++) {
if (in_array($i, $subarr)) $newarray[$key][$i - 1] = $i;
else $newarray[$key][$i - 1] = 0;
}
}
Where $newarray is your output and $arr is your input array.
You may want to note that PHP does not truly do multidimensional arrays. It only allows you to relate 2 flat arrays together which is not true multidimensionality.
This does not work and will produce results described above.
$menu[1] = "My Training"; //not $menu[1][0]
$menu[1][1] = "To Do List";
$menu[1][2] = "Catalog";
$menu[1][3] = "Self-Report";
$menu[1][4] = "Completions";
$menu[2] = "Manager";
$menu[2][1] = "Direct Reports";
$menu[2][2] = "Incompletes";
$menu[2][3] = "Completions";
$menu[3] = "Instructor";
$menu[3][1] = "My Classes";
$menu[3][2] = "Printables";
$menu[3][3] = "Qualifications";
This does work.
$menu[1] = "My Training"; //not $menu[1][0]
$submenu[1][1] = "To Do List";
$submenu[1][2] = "Catalog";
$submenu[1][3] = "Self-Report";
$submenu[1][4] = "Completions";
$menu[2] = "Manager";
$submenu[2][1] = "Direct Reports";
$submenu[2][2] = "Incompletes";
$submenu[2][3] = "Completions";
$menu[3] = "Instructor";
$submenu[3][1] = "My Classes";
$submenu[3][2] = "Printables";
$submenu[3][3] = "Qualifications";
$submenu is only related to $menu through the first key number as there are no first dimension values to $submenu.
Something like this (array_pad() won't do the trick). $myArray is your source array. Completed array is returned in $result:
$result = array();
foreach( $myArray as $subKey=>$subArray ) {
for( $i=0; $i<5; $i++ ) {
if( isset( $subArray[$i] )) {
$result[$subKey][$i] = $subArray[$i];
} else {
$result[$subKey][$i] = 0;
}
}
}
Note, we do copy of the array. You cannot fill array in-place.
It's been many years since I wrote any PHP but something like this might do the trick I guess?
for($i = 0; $i < 5; $i++)
{
if(empty($myArray[$i])
{
$myArray[$i] = 0;
}
}
I am trying to sort an array sent from an XML feed.
The Array looks like this from print_r($answer);:
Array
(
[size] => Array
(
[0] => 1.5m x 1.5m
[1] => 1.5m x 3m
[2] => 3m x 6.0m
[3] => 3m x 2.3m
)
[rate] => Array
(
[0] => 80
[1] => 135
[2] => 295
[3] => 180
)
[sortorder] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
)
.
.
.
)
I want to get out the array:
Array
(
[size] => Array
(
[0] => 3m x 6.0
[1] => 3m x 2.3m
[2] => 1.5m x 1.5m
[3] => 1.5m x 3m
)
[rate] => Array
(
[0] => 295
[1] => 180
[2] => 80
[3] => 135
)
[sortorder] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
.
.
.
)
What I am trying to do is use the Sort Order sub array to display the items in that order
I have tried a number of uasort() and array_multisort() examples, but all seem to order the sub arrays and not the values inside the sub arrays
Any ideas will be a great help. Cheers
This one sorts $answer['sortorder'] and uses those keys to sort the rest of $answer without restructuring first.
// sort $answer['sortorder'] and retrieve indices.
asort($answer['sortorder']);
$idx = array_keys($answer['sortorder']);
// do sorting
$sorted = array();
foreach($answer as $key=>$subarr) {
if ($key != 'sortorder') { // don't sort
foreach($idx as $i) {
$sorted[$key][] = $subarr[$i];
}
} else {
// $answer['sortorder'] is already sorted.
$sorted[$key] = $subarr;
}
}
print_r($sorted);
See it in action here.
This approach will re-structure the array with the sort order being the index, sort the array, then return it to it's original structure.
echo '<pre>';
$array['size'][0] = '1.5m x 1.5m';
$array['size'][1] = '1.5m x 3m';
$array['size'][2] = '3m x 6.0m';
$array['size'][3] = '3m x 2.3m';
$array['rate'][0] = 80;
$array['rate'][1] = 135;
$array['rate'][2] = 295;
$array['rate'][3] = 180;
$array['sortorder'][0] = 3;
$array['sortorder'][1] = 4;
$array['sortorder'][2] = 1;
$array['sortorder'][3] = 2;
$temp = array();
foreach($array['sortorder'] as $key => $value)
{
$temp[$array['sortorder'][$key]] = array(
'size'=>$array['size'][$key],
'rate'=>$array['rate'][$key],
'sortorder'=>$array['sortorder'][$key]
);
}
ksort($temp);
$array = array();
foreach($temp as $key => $value)
{
$array['size'][] = $value['size'];
$array['rate'][] = $value['rate'];
$array['sortorder'][] = $value['sortorder'];
}
print_r($array);
May I propose to use a different array structure that bundles each size, rate and sort order into one item:
array(
array('size' => '...', 'rate' => '...', 'sort order' => '...'),
...
)
That makes it trivial to sort, and in fact easier to work with in general.
This PHP 5.3+ code does this transformation and sorting:
$answer = array_map(function ($size, $rate, $sortorder) {
return compact('size', 'rate', 'sortorder');
}, $answer);
usort($answer, function ($a, $b) { return $a['sortorder'] - $b['sortorder']; });