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!
Link to comment
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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.