Issue while looping an array in laravel view - arrays

Hi i have 2 arrays returned from a controller.
$order
array (size=2)
0 =>
array (size=2)
'orderid' => int 1
'ostatus' => string 'Placed' (length=6)
1 =>
array (size=2)
'orderid' => int 4
'ostatus' => string 'Placed' (length=6)
$orderdetails
array (size=2)
1 =>
array (size=3)
0 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro1-list.jpg' (length=33)
'brand_name' => string 'Puma' (length=4)
1 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro2-list.jpg' (length=33)
'brand_name' => string 'DressBerry' (length=10)
2 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro3-list.jpg' (length=33)
'brand_name' => string 'United Colors of Benetton' (length=25)
4 =>
array (size=1)
0 =>
array (size=2)
'oid' => int 4
'img6_path' => string 'images/product-list/pro4-list.jpg' (length=33)
'brand_name' => string 'Puma' (length=4)
I am trying to print it in this order:
This is what i have been doing:
foreach($order as $ord)
//printing order id
foreach($orderdetail as $od)
foreach($od as $x)
//printing the products
endforeach()
endforeach()
endforeach()
But this is printing
orderid 1 with 3 products
orderid 1 with 1 product (That belonged to orderid 4)
orderid 4 with 3 products (That belonged to orderid 1)
orderid 4 with 1 product
Can anyone tell me how should i loop through the array so that after printing the first 3 products in order1 the loop breaks out and then print the 1 product in order 4 (Like in the picture above). Thanks

#foreach($order as $ord)
//printing order id
#foreach($orderdetail[$ord->orderid] as $od)
#foreach($od as $x)
//printing the products
#endforeach
#endforeach
#endforeach
where #foreach($orderdetail[$ord->orderid] as $od) is the big change... I would give this a try...

Related

laravel 8 multiple array value by the index

i have 2 array, and all i want is multiple array value by index, example 0=> 1 * 0=>100000 , 1=> 2 * 1=>90000, etc. Is that possible ?
array 1 :
array:2 [▼
0 => 1
1 => 2
next value ...
]
array 2 :
array:2 [▼
0 => 100000
1 => 90000
next value ...
]
very grateful if someone helps.
$arr1 = [0 => 1, 1 => 2];
$arr2 = [0 => 100000, 1 => 90000];
foreach ($arr1 as $k => $val) {
$total[$k]=$val * $arr2[$k];
}
$total will return list of multiplied value, I assumed $arr2 and $arr1 has same key count.

PHP Why is only one array returned in multidimensional array after a foreach

