arne_anka Posted August 1, 2006 Share Posted August 1, 2006 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 https://forums.phpfreaks.com/topic/16190-1-dimensional-arrays-from-multi-dim-array/ Share on other sites More sharing options...
sasa Posted August 1, 2006 Share Posted August 1, 2006 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 https://forums.phpfreaks.com/topic/16190-1-dimensional-arrays-from-multi-dim-array/#findComment-67404 Share on other sites More sharing options...
redarrow Posted August 1, 2006 Share Posted August 1, 2006 sasa can you kindly exsplain the code please and break it down cheers. Link to comment https://forums.phpfreaks.com/topic/16190-1-dimensional-arrays-from-multi-dim-array/#findComment-67406 Share on other sites More sharing options...
sasa Posted August 1, 2006 Share Posted August 1, 2006 OKfunction 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 $celse i add next element to $b and do this againsorry for bad english Link to comment https://forums.phpfreaks.com/topic/16190-1-dimensional-arrays-from-multi-dim-array/#findComment-67436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.