Restore array items after every iteration in PHP - php

I am trying to restore all array items after each for loop iteration. This I need to set Laravel validation Rule::notIn($arry)
I have an array of station_id generated by cloned input fields. I want to check if all cloned station ids are unique, and so be sure that no repeated station in the metro route.
For cloned fields, I am setting up a rule using for a loop by counting cloned items.
So the issue is, I want to use `Rule::notIn($stationIds) except the current iteration item id so I can validate by checking the current id is not in the rest of the array items.
public function rules()
{
// getting all input fields value
$rStationIds = $this->get('station_id')
...
// get the max number of input
$counter = $this->getMaxCount($rStationIds, ...);
$rules = [];
// loop through each item
for ($r = 0; $r < $counter; $r++) {
unset($rStationIds[$r]);
$rules['station_id'][$r] = ['required', 'int', Rule::notIn($rStationIds)];
}
...
}
The problem in the above code is that when I unset($var) the current item, it never reset back with the original array elements; thus, the last field will have nothing to compare because the array will get empty.
I am okay with any other approach as well to check the unique item for the cloned station id fields.

Change your loop to:
// loop through each item
for ($r = 0; $r < $counter; $r++) {
$temp = $rStationIds[$r];
unset($rStationIds[$r]);
$rules['station_id'][$r] = ['required', 'int', Rule::notIn($rStationIds)];
$rStationIds[$r] = $temp;
}

Related

PHP check for values before the loop to check

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

Resetting a submitted form in Symfony 2

I'm mapping some data coming from the remote source into my entites. I'm using forms for this purpose. Here is a fragment of code that is responsible for mapping data onto entities:
$i = 0;
$x = 0;
foreach ($bestellerLists[0] as $bestsellerList) {
$List[$i] = new BestsellerList();
foreach ($bestsellerList['books'] as $book) {
$Book = new Book();
$bookForm = $this->bookForm;
$bookForm->setData($Book);
$bookForm->submit($book);
$List[$i]->addBook($Book);
unset($bookForm);
unset($Book);
$x = $x + 1;
}
$i = $i+ 1;
}
The problem that I cannot solve is that in the first iteration if the inner foreach loop, everything is well, entities get mapped, the first $Book is added to the current $List.
However, the second interation of the inner loop errors out with:
You cannot change the data of a submitted form.
As you can see, the $bookForm is assigned the value with $bookForm = $this->bookForm. At the end of the current iteration I unset $bookForm in order to create a new one at the next iteration.
I really do not get why Symfony would complain about changing the data of a submitted form. A new empty form should be used at each iteration. Any idea what I might be missing here?

Push item into an array and replace php

i need to make an array like this
$privateMsgIdArray = array("idlistener" => $idlistener, "maxMsgId" => $lastMsgId);
I need to replace the maxMsgId to the corresponding idlistener, and if the idlistener that i pass doesn't not exist to create a new entry inside the array.
I am a but confused on how i am going to extract the maxMsgId value corresponding to an idlistener.
In other words i need to pass new values of idlisteners only once, and replace maxMsgId each time that they are not equal to the corresponing idlistener.
If the idlistener field doesn't exist create it (push into array).
I pass old array into a session and new array in the current run.
After the run i i replace them.
I believe this sounds a bit confusing though.
e.g
We have an array like this already:
[15][200]
next call maxMsgId is 210
array should be
[15][210]
next call we have a new listener id with maxMsgId 30
array should be
[15][210]
[16][30]
You should be able to accomplish this with a quick loop:
// your "new" values
$idListener = 15;
$maxMsgId = 210;
// loop over the array to see if it contains the `idlistener` you want
$end = count($privateMsgIdArray);
for ($i = 0; $i < $end; $i++) {
if ($privateMsgIdArray[$i]['idlistener'] == $idListener) {
// we found it! overwrite the `maxMsgId` field
$privateMsgIdArray[$i]['maxMsgId'] = $maxMsgId;
break;
}
}
if ($i == $end) {
// we reached the end of the array without finding the `$idListener`;
// add a new entry =]
$privateMsgIdArray[] = array(
'idlistener' => $idListener,
'maxMsgId' => $maxMsgId
);
}
This is a rather brute-force approach though and, if efficiency is something you're after, it would be wise to create a "cache"-style method of idlistener values and their index in the $privateMsgIdArray array.
For instance:
// key = idlistener, value = index in `$privateMsgIdArray`
$idCache = array(15 => 0, 16 => 1);
// check if the `$idListener` is in the cache
if (!isset($idCache[$idListener])) {
// it's not; add a new entry
$key = count($privateMsgIdArray);
$privateMsgIdArray[$key] = array(
'idlistener' => $idListener,
'maxMsgId' => $maxMsgId
);
// add the new index into the cache
$idCache[$idListener] = $key;
} else {
// it is in the cache; pull the corresponding index and set the `maxMsgId` =]
$privateMsgIdArray[$idCache[$idListener]]['maxMsgId'] = $maxMsgId;
}
Both of the approaches above could be converted into functions to make things "more portable" too.

Post HTML elements array to PHP

Here is the snapshoot of my form. The input values under LPO (1st column) are hidden, I just showed them here to show complete form.
if color_id (first left most inputbox) is 37 its DB value is BB552 as displayed. And if its 3, the value of that color is BB110, but when its 0, it means a user has selected Custom and written a value by him self in the third row it is FFBBBAA.
I need to submit the form to PHP
The field names are as follows
color_id[] <-- Hidden input
order_id[] <-- Hidden input
subcolor_id[] <-- Select
subcolor_string[] <-- Input
material_id[] <-- Select
weight[] <-- input
When I post the form, in PHP
<?PHP
$count = count($_POST['weight']);
for($i = 0; $i < $count; $i++){
$color_id = $_POST['color_id'][$i];
$order_id = $_POST['order_id'][$i];
$material_id = $_POST['material_id'][$i];
$weight = $_POST['weight'][$i];
// i can count one post item and can iterate over all.
if($color_id == 0){
$color_id = $_POST['subcolor_id'][$i]; // this give me wrong result;
}
}
So when 0 is there, admin approving this form, can leave it to custom or change the color back to some value from DB
I want to know what are the possibilities for getting the proper sub color values if first hidden input color_id has 0.
So far the one which I thought of is to add two more hidden fields in Sub Color columns to match their index, but this will require me to completely re-write the del sub color code
Is there any other way which can save me from doing lots of alterations to this form?
as eds mentioned it totally makes sense.. u shud do this way
$j=0;
$count = count($_POST['weight']);
for($i = 0; $i < $count; $i++){
$color_id = $_POST['color_id'][$i];
$order_id = $_POST['order_id'][$i];
$material_id = $_POST['material_id'][$i];
$weight = $_POST['weight'][$i];
// i can count one post item and can iterate over all.
if($color_id == 0){
$color_id = $_POST['subcolor_id'][$j]; // this give me wrong result;
$j++;
}
}
this shud solve your problem..
Your other option is to declare another iterator $j outside of the loop, starting at 0, and manually incrementing that iterator after you read a subcolor each time. Then it should always be the index of the next subcolor_id you need to read.

Create PHP array's on the fly

I am having the worst time trying to get this to work. In the following code, I am gathering data from a database query and trying to build a muti-dimensional array object that will keep totals and tally up some information in a specific way. The problem is that instead of getting a value that is incrementing as it should, the value seems to be suffering from the last value it was assigned problem. Here is the code:
$REVIEWS = array();
$USER_REVIEWS = array();
$USER_IMGREVS = array();
pseudo-code: loop here which iterates over the DB results creating
$date - which is into this function as its called for each day of month
$p1user - which is one of the users (there are 3) 'levels' of users
$hr - is the hour which is built from the transaction's timestamp
$hr = date('H', $row['P1TIMESTAMP']);
$p1user = $row['P1USER'];
$REVIEWS[$date] += 1;
$USER_REVIEWS[$date][$p1user][$hr] += 1;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
print "PASS1<br/>\n";
print "Value of Total Reviews: [".$REVIEWS[$date]."]<br/>\n";
print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n";
print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n";
print "<br/><br/>\n";
So - the 'total reviews' increments by one, as it should, for each time i print this. SO far so good. The next two arrays will only print the last values they were assigned, and will not be added together like they should. Why not? I have attempted to do this another way by literally creating the arrays one by one and assigning them in whole to the array containing them - but that also does not seem to work. Any insights?
i don't know how you initilize your array, maybe this will help:
// replace this 2 lines:
$USER_REVIEWS[$date][$p1user][$hr] += 1;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
// with this code:
if (!isset($USER_REVIEWS[$date]))
$USER_REVIEWS[$date] = array();
if (!isset($USER_REVIEWS[$date][$p1user]))
$USER_REVIEWS[$date][$p1user] = array();
if (!isset($USER_REVIEWS[$date][$p1user][$hr]))
$USER_REVIEWS[$date][$p1user][$hr] = 0;
$USER_REVIEWS[$date][$p1user][$hr] += 1;
if (!isset($USER_IMGREVS[$date]))
$USER_IMGREVS[$date] = array();
if (!isset($USER_IMGREVS[$date][$p1user]))
$USER_IMGREVS[$date][$p1user] = array();
if (!isset($USER_IMGREVS[$date][$p1user][$hr]))
$USER_IMGREVS[$date][$p1user][$hr] = 0;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
Sir, I dont understand very well why your coed is not working, but in my first test, I would change these lines:
$count = 1;
$USER_REVIEWS[$count][$p1user][$hr] += 1;
$USER_IMGREVS[$count][$p1user][$hr] += $row['F5'];
$count++;
Please, check if this code helps you anyway.
Your print statements for those values rely on the value of $p1user:
print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n";
print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n";
If you want to print it for all users you should loop over all possible users rather than just using $p1user. Either that or add them up if you want their sum.
Edit: Something that was bugging me was your data structure. It doesn't seem to represent your data very well. In your loop why don't you build up useful information that you store at the base of the review array?

Categories