Jump to content

Archived

This topic is now archived and is closed to further replies.

arne_anka

1-dimensional arrays from multi-dim array

Recommended Posts

hi,
it's probably very trivial but i still don't get it:
a multi-dimensional array like
$a=array(1 => array
(
2 => array
(
    3 => array(
    "3A" => array(
    "3B" => array(
    "X" => array()
    ),
    "3C" => array(
    "X" => array()
    )
    )
    ),
    4 => array(
    5 => array(
    "X" => array()
    ),
    "5A" => array(
    6 => array(
    "X" => array()
    )
    )
    )
),
)
);

shall be converted to one-dimensional arrays containing a path from 1 to X, in the given example four arrays:
1: array(1, 2, 3, 3A, 3B, X)
2: array(1, 2, 3, 3A, 3C, X)
3: array(1, 2, 4, 5, X)
4: array(1, 2, 4, 5A, 6, X)

any help appreciated!

Share this post


Link to post
Share on other sites
tr[code]<?php
$a=array(
1 => array
  (
      2 => array
        (
            3 => array(
                "3A" => array(
                  "3B" => array(
                      "X" => array()
                  ),
                  "3C" => array(
                      "X" => array()
                  )
                )
            ),
            4 => array(
                5 => array(
                  "X" => array()
                ),
                "5A" => array(
                  6 => array(
                      "X" => array()
                  )
                )
            )
        ),
  )
);

function path($a,$b=array()){
static $c;
if (count($a)==0) $c[]= $b;
foreach ($a as $key => $value){
$b1=$b;
$b1[]=$key;
path($value,$b1);
}
return $c;
}
echo "<pre>";
print_r (path($a));
echo "</pre>";
?>[/code]

Share this post


Link to post
Share on other sites
sasa can you kindly exsplain the code please and break it down cheers.

Share this post


Link to post
Share on other sites
OK
function must return more then one array cose use static variable $c
$a is array from whot i look for path and $b is part of path thet is find (start of path)
if $a has no elements the path is finished and i add $b in array $c
else i add next element to $b and do this again

sorry for bad english

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.