note 86190 modified in function.array-values by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
<?php
$array=array( 'value1'=>'value',
		    'array1'=> array( 'subItem1'=>'subItem1',	
	  				   'subItem2'=>'subItem2' ),
		    'array2'=> array( 'subItem1'=>'subItem1' ));
			
$newArray=array();
array_flatten($array,$newArray);
print_r( $newArray );

//Output
//Array
//(
//    [value1] => value
//    [array1|subItem1] => subItem1
//    [array1|subItem2] => subItem2
//    [array2|subItem1] => subItem1
//)

// recursively reduces deep arrays to single-dimensional arrays
function array_flatten($array, &$newArray = Array() ,$prefix='',$delimiter='|') {
  foreach ($array as $key => $child) {
	if (is_array($child)) {
	  $newPrefix = $prefix.$key.$delimiter;
	  $newArray =& array_flatten($child, $newArray ,$newPrefix, $delimiter);
	} else {
	  $newArray[$prefix.$key] = $child;
	}
  }
  return $newArray;
}
?>

--was--
$array=array( 'value1'=>'value',
		    'array1'=> array( 'subItem1'=>'subItem1',	
	  				   'subItem2'=>'subItem2' ),
		    'array2'=> array( 'subItem1'=>'subItem1' ));
			
$newArray=array();
array_flatten($array,$newArray);
print_r( $newArray );

//Output
//Array
//(
//    [value1] => value
//    [array1|subItem1] => subItem1
//    [array1|subItem2] => subItem2
//    [array2|subItem1] => subItem1
//)

// recursively reduces deep arrays to single-dimensional arrays
function array_flatten($array, &$newArray = Array() ,$prefix='',$delimiter='|') {
  foreach ($array as $key => $child) {
	if (is_array($child)) {
	  $newPrefix = $prefix.$key.$delimiter;
	  $newArray =& array_flatten($child, $newArray ,$newPrefix, $delimiter);
	} else {
	  $newArray[$prefix.$key] = $child;
	}
  }
  return $newArray;
}

http://php.net/manual/en/function.array-values.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.