1

This is the sample array

Array
(
    [note] => Array
        (
            [to] => Array
                (
                    [user] => Array
                        (
                            [name] => First User
                        )

                    [abc] => 123
                    [lmn] => 4582
                )

            [from] => Jani
            [heading] => Reminder
            [body] => Array
                (
                    [abc] => 123
                )

        )

)

I have a string like "note > to > abc" and want value of "abc" under "to" array. So how can I get the value?

6
  • Use a foreach loop.. Commented Oct 27, 2021 at 11:00
  • 2
    Just that value can be $arr['note']['to']['abc'] Commented Oct 27, 2021 at 11:01
  • 1
    If you're using laravel, there is a helper method that can be used, named data_get(). data_get($arr, 'note.to.abc) Commented Oct 27, 2021 at 11:09
  • @BABAKASHRAFI I think that should be the answer rather than just a comment Commented Oct 27, 2021 at 11:15
  • @RiggsFolly yes you're right, but the title of the question was about php in general. I posted it as answer and edited the title to specifically point to laravel. Commented Oct 27, 2021 at 11:25

1 Answer 1

5

In laravel, there is a helper method named data_get() for getting a specific key without throwing error if it doesn't exists.

It is documented here

You can use it like below:

data_get($arr, 'note.to.abc)
Sign up to request clarification or add additional context in comments.

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.