I have this array
$a = [
0 => [
'period' => '2017/2018',
'product' => 'AM',
'quantity_1' => 20,
'quantity' => 25,
],
1 => [
'period' => '2018/2019',
'product' => 'AM',
'quantity_1' => 12,
'quantity' => 19,
],
2 => [
'period' => '2017/2018',
'product' => 'DC',
'quantity_1' => 20,
'quantity' => 25,
],
3 => [
'period' => '2018/2019',
'product' => 'DC',
'quantity_1' => 8,
'quantity' => 10,
]
]
The idea is to divide values by period and by product, in this case we have 2 products 'AM' && 'DC'. So divide values fom 2018/2019 to 2017/2018 for product AM and for product DC. I need to get the result like this :
$result = [
0 => [
'product' => 'AM'
'quantity_1' => 12/20 = 0.6
'quantity_2' => 19/25 = 0.76
],
1 => [
'product' => 'DC'
'quantity_1' => 8/20 = 0.4
'quantity_2' => 10/25 = 0.4
]
]
I tried with foreach but I think exist some other easy ways to do that. If you have some ideas I will appreciate this. Thank you for your time.
I tried like this :
$i = 0;
foreach ($results as $result){
$result[] = [
'product' => $result['product'],
'tonnes_prod' => $results[$i+1]['quantity_1'] / $results[$i]['quantity_1']
];
$i++;
}
But I get the error : #message: "Notice: Undefined offset: 28"
Remake your array first, and then calculate the result
$temp = [];
foreach ($a as $x){
$temp[$x['product']][$x['period']] = $x;
}
$result = [];
foreach ($temp as $key => $res){
$result[] = [
'product' => $key,
'quantity_1' => $res['2018/2019']['quantity_1'] / $res['2017/2018']['quantity_1'],
'quantity_2' => $res['2018/2019']['quantity'] / $res['2017/2018']['quantity'],
];
}
demo
Related
I've got an array of selected dates and an array of rooms with set dates and prices. From the array $selectedDates, it should look for a date from the rooms. If one of the $selectedDates is found from each room, it should use the price under the options keys if not it should use the default_price key.
$selectedDates = ['10-04-2022', '11-04-2022', '12-04-2022'];
$setRooms = [
0 => [
'id' => 1,
'title' => 'Room1',
'default_price' => 50,
'options' => [
0 => [
'date' => '12-04-2022',
'price' => 100,
],
1 => [
'date' => '13-04-2022',
'price' => 200,
],
3 => [
'date' => '14-04-2022',
'price' => 200,
],
]
],
1 => [
'id' => 2,
'title' => 'Room2',
'default_price' => 120,
'options' => [
0 => [
'date' => '11-04-2022',
'price' => 200,
],
1 => [
'date' => '12-04-2022',
'price' => 300,
],
3 => [
'date' => '13-04-2022',
'price' => 400,
],
]
],
]
Expected Output:
[
// '10-04-2022' = 50 (default_price), '11-04-2022' = 50 (default_price), '12-04-2022' = 100 (using the option price). 50 + 50 + 100 = 200
0 => [
'id' => 1,
'title' => 'Room1',
'total_price' => 200
],
// '10-04-2022' = 50 (default_price), '11-04-2022' = 200 (using the option price), '12-04-2022' = 300 (using the option price). 120 + 200 + 300 = 620
1 => [
'id' => 2,
'title' => 'Room2',
'total_price' => 620
]
]
This is what I've done so far. But it turns out that room 2 is giving me a different output, it's like the sum of room 1 will be added to room 2.
$price = 0;
$roomBookings = [];
foreach ($hotelRooms as $key => $room) {
$count = count($listOfSelectedDates);
foreach ($listOfSelectedDates as $selectedDate) {
$getPrice = HelperTrait::searchForDate($selectedDate, $room['options']);
if ($getPrice['success']) {
$price += $getPrice['price'];
}
else {
$price += $room['default_price'];
}
$room['total_price'] = $price;
}
$roomBookings[] = $room;
}
/////
public static function searchForDate($date, $array): array
{
foreach ($array as $key => $val) {
$date = Carbon::parse($date)->format('d-m-Y');
$arrDate = Carbon::parse($val['date'])->format('d-m-Y');
if ($arrDate === $date) {
return [
'success' => true,
'price' => $val['price']
];
}
}
return [
'success' => false
];
}
Let me know if you have any questions.
Thanks in advance!
$setRooms = [
[
'id' => 1,
'title' => 'Room1',
'default_price' => 50,
'options' => [
[ 'date' => '12-04-2022', 'price' => 100, ],
[ 'date' => '13-04-2022', 'price' => 200, ],
[ 'date' => '14-04-2022', 'price' => 200, ],
]
],
[
'id' => 2,
'title' => 'Room2',
'default_price' => 120,
'options' => [
[ 'date' => '11-04-2022', 'price' => 200, ],
[ 'date' => '12-04-2022', 'price' => 300, ],
[ 'date' => '13-04-2022', 'price' => 400, ],
]
],
];
$selectedDates = [ '10-04-2022', '11-04-2022', '12-04-2022' ];
$result = [];
foreach ($setRooms as $room) {
$total = 0;
foreach ($selectedDates as $date) {
$filtered = array_filter($room['options'], fn($option) => $date === $option['date']);
if (count($filtered) === 1) {
$total += $filtered[array_key_first($filtered)]['price'];
} else {
$total += $room['default_price'];
}
}
$result[] = [ 'id' => $room['id'], 'title' => $room['title'], 'total_price' => $total ];
}
print_r($result);
Output:
Array
(
[0] => Array
(
[id] => 1
[title] => Room1
[total_price] => 200
)
[1] => Array
(
[id] => 2
[title] => Room2
[total_price] => 620
)
)
So I have array like this one
[
'custid' => [
'customer_number_1' => '20098374',
'customer_number_8' => '20098037',
'customer_number_15' => '20098297'
],
'destid' => [
'destination_numbers_1' => [
(int) 0 => '20024838',
(int) 1 => '20041339'
],
'destination_numbers_8' => [
(int) 0 => '20008293'
],
'destination_numbers_15' => [
(int) 0 => '20016969',
(int) 1 => '20022919',
(int) 2 => '20025815',
(int) 3 => '20026005',
(int) 4 => '20027083',
(int) 5 => '20045497'
]
]
]
Goal is to merge cust id with destid in pairs and should look like so
[
(int) 0 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20024838'
],
(int) 1 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20041339',
],
(int) 2 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098037',
'sap_destination_id' => '20008293,
],
(int) 3 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098297'
'sap_destination_id' => '20016969',
],
...
I have tried with code below, but I am getting destination_id number as array and duplicated customer numbers. Also I have tried with array walk but result is same.
$data = [];
foreach ($sap_data['custid'] as $custid) {
foreach ($sap_data['destid'] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
Thx for helping!
You should make inner loop in other way
foreach($sap_data['custid'] as $k => $custid) {
// Make destination key
$dkey = str_replace('customer_number', 'destination_numbers', $k);
// And get array, for example, destination_numbers_1 for customer_number_1
foreach ($sap_data['destid'][$dkey] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
demo on eval.in
I need to compare elements in array which have same elment value ex. $array[0]['change'] == $array[1]['change'] and if it true than make array push.
I have array like this
[
(int) 0 => [
'id' => (int) 2,
'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 3,
'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 11
],
(int) 2 => [
'id' => (int) 4,
'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 17
],
(int) 3 => [
'id' => (int) 5,
'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 19
],
(int) 4 => [
'id' => (int) 6,
'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 10
],
(int) 5 => [
'id' => (int) 7,
'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
(int) 6 => [
'id' => (int) 8,
'uuid' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
'change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
'company_id' => (int) 19
],
(int) 7 => [
'id' => (int) 9,
'uuid' => '64e146bf-5d76-483f-992c-274cde1202ce',
'change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b',
'company_id' => (int) 20
]
]
I need to get result like this
[
(int) 0 => [
(int) 0 => [
'id' => (int) 2,
'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 3,
'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
],
(int) 1 => [
(int) 0 => [
'id' => (int) 4,
'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 5,
'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 18
],
],
(int) 2 => [
'id' => (int) 6,
'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
(int) 3 => [
'id' => (int) 7,
'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
.
.
.
]
I have tried to make 2 arrays one with same values and another with different values, than make merge but the result is not as I want... and I hope so that there is "nicer" solution (maybe more readable logic).
$exchange_array = [];
$item_array = [];
foreach ($order->items as $item) {
foreach ($order->items as $exchange) {
if (
isset($item->change) && isset($exchange->change) &&
$item->change == $exchange->change &&
$item->id != $exchange->id
) {
$exchange_array[] = $exchange;
}
if (
!isset($item->change) && !isset($exchange->change) &&
$item->id != $exchange->id
) {
$item_array[] = $exchange;
}
}
}
$item = array_merge($exchange_array,$item_array);
Thank you!
you may use foreach or array_map with this as follows :
$arrays = [
['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
['change' => '9a356688-d08b-42ee-b26d-19d76bf6543b'],
['change' => '8a356688-d08b-42ee-b26d-19d76bf6543b'],
['change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b'],
['change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b'],
];
$list = [];
foreach ($arrays as $array) {
$list[$array['change']][] = $array;
}
print_r(array_values($list));
// if you would like to use array_map instead
$list = [];
array_map(function ($array) use (&$list) {
$list[$array['change']][] = $array;
}, $arrays);
print_r(array_values($list));
live demo : https://3v4l.org/1JO58
Use the following approach:
$result = [];
foreach ($order->items as $item) {
if (isset($result[$item['change']])) {
// check if nested array is a single item(not multidimensional)
if (isset($result[$item['change']]['change'])) {
$result[$item['change']] = [$result[$item['change']], $item];
} else {
$result[$item['change']][] = $item;
}
} else {
$result[$item['change']] = $item;
}
}
$result = array_values($result);
I would like to detect same records and then change quantity of the one record and delete the others. For example, given the following array:
'Cart' => [
(int) 0 => [
'size' => '38',
'code' => '5',
'qn' => (int) 1
],
(int) 1 => [
'size' => '37',
'code' => '5',
'qn' => (int) 1
],
(int) 2 => [
'size' => '37',
'code' => '5',
'qn' => (int) 1
]
],
i would like to print:
'Cart' => [
(int) 0 => [
'size' => '38',
'code' => '5',
'qn' => (int) 1
],
(int) 1 => [
'size' => '37',
'code' => '5',
'qn' => (int) 2
]
],
It looks to me that you're trying to sum quantities (qn) on duplicate sizes (size) and codes (code). One way to achieve this is by looping through the array containing the cart items and building out a new array. I suggest reading about PHP arrays and array_key_exists to learn more as they're used below:
<?php
$arr = [
['size' => '38', 'code' => 5, 'qn' => 1],
['size' => '37', 'code' => 5, 'qn' => 1],
['size' => '37', 'code' => 5, 'qn' => 1],
['size' => '37', 'code' => 4, 'qn' => 1],
];
$newArr = [];
foreach ($arr as $value) {
$key = $value['size'] . ':' . $value['code'];
if (array_key_exists($key, $newArr)) {
$newArr[$key]['qn'] += $value['qn'];
continue;
}
$newArr[$key] = $value;
}
// Resets keys
$newArr = array_values($newArr);
print_r($newArr);
I dont know it is enough for your problem, but try function array_unique
Iterate your array and use array_diff on each subarray.
If the returned array doesn't contain neither size nor code, add the two qn
The function array_unique() works for single dimension array. To find unique elements from multi-dimension array, we need to do a little modification.
Try this:
$array = array_map("serialize", $array);
$output = array_map("unserialize", array_unique($array));
If you want to follow another approach, use this as a reference: Create multidimensional array unique
Please go through following code.
$result = [
0=> ['size' => '38',
'code' => '5',
'qn' => 1
],
1=> ['size' => '37',
'code' => '5',
'qn' => 1
],
2=> ['size' => '37',
'code' => '5',
'qn' => 1
]
];
$finalArr = [];
foreach($result as $k => $v) {
$flag = 0;
foreach($finalArr as $kc => $vc){
if($v['size']==$vc['size'] && $v['code']==$vc['code']){
$flag = 1;
$finalArr[$kc]['qn'] = $finalArr[$kc]['qn'] + 1;
break;
}
}
if($flag==0){
$finalArr[] =
[
'size' => $v['size'],
'code' => $v['code'],
'qn' => 1
];
}
}
echo "<pre>";
print_r($finalArr);
The code is tested against your question and i have explained the code. This gives you solution for any number of arrays and with similar array elements with quantity(qn) incremented as you wanted:
<?php
//Compare the 1st and 2nd array
//Compare the 2st and 3rd array
// and so on
function compare($arr1 = array(), $arr2 = array()) {
$result = array_diff($arr1, $arr2);
return $result;
}
$cart = array(
0 => array('size' => 38, 'code' => 5, 'qn' => 1),
1 => array('size' => 37, 'code' => 5, 'qn' => 1),
2 => array('size' => 37, 'code' => 5, 'qn' => 1),
);
$j = 1;
$cart_total_count = count($cart);//Gives the count of your cart
$final_cart = array();//Final array with qn incremented
for($i=0;$i<$cart_total_count; $i++) {
if (!empty($cart[$i+1])) {//If 2nd array is not present then stop the comparision
if (empty(compare($cart[$i], $cart[$i+1]))){
$j++;
$cart[$i]['qn'] = $j;
$final_cart = $cart[$i];
}
}
}var_dump($final_cart);
//Output
//1. If 2 array are same
//array(3) { ["size"]=> int(37) ["code"]=> int(5) ["qn"]=> int(2) }
//2. If no array are same
//array(0) { }?>
I've been stuck trying to figure this out for a week now. I have an array in the following format:
[
1 => [
'name' => 'Maths',
'parent_category_id' => NULL
],
2 => [
'name' => 'Algebra',
'parent_category_id' => 1
],
3 => [
'name' => 'Expanding brackets',
'parent_category_id' => 2
],
4 => [
'name' => 'Factorising brackets',
'parent_category_id' => 2
],
5 => [
'name' => 'English',
'parent_category_id' => NULL
],
6 => [
'name' => 'Shakespeare',
'parent_category_id' => 5
]
]
and I want to transform it into an array in this format:
[
'Maths' => [
'category_id' => 1,
'questions' => [], //This array will then be filled with questions regarding each of the categories
'children_categories' => [
'Algebra' => [
'category_id' => 2,
'questions' => [],
'children_categories' => [
'Expanding brackets' => [
'category_id' => 3,
'questions' => [],
'children_categories' => []
],
'Factorising brackets' => [
'category_id' => 4,
'questions' => [],
'children_categories' => []
]
]
]
]
],
'English' => [
'category_id' => 5,
'questions' => [],
'children_categories' => [
'Shakespeare' => [
'category_id' => 6,
'questions' => [],
'children_categories' => []
]
]
]
]
So far I've been able to format categories that have no parents, but I for the life of me cannot figure out how to insert a category that has a parent into that parent's children_categories[] array. Here's the code I'm using, I need help figuring out what I should put in the "else" part of the foreach()
foreach($ids_as_keys as $category_id => $info){ //$info contains the name of the category, and the parent's ID (NULL if there is no parent)
if(is_null($info['parent_category_id'])){ //There is no parent, so put it at the root of $nested
$nested[$info['name']] = [
'category_id' => $category_id,
'questions' => [],
'children_categories' => []
];
}else{ //There is a parent, so search through all items (including sub-arrays, sub-sub-arrays etc.) until we find a match for the parent_category_id, and then add it into the children_categories[] array
}
}
return $nested;
I tested this, absolutely works :
$arr = array(
1 => array(
'name' => 'Maths',
'parent_category_id' => NULL
),
2 => array(
'name' => 'Algebra',
'parent_category_id' => 1
),
3 => array(
'name' => 'Expanding brackets',
'parent_category_id' => 2
),
4 => array(
'name' => 'Factorising brackets',
'parent_category_id' => 2
),
5 => array(
'name' => 'English',
'parent_category_id' => NULL
),
6 => array(
'name' => 'Shakespeare',
'parent_category_id' => 5
)
);
foreach ($arr as $key => &$value) {
if ($value['parent_category_id']) {
$arr[$value['parent_category_id']]['children_categories'][] = &$value;
}
else{
$parents[]=$key;
}
}
$result = array();
foreach ($parents as $val) {
$result[$val] = $arr[$val];
}
print_r($result);
This answer is too close may helpful for you
<?php
$array = [
1 => [
'name' => 'Maths',
'parent_category_id' => NULL
],
2 => [
'name' => 'Algebra',
'parent_category_id' => 1
],
3 => [
'name' => 'Expanding brackets',
'parent_category_id' => 2
],
4 => [
'name' => 'Factorising brackets',
'parent_category_id' => 2
],
5 => [
'name' => 'English',
'parent_category_id' => NULL
],
6 => [
'name' => 'Shakespeare',
'parent_category_id' => 5
]
];
//data array
$data = array();
$i = 0;
//gothrough one by one
foreach($array as $key=>$value)
{
//set the parent array
if(is_null($value['parent_category_id']))
{
$data[$value['name']]= array();
$data[$value['name']]['category_id'] = $key;
$data[$value['name']]['questions'] = array();
$data[$value['name']]['children_categories'] = array();
//add the childrens according to the parent
}elseif(array_key_exists($value['parent_category_id'], $array)){
//find the parent
$parent = $array[$value['parent_category_id']];
$data[$parent['name']]['children_categories'][$value['name']] = array();
$data[$parent['name']]['children_categories'][$value['name']]['category_id'] = $key;
$data[$parent['name']]['children_categories'][$value['name']]['questions'] = array();
}
}
//display the result
print_r($data);
Try using recursive function.
function insert_child($curArr,$childArray,&$parentArray){
foreach($parentArray as $key=>&$val){
if(is_array($val) && sizeof($val) > 0 ){
if($val['category_id']==$curArr['parent_category_id']){
$val['children_categories'][$curArr['name']] = $childArray;
return TRUE;
}else{
insert_child($curArr,$childArray,$val['children_categories']);
}
}
}
return FALSE;
}
Where $nest is your input array
$nest = array(
1=>array(
'name' => 'Maths',
'parent_category_id' => NULL
),
2=>array(
'name' => 'Algebra',
'parent_category_id' => 1
),
3=>array(
'name' => 'Expanding brackets',
'parent_category_id' => 2
),
4=>array(
'name' => 'Factorising brackets',
'parent_category_id' => 2
),
5=>array(
'name' => 'English',
'parent_category_id' => NULL
),
6=>array(
'name' => 'Shakespeare',
'parent_category_id' => 5
),
);
code
$result=array();
foreach($nest as $key=>$val){
$temp = array(
'category_id'=>$key,
'questions'=>array(),
'children_categories'=>array(),
);
if(!in_array($val['name'],$result) && $val['parent_category_id']==NULL){
$result[$val['name']] = $temp;
}else{
insert_child($val,$temp,$result);
}
}
echo "<pre>";
print_r($result);
exit();
PHPFIddle here