note 74757 added to function.array-merge
| From: | HalitYE�İLmailathalityesildotcom at osu1 dot php dot net | Date: | Thu, 26 Apr 2007 11:54:09 +0000 |
| Subject: | note 74757 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125886@lists.php.net to get a copy of this message | ||
Multi Array in single array function and array to string function...
<?PHP
/*
* code by Halit YEÅİL
* http://www.halityesil.com , http://www.trteknik.org , http://www.mysqlturkiye.com , http://www.trwhois.com/php.net
*
*/
/*
* Verilen diziyi sonuca string olarak düzenlenmiŠhalde verir
*
* @param array a
* @param int level
* @return string
*/
function array2string($a, $level = 0){
//EÄer dizi deÄilse direkt sonuca aktar
if(!is_array($a)) return $a;
//Satır sonlarını ayarla
$ln = "\r\n";
//Level boyunca boÅluk oluÅtur girinti
$l = return2space($level*11);
//Array elementi için girinti yap
$e = return2space(6);
//Array adını belirle
$o = 'Array('.$ln;
//iç girintiyi ayarla
$x = $l.$e;
//Diziyi döngüye al
foreach($a as $k=>$v){
//Sonucu stringe ekle ve iÅlemi tekrarlat
$o .= $x.'[ '.$k.' ]=> '. array2string($v, $level+1) . $ln;
}
//Diziyi sonlandır
$o .= $l.')';
//Sonucu çevir
return $o;
}
/*
*
* Verilen deÄer kadar html olarak boÅluk çevirir ascii
*
* @param int s
* @return string
*/
function return2space($s){ $l = ''; for($i=0; $i<$s; $i++) $l
.=' '; return $l; }
$a = Array (
'deneme',
arr1=>array(
array(
'value1',
variable=>'values2',
544545
),
'values3',
5456,
array(
testk=>array(ka=>546, 'asdsada'),
45644564
),
'assdasda'
),
arr2=>array(
array(
'value4',
variable=>'values5',
544545
),
'values6',
5456,
array(
testk=>array(ka=>546, 'asdsada'),
45644564
),
'assdasda'
),
'asdsadsada',
544556
);
function m2sarray($a, $s = '_', $p = false){
if(!is_array($a)) return array();
$out = array();
$pre = ($p && $p != '')?$p.$s:'';
foreach($a AS $k => $v){
$pkey = $pre.((is_int($k) && $pre == '')?$s.$k:$k);
if(!is_array($v))
$out[$pkey] = $v;
else{
$na = m2sarray($v, $s, $pkey);
if(is_array($na)) $out = $out + $na;
}
}
return $out;
}
echo '<pre>';
echo nl2br(array2string($a));
echo '<hr>';
echo nl2br(array2string(m2sarray($a, '.')));
echo '<hr>';
echo '</pre>';
?>
----
Server IP: 85.111.6.23
Probable Submitter: 144.122.180.150
----
Manual Page -- http://www.php.net/manual/en/function.array-merge.php
Edit -- https://master.php.net/note/edit/74757
Del: integrated -- https://master.php.net/note/delete/74757/integrated
Del: useless -- https://master.php.net/note/delete/74757/useless
Del: bad code -- https://master.php.net/note/delete/74757/bad+code
Del: spam -- https://master.php.net/note/delete/74757/spam
Del: non-english -- https://master.php.net/note/delete/74757/non-english
Del: in docs -- https://master.php.net/note/delete/74757/in+docs
Del: other reasons-- https://master.php.net/note/delete/74757
Reject -- https://master.php.net/note/reject/74757
Search -- https://master.php.net/manage/user-notes.php