I have a JSON multi-dimensional array of products from Shopify that I am looping through. Each product has 3 options and each option can have infinite numbers of values.
For example:
Lamp (product)
Glass Finish (option)
Clear (value)
Smoke (value)
Metal Finish (option)
Polished Chrome (value)
Drop (option)
600mm (value)
800mm (value)
Within the loop I am creating rows within a 'repeater' field in a CMS (one for each option and then a repeater within each option for each value).
This is all fine BUT I want to do a few checks as the array of products is cached to every 30 minutes so if the cache was updated and either an option was removed or a value to that option was added then it should update.
I have my first check in place (loop through all the existing repeater options that have been added, before doing anything, and if an option already exists then skip it (and thus not creating multiple of the same options).
What I need to do is to check each of the values to see if any new ones have been added. I can write the code to actually add the value to the repeater field but I am unsure how to check as by this point, if the option already exists, it skips over.
Any thoughts?
foreach($product['options'] as $option) {
foreach($p->shop_product_options as $options) {
// If this option already exists... then skip the parent loop on this product
if ($options->global_text == $option['name']) {
continue 2;
}
}
$options = $p->shop_product_options->getNew();
$options->of(false);
$options->global_text = $option['name'];
$options->save();
$p->shop_product_options->add($options);
foreach($option['values'] as $o) {
$values = $options->shop_product_options_option->getNew();
$values->of(false);
$values->global_text = $o;
$values->save();
$options->save();
$options->shop_product_options_option->add($values);
}
$options->save();
}
Still not sure I am completely understanding what you are after but it sounds like you need something like this :
foreach($product['options'] as $option) {
$optionNames = array_column($options, 'name');
$options = $p->shop_product_options->getNew();
foreach($option['values'] as $o) {
$values = $options->shop_product_options_option->getNew();
if($values->global_text === $o && in_array($option->global_text, $optionNames, true)) {
continue 2;
}
$values->of(false);
$values->global_text = $o;
$values->save();
$options->shop_product_options_option->add($values);
}
$options->of(false);
$options->global_text = $option['name'];
$p->shop_product_options->add($options);
$options->save();
}
array_column will create an array getting all the values of a multidimensional array when the key is name, and then the if statement will look for a combination of name and value.
And since the saving logic for the option is below the loop for the values,
it will check the cache before saving anything, this might not be a copy/paste answer, i'm just giving you some logic to work with
Related
I've seen a few questions and the ones worth referencing
How can i delete object from json file with PHP based on ID
How do you remove an array element in a foreach loop?
How to delete object from array inside foreach loop?
Unset not working in multiple foreach statements (PHP)
The last two from the list are closer to what I'm intending to do.
I've got a variable names $rooms which is storing data that comes from a particular API using Guzzle
$rooms = Http::post(...);
If I do
$rooms = json_decode($rooms);
this is what I get
If I do
$rooms = json_decode($rooms, true);
this is what I get
Now sometimes the group exists in the same level as objectId, visibleOn, ... and it can assume different values
So, what I intend to do is delete from $rooms when
group isn't set (so that specific value, for example, would have to be deleted)
group doesn't have the value bananas.
Inspired in the last two questions from the initial list
foreach($rooms as $k1 => $room_list) {
foreach($room_list as $k2 => $room){
if(isset($room['group'])){
if($room['group'] != "bananas"){
unset($rooms[$k1][$k2]);
}
} else {
unset($rooms[$k1][$k2]);
}
}
}
Note that $room['group'] needs to be changed to $room->group depending on if we're passing true in the json_decode() or not.
This is the ouput I get if I dd($rooms); after that previous block of code
Instead, I'd like to have the same result that I've shown previously in $rooms = json_decode($rooms);, except that instead of having the 100 records it'd give only the ones that match the two desired conditions.
If I am not totally wrong, then this should do the trick for you:
$rooms = json_decode($rooms);
$rooms->results = array_values(array_filter($rooms->results, function($room) {
return property_exists($room, 'group') && $room->group != "banana";
}));
Here is a verbose and commented version of this one above:
$rooms = json_decode($rooms);
// first lets filter our set of data
$filteredRooms = array_filter($rooms->results, function($room) {
// add your criteria for a valid room entry
return
property_exists($room, 'group') // the property group exists
&& $room->group == "banana"; // and its 'banana'
});
// If you want to keep the index of the entry just remove the next line
$filteredRooms = array_values($filteredRooms);
// overwrite the original results with the filtered set
$rooms->results = $filteredRooms;
hey there so basically i'm trying to make a cart and i want the id products to be in a array so that i can serialize them in a cookie for 15 days, the problem i that each time i press the button "add to cart" it changes the previous id instead of adding to the array, i've tried making a loop and changing the index each time and i've tried just adding with $cart[]=$itemId; and the array_push(); function, nothing seems to work.
if (isset($_POST['item'])) {
$item = $_POST['item'];
$panier = array();
$panier[] = $item;
print_r($panier);
}
Just to explain the code, this code will be used when the add to cart button is pressed and retain the product id from the post variable and then add to the array each time but instead i only get a single value in the array that keeps changing everytime i press the button to add.
That's because you're creating a new array each time and then just adding one value to it. Remove $panier = array() to avoid creating a new array each time.
Combining #IncredibleHat's suggestion into working example:
$item = 0;
while($item <= 5) {
if (!isset($panier)) { $panier = array(); }
$panier[] = $item;
$item++;
}
print_r($panier);
Notice that if you remove the if(!isset($panier), then $panier only ends up containing the last value, because it gets re-initialised to empty every iteration through the loop.
In addition to not using storage that persists between requests, your method prevents people buying more than one of an item:
<?php
session_start();
if (isset($_POST['item'])) {
$_SESSION['panier'][$_POST['item']]=
isset($_SESSION['panier'][$_POST['item']) ?
$_SESSION['panier'][$_POST['item']] + 1 :
1;
}
foreach($_POST['door_check'] as $door_check)
{
$_SESSION['front_door']['door'] = $door_check;
}
I have this little section of code that checks how many boxes were checked and then creates an array of the check box values.
The thing is, when I add that 'door' key, the array only adds one value no matter how many checkboxes were checked. When I just leave it empty, it adds all of them like [0], [1], [2] etc
Why is this?,
Your foreach() loops overwrites old variable each time. You need to make your session variable an array, for example
foreach($_POST['door_check'] as $door_check)
{
$_SESSION['front_door']['door'][] = $door_check;
}
edit: Don't forget to validate that data when you save it for later use.
Try something like this:
foreach($_POST['door_check'] as $door_check) {
$_SESSION['front_door']['door'][] = $door_check;
}
or maybe even:
$_SESSION['front_door']['door'] = $_POST['door_check'];
i have 3 files, maintransaction.php, computation.php, and computepayment.php
in maintransaction, it lists all the procedures and price which is in checkbox. once submitted, redirects to computation.php where it computes the checked procedures.
computation.php
foreach($ercharge as $charge) {
$p = explode(",", $charge);
$price[] = $p[0]; //stores the price of the $charge in $price array
$procedure[] = $p[1]; // stores the procedure of the $charge in $procedure array
I added that to my 2nd file to get the procedure and the price. its in foreach so that when the user selects MULTIPLE values, it will store it in an array. Cause thats the tricky part, when a user selects more than one in the checkbox.
i also added this to test that the mutliple selected procedures where passed
foreach ($procedure as $pro) {
$_SESSION['receipt'] = $pro;
echo $_SESSION['receipt'];
}
it works fine. all the procedures checked where printed out.
after clicking another button, redirects to computepayment.php, where you enter the amount to be paid.
i added this to the file:
print_r($_SESSION['receipt']);
it only shows ONE value. not ALL the values checked. i copied the foreach format from computation.php, as i wouldve guessed, it wouldnt work cause it doesnt recognize the variable $procedure.
How can i output the same values that were from the 2nd file, into the third file?
You need to set the session values in $_SESSION['receipt']. The way you did it, you just overrode at every iteration, the code below adds it to the array.
$_SESSION['receipt'] = array();
foreach ($procedure as $pro) {
$_SESSION['receipt'][] = $pro;
}
Ok, long story short: I am trying to place items in an array sorted by a value as part of an object(the object defined as $aProductOrdered and the public value:productMan). The post will be variable depending on items added and removed from a database so it has to be dynamic.
For example if the index is 12 for a certain productMan value all items with that would be in one row like:
[12][0]:prodObj [12][1]:prodObj
[15][0]:prodObj
[22][0]:prodObj [22][1]:prodObj
where the first is the prodMan value from the object and the second is an arbitrary auto assigned index to loop through representing each object.
Below is what I have but when I go to insert into the array, it accurately says the index I am trying to add to is undefined. How may I add the index if it doesn't exist or if it does just append to it?
$vendOrderArray = array(array());
//here we will loop through all non blank posted orders and create objects to place them in our $orderArray
foreach($_POST as $prodID=>$numOrderded)
{
if(is_numeric($numOrderded) && $numOrderded != "" && $numOrderded != "0")
{
$aProductOrdered = getProduct($prodId);
$aProductOrdered->numberOrdered = $numOrderded;
array_push($vendOrderArray[$aProductOrdered->productMan],$aProductOrdered);
}
}
if(!isset($vendOrderArray[$aProductOrdered->productMan]))
$vendOrderArray[$aProductOrdered->productMan] = array();
Just before the array_push call.