Re: segmentation fault - my fault?
| From: | Jeremy Privett | Date: | Mon, 12 Feb 2007 19:53:31 +0000 |
| Subject: | Re: segmentation fault - my fault? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-248788@lists.php.net to get a copy of this message | ||
Jim Lucas wrote:
Marc Weber wrote:function flatten_array() { $return = array(); foreach ( func_get_args() as $arg ) {Does this script cause a segmentation fault running on your php interpreter, too? ============= ======================================================= <?php function fa() { $res = array(); foreach(func_get_args() as $a)Try this: <plaintext><?php function fa() {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) Marcforeach ( 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. Off the cuff, just threw this together really quickly:
if ( is_array($arg) ) {
foreach ( $arg as $k => $v ) {
if ( is_array($v) ) {
$return = array_merge($return, flatten_array($v));
}
else {
if ( is_numeric($k) and intval($k) == $k ) {
$return[] = $v;
}
else {
$return[$k] = $v;
}
}
}
}
else {
$return[] = $arg;
}
}
return $return;
}
$flatten = array( 1, 2, 3, 4, array( 5, 6, array( 7, 8, 9, 10, array( 'test' => 11, 'test2' => 12 ), 13 ), 14, 15 ), 16, 17, 18, 19, 20 );
echo "<pre>" . print_r(flatten_array($flatten), true) . "</pre>";
Returns:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[test] => 11
[test2] => 12
[10] => 13
[11] => 14
[12] => 15
[13] => 16
[14] => 17
[15] => 18
[16] => 19
[17] => 20
)
This preserves array keys if it's an associative array being flattened. If the array is numerically indexed, it just appends the value to the end.
Hope this helps.
--
Jeremy C. Privett
Chief Operating Officer
Zend Certified Engineer
Completely Unique
jprivett@completelyunique.com
Phone: 303.459.4819
Mobile: 303.883.0312
Fax: 303.459.4821
Web: www.completelyunique.com
This email may contain confidential and privileged material for the sole use of the intended
recipient. Any review or distribution by others is strictly prohibited. If you are not the intended
recipient please contact the sender and delete all copies. Your compliance is appreciated.