0

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!

believe me
  • 910
  • 1
  • 5
  • 24
jphillip724
  • 269
  • 2
  • 5
  • 16
  • actually the notice you are seeing is not to worry because the compiler still runs through your program. if you are trying to access a key that is not present like this `$value=$array['notExistingIndex']; then you just will have null in your `$value`. – ITroubs Mar 20 '13 at 14:01
  • @ITroubs *Any* notice or warning is to worry! Never leave notices unfixed! – deceze Mar 20 '13 at 14:03
  • thanks for the reply iTroubles yes it still runs the rest of the script but i would much rather have 0 errors it's more professional and just good practise – jphillip724 Mar 20 '13 at 14:05
  • It is not an error it is just a notice. And since you want it to run without the notice you have to check whether your key is present and if not just set it with a value that you want. – ITroubs Mar 20 '13 at 14:07
  • @ITroubs [Why should I fix E_NOTICE errors?](http://stackoverflow.com/a/5073670/476) – deceze Mar 20 '13 at 14:11
  • @deceze you have to discriminate between compiletime notices and runtime notices. Compiletimenotices will be shown in a good IDE. I agree that they really should be corrected. But some runtimenotices are wanted. Especially when working with arrays. You would produce a massive amount of overhead if you checked for the existance of a value and then just set an empty value instead if it wasn't set yet. – ITroubs Mar 20 '13 at 14:24
  • @ITroubs It makes no difference. Notices are *potential problems*. If you do not know whether a variable/index exists, treat it as such and use `isset`/`empty`/`array_key_exists`. If you don't, how are you going to distinguish between variables that "work just fine undefined" and cases where you are actually trying to work with a non-existent value ***in error***? Programs that spew notices about potential problems as part of their normal operation are not good. – deceze Mar 20 '13 at 14:28
  • 1
    i quite enjoyed reading your comments, and the link that deceze posted, i have fixed the error however my two cents on the matter is all errors, notifications what ever should be fixed as if they were serious errors as eventually they can come back and haunt you down the line, since this is course work for university i ideally want it to be error free to helpfully boost my grade and so all thought this notice isnt a real serious one, it's in my best interests to fix it :) – jphillip724 Mar 20 '13 at 14:53

1 Answers1

1

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];
    }
ulentini
  • 2,413
  • 1
  • 14
  • 26
  • thanks for the reply, yeh that is true, i specified that cityname is required whereas pop is optional if cityname is not there, they will see something else, but the script can continue without the pop, and im trying to fix this notice by using the method i attempted to describe above but have no idea how to actually go about doing it. – jphillip724 Mar 20 '13 at 14:31
  • thanks man that is exactly what i was needing dam it's the same as what i had but i didnt have the [] after myarray, thanks for the help – jphillip724 Mar 20 '13 at 14:38