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.
Related
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.
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
How to get next array element from session array on next button click?
I tried next($_SESSION['qid']) it didn't work
if((int)$_SESSION['qn']<=20) {
$_SESSION['qn']=$_SESSION['qn']+1;
$_SESSION['qid']++;
}
I also tried
$_SESSION['qid']=next($_SESSION['qid']);
But this neither worked. Can someone help me?
_SESSION array is an associative array. You can't access it by numeric index, but you must specify the index name (e.g. in your code, $_SESSION['qid']). Anyway, you can still use next() function, passing the array $_SESSION (see here: http://php.net/manual/en/function.next.php). The correct way to use it is:
$element = next($_SESSION)
you'd likely want to put this code in a cycle.
Additionally, your code:
$_SESSION['qn']=$_SESSION['qn']+1;
means: assign to $_SESSION['qn'] the value of $_SESSION['qn'] plus 1, which is not what you want.
In case you'd want the next element in a NON associative array, you should use:
$arr = $arr[$i+1]
where $i is a integer value.
Update: regarding your comment, why don't you save a regular array (non associative) inside $_SESSION['questions']? This way you'd have the questions accessible this way:
$_SESSION['questions'][0], $_SESSION['questions'][1]...
Now you can use it within a cycle, or whatever you want. E.g.:
echo $_SESSION['questions'][$current_question_id+1];
where $current_question_id would be the current question index, which will be updated (+1) every clic on the next button
I am paging through the elements of an array.
I get the total number of elements in the array with:
$total = count($myarray);
My paging function loads the current element on the page and provides "Previous" and "Next" links that have urls like:
http://myapp.com?page=34
If you click the link I grab that and load it onto the page by getting (I sanitize the $_GET, this is just for example):
$element = $myarray[$_GET['page']];
This should grab the element of the array with a key == $_GET['page'] and it does. However, the problem is that my total count of elements doesn't match some keys because while there are 100 elements in the array, certain numbers are missing so the 100th item actually has a key of 102.
How should I be doing this? Do I need to rewrite the keys to match the available number of elements? Some other method? Thanks for your input.
If you have gaps in the indices, you should reindex the array. You can do that before you generate the links, or probably easier on the receiving page:
$myarray = array_values($myarray);
$element = $myarray[$_GET['page']];
This would give you the 100th element, even if it previously had the key 102. (You could use a temporary array of course, if you need to retain the original indexing.)
you can use
$array = array_values($array);
How should I be doing this? Do I need
to rewrite the keys to match the
available number of elements? Some
other method?
No you don't need to worry about them not matching. Php arrays are associative containers, like dictionaries in other languages. If you define something at 98 and 100, 99 isn't sitting there in memory, the data structure behind the associative container only stores whats there. You're not wasting space by not "filling it up" up to count.
So the practice you describe is fine. If there is no page "99" nothing need show up in your array. It may be nice, however, to see that your array doesn't have anything for the 'page' parameter and display an error message.
But then why, when I access $total =
count($myarray); $myarray[$total]
where $total = 100 I do not get the
last element? I can put page=101 and
get one more record. I should not be
able to do this
Because count is counting how many things are in the array. If you have an array with only the even elements filled in, ie:
$myArray[0] = "This";
$myArray[2] = "is";
$myArray[4] = "even";
Here count($myArray) is 3. There's nothing in [1] or [3]. Maybe this is easier to see when you take numbers out of the equation. Arrays can have string indexes
$myArray = array();
$myArray["Hello"] = "A Bunch of";
$myArray["World"] = "words";
Here count($myArray) is 2.
In the first case, it wouldn't make sense to access $myArray[3] because nothing is there. Clearly in the second example, there's nothing at 2.