1

I have this kind of Array

    Array
(
    [year] => Array
        (
            [0] => 2019
        )

    [user] => [email protected]
)

The only thing I want is to get the usermail from this array.

it seems simple, but still doesn't work for me. I tried to get it like this:

   foreach ($filter as $item) { echo $item['user'];}

but this goves mi only the first character of the email, so the result is 'u'.

1
  • 8
    Just use echo $filter['user']; without the loop. Commented Jun 26, 2019 at 11:16

2 Answers 2

4

No need to iterate, do like below:-

echo $filter['user'];

Sample output:-https://3v4l.org/Gsmhr

Note:

In-case you have multidimensional array then you have to use foreach(): https://3v4l.org/9TBeG

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

2 Comments

ahh, seems I had a hard day :) Trying to complicate things. Thank you, works as expected.
@wotan glad to help you :):)
2

As array contains only one element. So, there is no need to iterate the loop. You may get the value of user directly like :

echo $filter['user'];

Suppose, you have multiple value within the same array, you may have to iterate the loop if your array is look like below:

array(
    [0] => array(
            [year] => Array
                (
                    [0] => 2019
                )

            [user] => [email protected]
        ),
    [1] => array(
            [year] => Array
                (
                    [0] => 2019
                )

            [user] => [email protected]
        )
)

Comments

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.