As the question says i need to validate a multidimensional array, just so you know this is my first time actually using arrays so it might be a pretty bad script but it works and that's all I'm after at the moment. Okay so it's working I have two sessions displaying in this array, when i remove one of the sessions I get this error
"Notice: Undefined index: pop in C:\inetpub\wwwroot\dropdown\test.php on line 30"
I think i know how to fix it but I don't actually know how to implement it. this is me talking through what im after
$myarray (
IF isset session cityname
add the value to my array
ELSE
add a blank value in its place (or just remove it from the array altogether)
IF isset session pop
add the value to my array
ELSE
add a blank value in its place (or just remove it from the array altogether)
echo myarray
please note cityname is mandatory whereas pop is not
That's essentially what I'm trying to achieve but i haven't the slightest how to actually go about doing it, here is my current code
if(isset($_SESSION['cityname'])){
$myarray = array(array($_SESSION['cityname']),
array($_SESSION['pop'])
);
foreach($myarray as $key=>$value){
echo $myarray[$key][0];
}
Any help MUCH appreciated I've lost to much hair to this problem the last couple of weeks!
That notice is telling you that you're using $_SESSION['pop'] that has never been set.
In fact in your code you just checked for $_SESSION['cityname'] but then you add $_SESSION['pop'] to your array.
EDIT
If you want $_SESSION['pop'] to be optional and you want to get rid of that notice, just check if $_SESSION['pop'] is set or not:
if(isset($_SESSION['cityname'])){
$myarray = array(array($_SESSION['cityname']));
if(isset($_SESSION['pop'])) { $myarray[] = array($_SESSION['pop']); }
foreach($myarray as $key=>$value){
echo $myarray[$key][0];
}
Related
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
So as the question states, im trying to create an array using a for loop, this seems as though its a simple question, but i cant find the asnwer on SO or googling. Heres what im doing:
$twelve=array("user","day");
for($i=0; $i<$value; $i++)
{
$total=$anarray[$i][value]; //get a value
$twelve[$i]=($i,$total); //insert values into array
}
this doesn't work, how should i go about getting this to work?
You may end up in a never-ending loop if $total=$anarray[$i][value]; is an increasing value. Regardless of the loop, you'll want to do as the other answerer mentioned, namely:
$twelve[$i] = array($i, $total);
I think it should be $twelve[$i] = array($i, $total);
Also, on this line;
$total=$anarray[$i][value]; //get a value
Unless value is defined as a constant, I think you want to do $anarray[$i][$value];.
PHP might not recognize value as a set variable or a constant, therefore crashes and never sets $twelve to any value.
I have found a solution that does work
in each loop simply do:
$twelve[$i]["user"]=$i;
$twelve[$i]["day"]=$total;
It would be nice if there is a way to do that in one line, but that is working.
In my form i have fields with name photoid[] so that when sent they will automatically be in an array when php accesses them.
The script has been working fine for quite some time until a couple days ago. And as far as i can remember i havent changed any php settings in the ini file and havent changed the script at all.
when i try to retrieve the array using $_POST['photoid'] it returns a string with the contents 'ARRAY', but if i access it using $_REQUEST['photoid'] it returns it correctly as an array. Is there some php setting that would make this occur? As i said i dont remember changing any php settings lately to cause this but i might be mistaken, or is there something else i am missing.
I had the same problem. When I should recieve array via $_POST, but var_dump sad: 'string(5) "Array"'. I found this happens, when you try use trim() on that array!
Double check your code, be sure you're not doing anything else with $_POST!
Raise your error_reporting level to find any potential source. It's most likely that you are just using it wrong in your code. But it's also possible that your $_POST array was mangled, but $_REQUEST left untouched.
// for example an escaping feature like this might bork it
$_POST = array_map("htmlentities", $_POST);
// your case looks like "strtoupper" even
To determine if your $_POST array really just contains a string where you expected an array, execute following at the beginning of your script:
var_dump($_POST);
And following for a comparison:
var_dump(array_diff($_REQUEST, $_POST));
Then verifiy that you are really using foreach on both arrays:
foreach ($_POST["photoid"] as $id) { print $id; }
If you use an array in a string context then you'll get "Array". Use it in an array context instead.
$arr = Array('foo', 42);
echo $arr."\n";
echo $arr[0]."\n";
Array
foo
$_POST['photoid'] is still an array. Just assign it to a variable, and then treat it like an array. ie: $array = $_POST['photoid'];
echo $array[0];
I'm new to programming and I'm tackling arrays. I thought I had multi-dimensional arrays figured out but I guess I don't. Here's the code I'm working on:
$loopcounter = 0;
while ($myrow = mysql_fetch_array($results)) {
//...other stuff happens...
$allminmax[$loopcounter][] = array("$myrow[3]","$currentcoltype","$tempmin","$tempmax");
$loopcounter++;
}
This chunk of code is supposed to create an array of four values ($myrow[3], currentcoltype, tempmin, tempmax) and insert it into another array every time the loop goes through. When I do this:
echo implode($allminmax);
I get:
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Do I need to implode each array before it goes into the master array? I really want to be able to do something like $allminmax[0][4] and get the $tempmax for the first row. When I try this now nothing happens. Thanks -- any help is appreciated!
It looks like you should either use [$loopcounter] or [], but not both. You also should drop the quotes. They're unnecessary and in the case of "$myrow[3]" they interfere with the variable interpolation.
$allminmax[] = array($myrow[3], $currentcoltype, $tempmin, $tempmax);
By the way, arrays are zero-indexed, so to get $tempmax for the first row it'd be $allminmax[0][3] not $allminmax[0][4].
Also, a better way to display the contents of your array is with print_r or var_dump. These will display arrays within arrays while a simple echo will not.
var_dump($allminmax);
My php reads in xml and I want to ouput the values within a given range. I have no way of knowing the size of the array or the range. However, I do know where to start; I have a $key that holds my current location. I also know where to stop; I have the word "ENDEVENTS" between each set. I want to get the values from my current position ($key) to my end position ("ENDEVENTS").
for example i may have an array set like this:
Array(
[0]=1
[1]=apple
[2]=straw
[3]=bike
[4]=ENDEVENTS
[5]=15
[6]=hair
[7]=shirt
[8]=nose
[9]=kiwi
[10]=ENDEVENTS
)
My code knows when I'm on 15 (in this example $key=5). I want to print 6 through 9. I tried using foreach but it's thats causing issues with what I'm trying to produce.
Any ideas?
Not so sure if i understood all ok but, ill give a try :-D
while(array[$key]!="ENDEVENTS"){
echo array[$key];
$key++;
}
Not entirely sure if I understand your question, but maybe this is helpful:
$stuff_to_print = array_slice($my_array,$key,array_search('ENDEVENTS',$my_array));
This should return an array with key values from the $key to the next 'ENDEVENTS' value