Following is the array in the collection:
array:1 [▼
"online" => array:2 [▼
"IS-003" => array:19 [▼
"product" => Product {#831 ▶}
"quantity" => 1
"payment-option" => "online"
]
"HA4" => array:19 [▼
"product" => Product {#822 ▶}
"quantity" => 1
"payment-option" => "online"
]
]
]
Whenever a user changes the payment-option, accordingly the above array should change.
For instance, if HA4['payment-option'] is changed from online to cod, then there should be 2 arrays in parent array.
Following is the array that I want as result.
array:2 [▼
"online" => array:1 [▼
"IS-003" => array:19 [▼
"product" => Product {#831 ▶}
"quantity" => 1
"payment-option" => "online"
]
]
"cod" => array:1 [▼
"HA4" => array:19 [▼
"product" => Product {#822 ▶}
"quantity" => 1
"payment-option" => "cod"
]
]
]
The thing that I have tried so far but couldn't get the desired result:
$paymentOptionCart = collect();
foreach ($cart as $paymentType => &$details) {
foreach ($details as $c => $p) {
if ($c == $code) {
$details[$c]['payment-option'] = $request->option;
$paymentOptionCart->put($paymentType, $details);
unset($details[$c]);
}
}
}
On executing the above code, nothing happens except the payment-option is updated to cod.
I know I am making a silly mistake somewhere, but I am unable to locate where and how.
Can anybody help me out?
This should do your task:
$array = [
"online" => [
"IS-003" => [
"quantity" => 1,
"payment-option" => "online"
],
"HA4" => [
"quantity" => 1,
"payment-option" => "online"
]
]
];
$code = "HA4";
$request_option = "cod";
foreach ($array as $paymentType => $details) {
foreach ($details as $c => $p) {
if ($c == $code) {
$array[$request_option][$c] = $p;
$array[$request_option][$c]["payment-option"] = $request_option;
unset($array[$paymentType][$c]);
}
}
}
unset($details[$c]) seems to be the problem... this punches out the element at index 0, at the end of the first iteration - and index 1 should then become index 0, which subsequently will not be accessible at index 1, during the next iteration of the loop. just run the loop until the exit condition is met and then unset them all, not during the loop... or loop backwards, in order to keep the indexes intact; this would unset the last one element at first and the loop would not exceed the array boundaries.
having two different payment options within a single online order is rather an unlikely example, haven't seen this yet. people might rather post two orders (which implies, not only the business logic is flawed, but the array structure has an unfortunate design, which requires such messing around).
xdebug is great for understanding what is happening. even if my answer might not answer the question in code one can copy & paste (that's not my job), xdebug will tell you exactly what the problem is.
Related
I am trying to get the last 12 months of page insights data like unique. I am getting an array with the fields on the returned results, but it seems they are only for that day, or maybe I am mistaken; the response also contains pagination; when I go on the previous link, it only returns. I may be missing something, but I'd appreciate it if anyone could assist or explain it to me. The following is the function that makes the API response.
public static function getMultipleMetrics($pageID, $pageAccessToken, $metric)
{
$current = Carbon::now();
$getURL = "https://graph.facebook.com/" . $pageID . "/insights";
$params = [
"access_token" => $pageAccessToken,
"metric" => $metric,
"period" => "days_28",
"since" => $current->subMonths(12),
"until" => $current
];
$response = Http::get($getURL, $params);
return $response;
}
The response
array:2 [▼
"data" => array:2 [▼
0 => array:6 [▼
"name" => "page_impressions"
"period" => "days_28"
"values" => array:2 [▼
0 => array:2 [▼
"value" => 72
"end_time" => "2022-04-04T07:00:00+0000"
]
1 => array:2 [▼
"value" => 69
"end_time" => "2022-04-05T07:00:00+0000"
]
]
"title" => "28 Days Total Impressions"
"description" => "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content o ▶"
"id" => "pageid/insights/page_impressions/days_28"
]
1 => array:6 [▶]
]
"paging" => array:2 [▼
"previous" => "https://graph.facebook.com/v13.0/pageid/insights?access_token="
"next" => "https://graph.facebook.com/v13.0/pageid/insights?access_token=
]
]
Only 90 days of insights can be viewed at one time when using the since and until parameters.please refer to Meta page insight limitation section
php unset is working but on reload the session data is still there:
I'm building simple addtocart and remove from cart in php/symfony5, but I'm unable to unset the array from multi dimension array in symfony session.
^ array:3 [▼
0 => array:1 [▼
"items" => array:4 [▼
0 => 9
1 => "Dry fruit"
2 => "1234"
3 => "250g"
]
]
1 => array:1 [▼
"items" => array:4 [▼
0 => 8
1 => "Pumpkin"
2 => "123"
3 => "x"
]
]
This is my cart controller:
$basket = $session->get('basket',[]);
dump($basket);
$size = $session->get('size');
if($request->isMethod('POST')){
$id = $request->request->get('0');
foreach($basket as $key => $value){
if($value['items'][0] == $id){
unset($basket[$key]['items']);
dd($basket);
//$session->set('basket',[]);
//return $this->redirectToRoute('cart');
}
}
}
On the above code when I click remove button in twig and when dd($basket) is done the array is empty like below:
^ array:3 [▼
0 => [] //empty
1 => array:1 [▼
"items" => array:4 [▼
0 => 8
1 => "Pumpkin"
2 => "123"
3 => "x"
]
]
But, when I comment dd($basket) and page is reloaded the array is as it is like before(seems reverted idk) and if I uncomment $session->set('basket',[]); everything is empty.
what I want to achieve here is, how to remove array from session multi dimension array?
You simply need to set the session variable to the updated $basket value.
// get a copy of the basket from the session
$basket = $session->get('basket',[]);
// do whatever to update $basket here
// overwrite the basket in the session
$session->set('basket', $basket);
// then return a Response
i have form to make a bill for multiple products,
that form return request like this
#parameters: array:5 [▼
"quantity" => array:2 [▼
0 => "1"
1 => "2"
]
"product" => array:2 [▼
0 => "Mr. Jasen Beer,OliveDrab,XS"
1 => "Carlotta Yundt"
]
"date" => array:2 [▼
0 => "2019-12-29"
1 => "2019-12-29"
]
"id" => array:2 [▼
0 => "15"
1 => "11"
]
]
}
i need to loop all arrays to make insert all at once
thanks.
Try to loop it like this, you will get the array wrap every product's attributes:
$inserted_array = [];
foreach($parameters as $key => $values) {
foreach($values as $index => $val) {
$inserted_array[$index][$key] = $val;
}
}
\DB::table('table_name')->insert($inserted_array);
And I found that there is id in your array, remember to make it fillable.
I have a problem to passing array of arrays from controller to view in Laravel. I've done some research but none of topics helped. My tables are Shops, Items, Items Price. Shops contains shop id, which I get for use from url application/id. In Items Price I got information like shop_id , item_id (these two are FK), price. This table shows which items are in which shops. And in Items I have information about items: id ,picture. When I go to application/1, I want site to show items, which are in this specific shop, information.
My controller method:
public function getItems($id)
{
$items=ItemPrice::where('shop_id', $id)->select('item_id')->get()->toArray();
foreach($items as $item)
$products[] = array(Item::where('id',$item)->get()->toArray());
$shops=Shop::all();
return view('shop')->with(compact(['products','shops']));
}
when I debugging array with dd($products); I get:
array:4 [▼
0 => array:1 [▼
0 => array:1 [▼
0 => array:5 [▼
"id" => 1
"name" => "Item1"
"price" => 0.8
"type" => 2
"img_dir" => "svg/d.jpg"
]
]
]
1 => array:1 [▼
0 => array:1 [▼
0 => array:5 [▼
"id" => 2
"name" => "Item2"
"price" => 1.1
"type" => 2
"img_dir" => "svg/d2.jpg"
]
]
]
2 => array:1 [▼
0 => array:1 [▼
0 => array:5 [▼
"id" => 3
"name" => "Item3"
"price" => 3.1
"type" => 5
"img_dir" => "svg/p1.jpg"
]
]
]
3 => array:1 [▼
0 => array:1 [▼
0 => array:5 [▼
"id" => 4
"name" => "Item4"
"price" => 1.56
"type" => 5
"img_dir" => "svg/p2.jpg"
]
]
]
]
In view I do foreach #foreach($products as $product) and I get error:
Trying to get property 'img_dir' of non-object.
Any help would be appreciated.
Try like this
public function getItems($id)
{
$items = ItemPrice::where('shop_id', $id)
->select('item_id')
->pluck('item_id')
->toArray();
$products = Item::whereIn('id', $items)->get();
$shops = Shop::all();
return view('shop', compact('products','shops'));
}
You have some nested arrays in $products. What you think is a product is in fact an array. Maybe if you simplify your $products variable content :
public function getItems($id) {
$items = ItemPrice::where('shop_id', $id)->select('item_id')->get()->toArray();
$products = [];
foreach($items as $item) {
$products[] = Item::where('id',$item)->get();
}
$shops = Shop::all();
return view('shop')->with(compact(['products', 'shops']));
}
Look at this line
$products[] = array(Item::where('id',$item)->get()->toArray());
$products is an array, and you assign a new element which is an array made by the array value of your query (which contains array).
So you have a 3-level nested array which leads to confusion.
Why don't you just send to your blade view $products = Item::where('id',$item)->get(); ?
I have some input fields in my blade that looks like this:
<input type="text" name="product[0][name]">
<input type="text" name="product[0][price]">
<input type="text" name="product[1][name]">
<input type="text" name="product[1][price]">
if I submit the form, I get this back if I dd(Input::get("product)):
MY PRODUCT ARRAY
array:2 [▼
0 => array:2 [▼
"name" => "unicorn"
"price" => 5000
]
1 => array:2 [▼
"name" => "house"
"price" => 10000
]
]
Now I have another array, that looks like this:
API PRODUCT ARRAY
array:80 [▼
0 => array:18 [▼
"name" => "john doe for sale"
"price" => 120
...
]
1 => array:18 [▼
"name" => "house"
"price" => 12000
..
]
3 => array:18 [▼
"name" => "unicorn"
"price" => 5100
...
]
4 => array:18 [▼
"name" => "blabla"
"price" => 60000
...
]
And now my problem
I want to check if in "My Product Array" are products with the same name, like in the "API Product Array"
And if this should be the case, then it should be checked if the price of one of the products from the "API Products Array" is higher than that of the "My Products Array".
If this happens, this product should be added to a UserNotificationProducts array.
The code I made looks like this, but haven't worked for me at all.
Here I need some help.
foreach ($apiproducts as $key => $product)
{
if (in_array($product["name"], $myproducts["name"]))
{
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}
The "My Product Array" have a maximum of three products. The API Products array up to 100.
Thanks!
I tried both answers out and calculated the time cost of both solutions.
The answer of Istiaque Ahmed needed 0.15902519226074 ms
The answer of Hamelraj ( I tried exactly what you did before you posted your answer :D ) needed 0.031232833862305 ms
So the answer of hamelraj is the quicker solution. Thanks both of you!
$productnotification = [];
foreach ($apiproducts as $api)
{
foreach ($myproduct as $product)
{
if($product['name'] == $api['name'] && $api['price'] > $product['price'])
array_push($productnotification, $product["name"]);
}
}
You wrote if (in_array($product["name"], $myproducts["name"])). But $myproducts is an array of arrays. So you do not get any name from there. The following code might help:
foreach ($apiproducts as $key => $product)
{
$product_name=$product["name"];
for($i=0; $i<count($myproducts); $i++){
if (strcasecmp($product_name,$myproducts[$i]['name']==0) ){
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}// end of for loop
}