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!

8
  • 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`. Commented Mar 20, 2013 at 14:01
  • @ITroubs Any notice or warning is to worry! Never leave notices unfixed! Commented Mar 20, 2013 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 Commented Mar 20, 2013 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. Commented Mar 20, 2013 at 14:07
  • 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 :) Commented Mar 20, 2013 at 14:53

1 Answer 1

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];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.