/    Sign up×
Community /Pin to ProfileBookmark

check for multiple dynamic array keys

Hello,

If there’s an array such as:

[CODE]
$arr=array(
‘key1’=>array(
‘key1a’=>array(…),
‘key1b’=>array(…)
)
);
[/CODE]

and, since ‘key1’, ‘key1b’, etc may be any number of keys, I want to find out if $arr[‘key1’][‘key1b’] exists using a function like:

[CODE]
function updateArr($keys, $val){
global $arr;
//check if $arr[$k1][$k2][$k3] exists

//update $arr[$k1][$k2][$k3] equal to $val

}
updateArr(array($k1,$k2,$k3,…),’myVal’);//call that function to update the array
[/CODE]

How should I go about doing that? For example, should I use something like “foreach($keys as $key)” and use a reference to $arr (&$arr, for example)? If so, how would I reference it? Or is there something easier to use for this?

Thanks in advance.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@speghettiCodeauthorFeb 03.2012 — Still struggling with this...here's another way of asking the question:

If I have [CODE]$keys=array('a','ab','abc');[/CODE] How can I tell if $arr['a']['ab]['abc'] exists if the keys are dynamic?

For example, do i loop through somehow (and how):
[CODE]
$ok=0;
foreach ($keys as $k){
if(array_key_exists($k,$arr)){$arr=$arr[$k];$ok=1;}else{$ok=0;break;}
}
[/CODE]


Is there a built-in function that can do this? something like:
[CODE]
array_key_exists($keys,$arr)
[/CODE]


PHP.net says "bool array_key_exists ( mixed $key , array $search )" so $key presumably can be an array. But how is it handled in the search? I can't seem to find the answer to this.

Any help, work-arounds, or ideas are appreciated.
Copy linkTweet thisAlerts:
@bionoidFeb 03.2012 — What you seem to need is a way to check if a value exists in a array.

You can use in_array for that purpose:

[code=php]<?php

$keys = array('a', 'ab', 'abc');

echo in_array('abc', $keys) ? 'Yes' : 'No'; //Yes

?>[/code]


If you have an array hierarchy and you want to know if it exists, you can use isset:

[code=php]<?php

$keys = array(
'a' => array(
'ab' => array(
'abc' => 'value'
)
)
);

echo isset($keys['a']['ab']['abc']) ? 'Yes' : 'No'; //Yes

?>[/code]


Let me know if I'm missing the plot.
Copy linkTweet thisAlerts:
@speghettiCodeauthorFeb 03.2012 — Thanks for the response bionoid.

Your second example is what I am looking to do. The trouble is that I don't know how many levels there will be in the array or what the keys will be. So I need something that can check 'isset' using a variable number of keys.

I guess my trouble is how to build the structure of the 'isset' argument:

with:[CODE]
$keys=array('a','ab',...'abcdefghij');
[/CODE]


how could I get the argument for:[CODE]
isset($arr[ $keys[0] ][ $keys[1] ]...[ $keys[ (count($keys)-1) ] ])
[/CODE]
Copy linkTweet thisAlerts:
@bionoidFeb 03.2012 — Ok, I was thinking about it before you responded... and came up with this:

[code=php]<?php

$arr = array(
'a' => array(
'ab' => array(
'abc' => 'value'
)
)
);

function updateArr($keys, $arr, $default = null)
{
foreach ($keys as $key) {if (isset($arr[$key])) {$arr = &$arr[$key];} else {return $default;}}
return $arr;
}

//Examples
echo updateArr(array('a', 'ab', 'abc'), $arr, 'Default'); //value
echo updateArr(array('ab', 'a', 'abc'), $arr, 'Default'); //Default

?>[/code]
Copy linkTweet thisAlerts:
@speghettiCodeauthorFeb 03.2012 — Ah! Thanks for your help bionoid.

I had something nearly identical to that:
[CODE]&$arr = $arr[$key];[/CODE]
(which didn't work) instead of
[CODE]$arr = &$arr[$key];[/CODE]

That makes sense and will get the job done. Thanks!
×

Success!

Help @speghettiCode spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...