# Funny array function

- Thread: https://externals.io/message/63247
- Original archive: https://news-web.php.net/php.internals/63247
- Started by kuldeep dhaka on 2012-09-30T11:50:13+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #63247 — kuldeep dhaka — 2012-09-30T11:50:13+00:00

URL: https://externals.io/message/63247#63247

hello guys,
this is my first time, if thing mail is at wrong place please forward it
the right place.
while coding , i just found a requirement, its little bit funny. if it
already exists, please be calm and mail me the function name and its usage.

for accessing arrays we use
<?php
echo $array["school"]["class"]["roll_number"]["name"]; // this will print
the name of the student
/*
 i was thinking if is their any function that can do the above work like
*/
echo array_drill($array, array("school","class","roll_number","name"));

// like wise while assigning values
array_drill(<array to perform on>, <keys>, <new value>);
?>
i hope you guys would have understood what i mean.
kuldeep singh dhaka
kuldeepdhaka9@gmail.com

---

## #63248 — Charlie Somerville — 2012-09-30T11:56:11+00:00

URL: https://externals.io/message/63247#63248 · In reply to: #63247

I'm struggling to imagine how such a function would be useful considering you can already use [] to index arrays. 

If you have a variable 'depth' you need to drill down to, then writing a function to do that is also fairly trivial. 




> hello guys,
> this is my first time, if thing mail is at wrong place please forward it
> the right place.
> while coding , i just found a requirement, its little bit funny. if it
> already exists, please be calm and mail me the function name and its usage.
> 
> for accessing arrays we use
> <?php
> echo $array["school"]["class"]["roll_number"]["name"]; // this will print
> the name of the student
> /*
> i was thinking if is their any function that can do the above work like
> */
> echo array_drill($array, array("school","class","roll_number","name"));
> 
> // like wise while assigning values
> array_drill(<array to perform on>, <keys>, <new value>);
> ?>
> i hope you guys would have understood what i mean.
> kuldeep singh dhaka
> kuldeepdhaka9@gmail.com (mailto:kuldeepdhaka9@gmail.com)

---

## #63249 — Anthony Ferrara — 2012-09-30T12:03:16+00:00

URL: https://externals.io/message/63247#63249 · In reply to: #63248

Yes, it's fairly trivial to write a function for this. I wrote a pair of
them in about 2 minutes:

http://codepad.viper-7.com/rqVlqL



On Sun, Sep 30, 2012 at 7:56 AM, Charlie Somerville <
charlie@charliesomerville.com> wrote:

> I'm struggling to imagine how such a function would be useful considering
> you can already use [] to index arrays.
>
> If you have a variable 'depth' you need to drill down to, then writing a
> function to do that is also fairly trivial.
>
>
> 
>
> > […]
> usage.
> > […]

---

## #63250 — Ryan McCue — 2012-09-30T12:03:42+00:00

URL: https://externals.io/message/63247#63250 · In reply to: #63247

kuldeep dhaka wrote:
> /*
>  i was thinking if is their any function that can do the above work like
> */
> echo array_drill($array, array("school","class","roll_number","name"));

The following should work:

function array_drill($array, $keys) {
	while (($key = array_shift($keys)) !== null) {
		$array =& $array[$key];
	}
	return $array;
}

(Not tested. It will fail if the key doesn't exist, you can optionally
check that and fail however you'd like.)

-- 
Ryan McCue
<http://ryanmccue.info/

---

## #63251 — kuldeep dhaka — 2012-09-30T12:05:36+00:00

URL: https://externals.io/message/63247#63251 · In reply to: #63248

i m extending the wordpress internal widget input api to simplify things,
so for that i wanted a function that perform drill.

i have considered other options but they wont be effecient, if a php
internal function is build it would be just a [] similar one, and most
effecient

because while recursing through the function.
i built a drill function
function array_drill( $array, $key){
    foreach( $key as $k ){
        if( isset( $array[$k] ) )
            $array = $array[$k];
        else
            return false;
    }
    return $array;
}
just convert [] to function drill

On Sun, Sep 30, 2012 at 5:26 PM, Charlie Somerville <
charlie@charliesomerville.com> wrote:

>  I'm struggling to imagine how such a function would be useful considering
> you can already use [] to index arrays.
>
> If you have a variable 'depth' you need to drill down to, then writing a
> function to do that is also fairly trivial.
>
> 
>
> hello guys,
> this is my first time, if thing mail is at wrong place please forward it
> the right place.
> while coding , i just found a requirement, its little bit funny. if it
> already exists, please be calm and mail me the function name and its usage.
>
> for accessing arrays we use
> <?php
> echo $array["school"]["class"]["roll_number"]["name"]; // this will print
> the name of the student
> /*
> i was thinking if is their any function that can do the above work like
> */
> echo array_drill($array, array("school","class","roll_number","name"));
>
> // like wise while assigning values
> array_drill(<array to perform on>, <keys>, <new value>);
> ?>
> i hope you guys would have understood what i mean.
> kuldeep singh dhaka
> kuldeepdhaka9@gmail.com

---

## #63252 — Andrew Faulds — 2012-09-30T15:15:49+00:00

URL: https://externals.io/message/63247#63252 · In reply to: #63251

> i m extending the wordpress internal widget input api to simplify things,
> so for that i wanted a function that perform drill.
>
> i have considered other options but they wont be effecient, if a php
> internal function is build it would be just a [] similar one, and most
> effecient
Not really. A PHP function to implement it, done without recursion, 
would be pretty efficient. And unless you're making tens of thousands of 
these operations, this is probably a case of premature optimisation.

-- 
Andrew Faulds
http://ajf.me/

---

## #63253 — Eitan Mosenkis — 2012-09-30T16:52:48+00:00

URL: https://externals.io/message/63247#63253 · In reply to: #63249

Those look good except that you're using isset($ar[$key]) when you should
be using array_key_exists($key, $ar). If the value you were trying to set
is set to null, your function will erroneously return false instead of null
(since it isn't 'set', but the key does in fact exist)

On Sun, Sep 30, 2012 at 8:03 AM, Anthony Ferrara <ircmaxell@gmail.com>wrote:

> Yes, it's fairly trivial to write a function for this. I wrote a pair of
> them in about 2 minutes:
>
> http://codepad.viper-7.com/rqVlqL
>
>
>
> On Sun, Sep 30, 2012 at 7:56 AM, Charlie Somerville <
> charlie@charliesomerville.com> wrote:
>
> > […]
> it
> > […]
> print
> > […]
