Jump to content

1-dimensional arrays from multi-dim array


arne_anka

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!
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]
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

Archived

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

×
×
  • Create New...

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.