0

I have my array looking like this when I print_r(delegates[$i])

Array
(
    ['firstName'] => Brady
    ['surname'] => Manning
    ['jobTitle'] => CEO
    ['memberNumber'] => 123456
    ['dieteryRequirements'] => halal
)

Howerver, when I try to get the firstName like this echo delegates[$i]['firstName'] I get the following error

Notice: Undefined index: firstName 

Schoolboy question but I'd really appreciate some help here.

3
  • 1
    Try: echo delegates[$i]["'firstName'"] Commented Feb 15, 2016 at 14:16
  • Try: echo delegates['firstName'] :/ Commented Feb 15, 2016 at 14:19
  • 1
    Where does $i come from? is this inside an iteration? Provide that information as well as it will help giving you a better answer. Commented Feb 15, 2016 at 14:29

1 Answer 1

8

After examining the error you are having, this is my best answer. I initially assumed you didnt need the index $i, but that was not the case since you are actually getting results when you print_r($delegates[$i]) Therefore I am lead to believe your array is a multidimensional array.

Another thing I noticed, (and I give credit to @Rizier123 who pointed out in the comments to use both single and double quotes) is that your print_r result is outputting single quotes '' around your element keys like this 'firstname' This means that you are actually storing the quotes inside your array. With all that said I believe your $delegates array looks something like this:

$delegates = array(
    array(
        "'firstName'" => 'Brady',
        "'surname'" => 'Manning',
       "'jobTitle'" => 'CEO',
        "'memberNumber'" => 123456,
        "'dieteryRequirements'" => 'halal',
    )
);

Therefore in order to access the element you will need to use the index, and use the element with the single quotes '' like this:

echo $delegates[0]["'firstname'"]

What I would do is remove all those single quotes so that you can access them correctly.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks my friend, I didn't for one second pay attention to the single-quotes. Long day
I missed that initially as well. Glad I was able to 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.