So im trying to make a multidimensional array, inserting dates under their day. However the days only show one of their dates coming from the database. It also looks like its repeating itself. Eventually my goal is to get all dates under their respected day.
What I tried:
-array_push, trying to push the new data in the array.-A for loop
The code for handling
$currentDate = new Date();
$month = $currentDate->getCurrentDate()->format("m");
$year = $currentDate->getCurrentDate()->format("Y");
$availableCollection = new AvailabilitiesCollection();
try {
$availableCollection->add(Availability::getByMonthAndYear($this->db, $month, $year));
$availabilities = $availableCollection->get();
foreach ($availabilities as $this->availability ){
$this->date = new \DateTime($this->availability['date']);
$this->dayLabel = $this->date->format('D');
$this->dataDb[] = $this->dayLabel;
}
$count = array_count_values($this->dataDb);
foreach ($availabilities as $this->availability) {
$this->date = new \DateTime($this->availability['date']);
$this->dayLabel = $this->date->format('D');
$this->insertAvailabilityInDays[$this->dayLabel] = array();
$availableData = array(
'id' => $this->availability['reservation_id'],
'date' => $this->availability['date'],
'start_at' => $this->availability['start_at'],
'end_at' => $this->availability['end_at']
);
array_push($this->insertAvailabilityInDays[$this->dayLabel], $availableData);
// for($i = 0; $i < $count[$this->dayLabel]; $i++){
// $availableData = array(
// 'id' => $this->availability['reservation_id'],
// 'date' => $this->availability['date'],
// 'start_at' => $this->availability['start_at'],
// 'end_at' => $this->availability['end_at']
// );
//
// array_push($this->insertAvailabilityInDays[$this->dayLabel], $availableData);
// }
var_dump($this->insertAvailabilityInDays);
continue;
}
// var_dump($this->insertAvailabilityInDays);
// exit;
} catch (\Exception $e) {
$this->logger->error($e);
}
The data in my db:
+----------------+------------+----------+--------+
| reservation_id | date | start_at | end_at |
+----------------+------------+----------+--------+
| 6 | 2020-01-22 | 12:30 | 12:45 |
| 7 | 2020-01-20 | 12:50 | 13:00 |
| 8 | 2020-01-22 | 15:45 | 16:00 |
| 9 | 2020-01-21 | 14:45 | 15:00 |
+----------------+------------+----------+--------+
My current output:
array (size=1)
'Wed' =>
array (size=4)
'id' => string '6' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '12:30:00' (length=8)
'end_at' => string '12:45:00' (length=8)
/var/source/app/classes/System/Handlers/CalendarHandler.php:87:
array (size=2)
'Wed' =>
array (size=4)
'id' => string '6' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '12:30:00' (length=8)
'end_at' => string '12:45:00' (length=8)
'Mon' =>
array (size=4)
'id' => string '7' (length=1)
'date' => string '2020-01-20' (length=10)
'start_at' => string '12:50:00' (length=8)
'end_at' => string '13:00:00' (length=8)
/var/source/app/classes/System/Handlers/CalendarHandler.php:87:
array (size=2)
'Wed' =>
array (size=4)
'id' => string '8' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '15:45:00' (length=8)
'end_at' => string '16:00:00' (length=8)
'Mon' =>
array (size=4)
'id' => string '7' (length=1)
'date' => string '2020-01-20' (length=10)
'start_at' => string '12:50:00' (length=8)
'end_at' => string '13:00:00' (length=8)
/var/source/app/classes/System/Handlers/CalendarHandler.php:87:
array (size=3)
'Wed' =>
array (size=4)
'id' => string '8' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '15:45:00' (length=8)
'end_at' => string '16:00:00' (length=8)
'Mon' =>
array (size=4)
'id' => string '7' (length=1)
'date' => string '2020-01-20' (length=10)
'start_at' => string '12:50:00' (length=8)
'end_at' => string '13:00:00' (length=8)
'Tue' =>
array (size=4)
'id' => string '9' (length=1)
'date' => string '2020-01-21' (length=10)
'start_at' => string '14:45:00' (length=8)
'end_at' => string '15:00:00' (length=8)
I`m expecting that every date goes in the array with their day. Together with its database information.
E.g:
'Wed' =>
array (size=4)
0 =>
'id' => string '6' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '12:30:00' (length=8)
'end_at' => string '16:45:00' (length=8)
1 =>
'id' => string '8' (length=1)
'date' => string '2020-01-22' (length=10)
'start_at' => string '15:45:00' (length=8)
'end_at' => string '16:00:00' (length=8)
Like #TomShaw mentioned, indeed changing $this->insertAvailabilityInDays[$this->dayLabel][] = $availableData fixed the problem.

Cakephp 2 hash function to generate a new array

Hi i do receive an array in this format :
array (size=2)
0 =>
array (size=1)
22 => string 'One string' (length=20)
1 =>
array (size=1)
8 => string 'Another string' (length=17)
How can i use cakephp hash to create another array usign 8 and 22 as indexes :
array (size=2)
22 => string 'One string' (length=20)
8 => string 'Another string' (length=17)
I try hash::nest but the result is :
array (size=2)
0 =>
array (size=3)
0 => string 'One string' (length=17)
'children' =>
array (size=0)
empty
1 => string 'Another string' (length=20)
1 =>
array (size=3)
0 => string 'One string' (length=17)
'children' =>
array (size=0)
empty
1 => string 'Another string' (length=20)
This can be done with Hash::merge
$formattedArray = Hash::merge($array[0], $array[1]);

Cakephp - SQL not being generated for all records being inserted

I have the following code in my controller to insert records.
//Perform Inserts
$success = true;
foreach($toinsertrecords as $toinsertrecord) {
$this->BillingCenterDetail->create();
if (!$this->BillingCenterDetail->save($toinsertrecord)) {
$success = false;
echo "insert fail";
} else {echo "insert success";}
}
if ($troubleshoot) {
$log = $this->BillingCenterDetail->getDataSource()->getLog(false, false);
echo "<pre>Output from Datasource Log";
var_dump($log);
echo "</pre>";
}
The array being looped at, has the following contents (Note that the last record has the field order slightly switched, but as this is an associative array, i thought it should not matter)
array (size=3)
0 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'startdate' => string '2014-03-10' (length=10)
'enddate' => string '2014-03-10' (length=10)
'billing_center_id' => string '50' (length=2)
'isactive' => boolean false
'addr1' => string 'melbourne' (length=9)
'addr2' => string 'melbourne' (length=9)
'addr3' => string 'melbourne' (length=9)
'city' => string 'melbourne' (length=9)
'postcode' => string '777' (length=3)
'country' => string 'aus' (length=3)
'email' => string 'a#a.com' (length=7)
'phone' => string '1234' (length=4)
1 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'startdate' => string '2014-03-12' (length=10)
'enddate' => string '2028-12-10' (length=10)
'billing_center_id' => string '50' (length=2)
'isactive' => boolean false
'addr1' => string 'melbourne' (length=9)
'addr2' => string 'melbourne' (length=9)
'addr3' => string 'melbourne' (length=9)
'city' => string 'melbourne' (length=9)
'postcode' => string '777' (length=3)
'country' => string 'aus' (length=3)
'email' => string 'a#a.com' (length=7)
'phone' => string '1234' (length=4)
2 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'billing_center_id' => string '50' (length=2)
'startdate' => string '2014-03-11' (length=10)
'enddate' => string '2014-03-11' (length=10)
'isactive' => string '1' (length=1)
'addr1' => string 'test' (length=4)
'addr2' => string 'test' (length=4)
'addr3' => string 'test' (length=4)
'city' => string 'test' (length=4)
'postcode' => string 'test' (length=4)
'country' => string 'test' (length=4)
'email' => string 'test#test' (length=9)
'phone' => string 'test' (length=4)
This is the output from my getDataSource()->getLog statement
Output from Datasource Log
array (size=3)
'log' =>
array (size=4)
0 =>
array (size=5)
'query' => string 'SELECT `BillingCenterDetail`.`id`, `BillingCenterDetail`.`startdate`, `BillingCenterDetail`.`enddate`, `BillingCenterDetail`.`billing_center_id`, `BillingCenterDetail`.`isactive`, `BillingCenterDetail`.`addr1`, `BillingCenterDetail`.`addr2`, `BillingCenterDetail`.`addr3`, `BillingCenterDetail`.`city`, `BillingCenterDetail`.`postcode`, `BillingCenterDetail`.`country`, `BillingCenterDetail`.`email`, `BillingCenterDetail`.`phone`, `BillingCenterDetail`.`created`, `BillingCenterDetail`.`modified` FROM `bm`.`bil'... (length=718)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
1 =>
array (size=5)
'query' => string 'BEGIN' (length=5)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
2 =>
array (size=5)
'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-10', '2014-03-11', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a#a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
3 =>
array (size=5)
'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-13', '2028-12-10', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a#a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
'count' => int 4
'time' => float 0
You can clearly see that it has only tried to insert the first 2 records, but for some reason it hasn't inserted the 3rd record. Based on the log, it also didn't generate the 3rd Insert SQL statement?
By the time it hits the 3rd loop, the "Save" function fails, this causes $success = false in the end.
Can anyone guess why this is happening?
My guess is the data in the third entry isn't passing validation.
Possibly your model has the postcode field as numeric only? In that case this data will fail:
'postcode' => string 'test' (length=4)
To debug this, I'd add this line inside your existing failure routine:
var_dump($this->BillingCenterDetail->invalidFields());

