DEV Community

Cover image for data_get(): Warning with array keys with dots - Laravel Tips
Tony Joe Dev
Tony Joe Dev

Posted on • Originally published at tonyjoe.dev

data_get(): Warning with array keys with dots - Laravel Tips

Ingredients

data_get()

data_get() is a useful helper function in Laravel that retrieves values from a nested array or an object using dot notation.
The strength of data_get() is also that it accepts wildcards.

If you don't know or you don't use it, I suggest you to discover it (here the docs), with brothers and sisters: data_set(), data_fill(), data_forget()! ;)

Arr::get()

Unlike data_get(), Arr::get() works only on arrays and it doesn't accept wildcards.


The Tip

Let’s show this example:



$array = [
    'key.sub' => 'a-value'
];

data_get($array, 'key.sub');  // --> null ❗

// instead...
\Arr::get($array, 'key.sub'); // --> "a-value"


Enter fullscreen mode Exit fullscreen mode

Instead of this:



$array = [
    'key' => [
        'sub' => 'a-value'
    ]
];

data_get($array, 'key.sub');  // --> "a-value"

// and also...
\Arr::get($array, 'key.sub'); // --> "a-value"


Enter fullscreen mode Exit fullscreen mode

And you?

  • Did you use already data_get()?
  • And Arr::get()?
  • Did you know that difference?

Leave your comment!


✸ Enjoy your coding!

 

If you liked this post, don't forget to add your Follow to my profile!

If you want to preview my content, Subscrive to my Newsletter!

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay