Re: segmentation fault - my fault?

From: Date: Mon, 12 Feb 2007 19:37:08 +0000
Subject: Re: segmentation fault - my fault?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-248784@lists.php.net to get a copy of this message
Marc Weber wrote:
Does this script cause a segmentation fault running on your php interpreter, too? ============= ======================================================= <?php function fa() { $res = array(); foreach(func_get_args() as $a)
    if (is_array($a)){
      foreach(fa($a) as $a2)
        $res[]=$a2;
    }else
      $res[]=$a;
return $res; } var_dump(fa(array(1,2),array(array(3,4),array(5,6)))); ?> ============= ======================================================= My version: marc@localhost ~ $ php -v PHP 5.1.6-pl6-gentoo (cli) (built: Feb 11 2007 02:37:11) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies This function should take all elements in all arrays and flatten them down to one array. Thus the result of the example above should be array(1,2,..,6) Marc
Try this: <plaintext><?php function fa() { foreach ( func_get_args() AS $arg ) { if ( is_array($arg) ) { foreach( $arg AS $value ) { flatten_array( $value ); } } else { $GLOBALS['master_arr'][] = $arg; } } } $GLOBALS['master_arr'] = array(); fa(array(1,2),array(array(3,4),array(5,6))); print_r($GLOBALS['master_arr']); ?> This does not preserve array keys, but you didn't mention wanting to preserve them. -- Enjoy, Jim Lucas Different eyes see different things. Different hearts beat on different strings. But there are times for you and me when all such things agree. - Rush

« previous php.general (#248784) next »