I am new to this so please do not judge.
I have transformed a .csv file into an array([0]=>array([0]=>string(), [1]=>string())[1]=>array([0]=>string(), [1]=>string()) etc. So I can access it numerically i.e. $the_big_array[1][1]
Now I want the program to write a couple of lines of code for each array within an array.
Basically what I am doing is creating a table like this that will be encoded in json.
$request['AddPrice'][0][variable1] = $the_big_array[1][2]
$request['AddPrice'][0][variable2] = $the_big_array[1][3]
and I want different values loaded for and from each line of .csv file/$the_big_array
$request['AddPrice'][1][variable1] = $the_big_array[1][2]
$request['AddPrice'][1][variable2] = $the_big_array[1][3]
I am stuck at foreach function as I cannot grasp how to make it execute certain action for each array within an array.
You have to use a foreach loop for every array dimension.
If You have 2 dimensions, like in $the_big_array[1][1], go through the first dimension with your first loop. Inside this loop, do another foreach to go through your second dimension.
so I have fixed this problem. Perhaps the question was not clear enough.
What I did was to create arrays with maximum occupancy for each of my rooms and add nested foreachloop for each occupancy, so it has to repeat until maximum occupancy is reached and only then it can iterate to the next key in the first foreachloop.
Related
I'm newby in PHP but I allready have a difficult question.
I have an array which is like this:
$test = array("00000","00001","00011","00111","00101","00110","00110", etc (so it is unordered...)
now:
I'd like to check the first position with the second. if the Levensthein is <=1 then that value should stay in place and if it is not it should either.
move to the last position in the array...
or move to another array (i don't know what is best)
after that, the next 2 values should be checked with eachother...
so how can you keep looping through an array?
The easiest way to keep looping over an array until it is empty is to have two loops: one that loops until the array is empty and inside it one that loops over each element remaining in the array.
Just make sure that your code ensures that the array will go empty eventually or it'll be trapped in an infinite loop.
while(count($array)) {
foreach( $array as $element ) {
// your code goes here
}
}
When working with existing code, it takes one array and places it into another in the fashion shown below.
I believe the empty brackets are the same thing as simply pushing it and appending it to the first available index.
$g['DATA'][] = $p;
After this is done, I have my own array that I would like to append to this as well. I tried using array_merge() with $g['DATA'][]as a parameter, but this is invalid for obvious reasons.
My only thought is to create a foreach loop counter so I can figure out the actual index it created, however I have to assume there is some cleaner way to do this?
Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this:
$g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray);
//^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
// -1 Because an array is based index 0 -> means count() - 1 = last key
Just thought I would like to share this should someone find a use for it.
Basically I needed a list of HTML colors to loop / cycle through. So I need to remove the first element of the array, place it at the end and then get the current HTML color.
Given the following array:
$colors = array(
"#2265fa", "#b61d1e", "#53b822", "#a9d81c", "#d6e9f5", "#43cc7d", "#e3159a",
"#80c85e", "#17b303", "#989240", "#014c07", "#d265f3", "#22bbb9", "#6c69a9",
"#7ea13a", "#0dcea2", "#99c27d", "#41405b", "#731801"
);
So this is what I came up with. Sure there will be hundreds of ways to do this. This is my take on it.
# Array_shift returns the value it takes off the beginning of the array.
# And I merely append this to the end of the array
$colors[] = array_shift($colors);
# Using current I am able to get the current first element of the array back
echo current($colors);
In this case it would be "#b61d1e" that is the current index for the array. May you find this useful somewhere.
I have an array with 18 values in it from which I select random values using $array[rand(0,17)]. I put these randomly selected values next to each other on the page. Within the array are 6 sets of values that I do not want to be put next to each other on the page. Is there any way that I can detect when the pairs are together and select new values because of that
warning: Do you know for sure that you won't get any degenerate cases where there are no possible orderings of the array? For example, if you won't allow the pairs [1,2] or [2,1] and the array you get is [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2], the you're out of luck. There's no way to display the array in the way you want, and a method like I describe below will never terminate.
I would use shuffle($array) and then iterate through the shuffled array one item at a time, to find out whether any value is "incompatible" with the item before it. If so, just reshuffle the array and try again. You can't predict how many tries it will take to get a shuffled array that works, but the amount of time it takes should be negligible.
To detect whether two values are compatible, I'd suggest making an array that contains all incompatible pairs. For example, if you don't want to have the consecutive pairs 1 and 3 or 2 and 5, then your array would be:
$incompatible = array(
array(1,3),
array(2,5) );
Then you'd iterate over your shuffled array with something like:
for ($i=1; i<count($array)-1; i++;) {
$pair = $array[i, i+1]; // this is why the for loop only goes to the next-to-last item
if in_array($pair, $incompatible) {
// you had an incompatible pair in your shuffled array.
// break out of the for loop, re-sort your array, and try again.
}
}
// if you get here, there were no incompatible pairs
// so go ahead and print the shuffled array!
Or use with unset() for remove the keys, or use by Session, for next skip.
I'm trying to look through an array of records (staff members), in this loop, I call a function which returns another array of records (appointments for each staff member).
foreach($staffmembers as $staffmember)
{
$staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
// print_r($staffmember['appointments'] works fine
}
This is working OK, however, later on in the script, I need to loop through the records again, this time making use of the appointment arrays, however they are unavailable.
foreach ($staffmembers as $staffmember)
{
//do some other stuff
//print_r($staffmember['appointments'] no longer does anything
}
Normally, I would perform the function from the first loop, within the second, however this loop is already nested within two others, which would cause the same sql query to be run 168 times.
Can anyone suggest a workaround?
Any advice would be greatly appreciated.
Thanks
foreach iterates over a copy of the array. If you want to change the value, you need to reference it:
foreach($staffmembers as &$staffmember) // <-- note the &
{
$staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
// print_r($staffmember['appointments'] works fine
}
From the documentation:
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
and
As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.