Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18690 > unrolled thread

convert array keys to paths

Started byalex <1j9448a02@lnx159sneakemail.com.invalid>
First post2021-06-28 12:57 +0200
Last post2021-08-05 05:25 -0700
Articles 6 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  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

#18690 — convert array keys to paths

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-06-28 12:57 +0200
Subjectconvert array keys to paths
Message-ID<sbc9ua$11kl$1@gioia.aioe.org>
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.

[toc] | [next] | [standalone]


#18733

FromArne Vajhøj <arne@vajhoej.dk>
Date2021-07-23 21:14 -0400
Message-ID<sdfpho$1ll9$1@gioia.aioe.org>
In reply to#18690
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

[toc] | [prev] | [next] | [standalone]


#18735

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-24 09:06 +0200
Message-ID<sdge67$4vj$1@gioia.aioe.org>
In reply to#18733
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

[toc] | [prev] | [next] | [standalone]


#18745

FromArne Vajhøj <arne@vajhoej.dk>
Date2021-07-24 20:36 -0400
Message-ID<sdibn6$1eia$1@gioia.aioe.org>
In reply to#18735
On 7/24/2021 3:06 AM, alex wrote:
> 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

You changed:

'c1' => 1

to:

'c1' => '1'

and my code required integer.

If you want the final item to be anything then you can use:

function pathjoin_help($o, $elms, &$res) {
     if(is_array($o)) {
         $elms[] = null;
         foreach($o as $pathelm => $value) {
             $elms[count($elms)-1] = $pathelm;
             pathjoin_help($value, $elms, $res);
         }
     } else {
         $res[implode('/', $elms)] = $o;
     }
}

Arne

[toc] | [prev] | [next] | [standalone]


#18748

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-07-25 12:50 +0200
Message-ID<sdjfkr$16j1$1@gioia.aioe.org>
In reply to#18745
Il 25/07/21 02:36, Arne Vajhøj ha scritto:
> 
> If you want the final item to be anything then you can use:

Thanks :)

[toc] | [prev] | [next] | [standalone]


#18758

FromHe Llo <ya12983@mail.com>
Date2021-08-05 05:25 -0700
Message-ID<6cd0516a-7078-4afb-8a2c-a809bddc99dan@googlegroups.com>
In reply to#18748
💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web