Create array with Set Extract in Cakephp with conditions

I have the following array:
Array
(
[0] => Array
(
[id] => 4
[rate] => 82.50
[pounds] => 2
[ounces] => 3
[mailtype] => Package
[country] => UNITED KINGDOM (GREAT BRITAIN)
[svccommitments] => 1 - 3 business days
[svcdescription] => Global Express Guaranteed (GXG)
[maxdimensions] => Max. length 46", width 35", height 46" and max. length plus girth combined 108"
[maxweight] =>30
)
[1] => Array
(
[id] => 6
[rate] => 82.50
[pounds] => 2
[ounces] => 3
[mailtype] => Package
[country] => UNITED KINGDOM (GREAT BRITAIN)
[svccommitments] => 1 - 3 business days
[svcdescription] => Global Express Guaranteed Non-Document Rectangular
[maxdimensions] => Max. length 46", width 35", height 46" and max. length plus girth combined 108"
[maxweight] => 70
)
And I want to use CakePHP's Set:extract tools to filter this array on the 'maxweight', so all elements that have a 'maxweight' more than X and get an array made up of the 'rate' and 'svcdescription' fields ie:
Array (
[82.50] => Global Express Guaranteed Non-Document Rectangular
...
etc
)
Is this at all possible?
In my opinion, to get the most value out of Set::extract, it would be better to start with an array with a structure more like the following (otherwise I believe you'd have to run Set::extract inside a loop):
$array = array(
'Shipping' => array(
array (
"id" => 4,
"rate" => 82.50,
"pounds" => 2,
"ounces" => 3,
"mailtype" => "Package",
"country" => "UNITED KINGDOM (GREAT BRITAIN)",
"svccommitments" => "1 - 3 business days",
"svcdescription" => "Global Express Guaranteed (GXG)",
"maxdimensions" => 'Max. length 46", width 35", height 46" and max. length plus girth combined 108"',
"maxweight" => 30
),
array (
"id" => 6,
"rate" => 82.50,
"pounds" => 2,
"ounces" => 3,
"mailtype" => "Package",
"country" => "UNITED KINGDOM (GREAT BRITAIN)",
"svccommitments" => "1 - 3 business days",
"svcdescription" => "Global Express Guaranteed Non-Document Rectangular",
"maxdimensions" => 'Max. length 46", width 35", height 46" and max. length plus girth combined 108"',
"maxweight" => 70
)
)
);
Now you can use the path syntax for Set::extract() to extract elements that have a maxweight greater than $x.
$extracted = Set::extract($array, '/Shipping[maxweight>'.$x.']');
With this data, you can build the array you're looking for using the rates as keys and svcdescription as values using Set::combine().
$combined = Set::combine($extracted, '{n}.Shipping.rate', '{n}.Shipping.svcdescription');
I've never used this before, but thanks for encouraging me to read up on it.
Have you tried using Set::combine() ?
http://book.cakephp.org/view/662/combine
Why can't you just use a foreach loop to process through the array.
$returnArray = array();
// Where $x is weight amount you're testing against
foreach ($yourData as $eachData) {
if ($eachData['maxweight'] > $x) {
$returnArray[$eachData['rate']] = $eachData['svcdescription'];
}
}

Resources