Welcome Guest, Not a member yet? Register   Sign In
Array helper: dot search
#1

(This post was last modified: 04-25-2022, 02:56 PM by iRedds.)

The dot_array_search() function has strange behavior.

I am expecting a value that matches the key, or null if the key is not found. But

PHP Code:
dot_array_search('a.b.c', ['a' => ['b' => ['c' => 'c']) // return "c". Ok
dot_array_search('a.b.c', ['a' => ['b' => ['c']) // return "null". Ok. because the key "c" does not exist.

dot_array_search('a.b.c', ['a' => ['b' => 'b']) // return "b". why? key "c" does not exist and should return "null"

//but if we replace "b" with wildcard "*"
dot_array_search('a.*.c', ['a' => ['b' => 'b']) // return "null". Ok 
dot_array_search('a.*.c', ['a' => ['b' => []]) // return "null". Ok 

//but 
dot_array_search('a.*.c', ['a' => []) // return "empty array" ?? 
Reply
#2

I haven't really looked at it, but is the function expecting the user to at least provide an array with the correct number of levels? You'll have to wait for a CI4 to elaborate.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

Find exact you must use function array_deep_search()

Code:
array_deep_search('a.b.c', ['a' => ['b' => 'b']]);  //return b
array_deep_search('a.*.c', ['a' => []]); //return NULL
Reply
#4

(This post was last modified: 04-30-2022, 12:55 AM by kenjis.)

I don't know why.

I created an issue.
https://github.com/codeigniter4/CodeIgni...ssues/5939

I concluded this is a bug, and sent a PR:
https://github.com/codeigniter4/CodeIgniter4/pull/5940
Reply




Theme © iAndrew 2016 - Forum software by © MyBB