Jump to content


Photo

1-dimensional arrays from multi-dim array


  • Please log in to reply
3 replies to this topic

#1 arne_anka

arne_anka
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 August 2006 - 09:57 AM

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!

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 August 2006 - 10:17 PM

tr
<?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>";
?>


#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 10:21 PM

sasa can you kindly exsplain the code please and break it down cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 August 2006 - 10:54 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users