Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #18735
| From | alex <1j9448a02@lnx159sneakemail.com.invalid> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: convert array keys to paths |
| Date | 2021-07-24 09:06 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <sdge67$4vj$1@gioia.aioe.org> (permalink) |
| References | <sbc9ua$11kl$1@gioia.aioe.org> <sdfpho$1ll9$1@gioia.aioe.org> |
Il 24/07/21 03:14, Arne Vajhøj ha scritto:
> On 6/28/2021 6:57 AM, alex wrote:
>> Look at this array
>>
>> array (
>> 'a' =>
>> array (
>> 'b1' =>
>> array (
>> 'c1' => 1,
>> 'c2' => 2,
>> 'c3' => 3,
>> ),
>> 'b2' =>
>> array (
>> 'c' => 4,
>> ),
>> ),
>> )
>>
>> You can convert it to
>>
>> array (
>> 'a/b1/c1' => 1,
>> 'a/b1/c2' => 2,
>> 'a/b1/c3' => 3,
>> 'a/b2/c' => 4,
>> )
>>
>> Possibly without using a complex set of instructions:
>> foeach, array_map (), etc.
>
> A little bit of complexity may be needed.
>
> :-)
>
> I came up with:
>
> function pathjoin_help($o, $elms, &$res) {
> if(is_int($o)) {
> $res[implode('/', $elms)] = $o;
> } else if(is_array($o)) {
> $elms[] = null;
> foreach($o as $pathelm => $value) {
> $elms[count($elms)-1] = $pathelm;
> pathjoin_help($value, $elms, $res);
> }
> } else {
> die("Houston we have a problem");
> }
> }
>
> function pathjoin($a) {
> $res = array();
> pathjoin_help($a, array(), $res);
> return $res;
> }
>
> Arne
function pathjoin_help($o, $elms, &$res) {
if(is_int($o)) {
$res[implode('/', $elms)] = $o;
} else if(is_array($o)) {
$elms[] = null;
foreach($o as $pathelm => $value) {
$elms[count($elms)-1] = $pathelm;
pathjoin_help($value, $elms, $res);
}
} else {
die("Houston we have a problem");
}
}
function pathjoin($a) {
$res = array();
pathjoin_help($a, array(), $res);
return $res;
}
print_r(
pathjoin(
array (
'a' =>
array (
'b1' =>
array (
'c1' => '1',
'c2' => 2,
'c3' => 3,
),
'b2' =>
array (
'c' => 4,
),
),
)
)
);
Houston we have a problem
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar
convert array keys to paths alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-06-28 12:57 +0200
Re: convert array keys to paths Arne Vajhøj <arne@vajhoej.dk> - 2021-07-23 21:14 -0400
Re: convert array keys to paths alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-24 09:06 +0200
Re: convert array keys to paths Arne Vajhøj <arne@vajhoej.dk> - 2021-07-24 20:36 -0400
Re: convert array keys to paths alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-07-25 12:50 +0200
Re: convert array keys to paths He Llo <ya12983@mail.com> - 2021-08-05 05:25 -0700
csiph-web