note 72087 deleted from function.explode by colder
| From: | colder@php.net | Date: | Tue, 09 Jan 2007 10:26:41 +0000 |
| Subject: | note 72087 deleted from function.explode by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121480@lists.php.net to get a copy of this message | ||
Note Submitter: sampathperera at hotmail dot com Sri Lanka
----
I just need to create an array with array key is same as value. By explode we can get an array from
key [0] to [n].
First I created an array() using explode, then assign keys using foreach and array_shift().
<?php
$clist = "COLOMBO
KANDY
GALLE
PANADURA";
$clist = explode("\n",$clist);
print_r($clist);
foreach ($clist as $k => $v) {
$clist[$v] = array_shift($clist);
}
print_r($clist);
?>
/////////////////// Output ///////////////////////
Array
(
[0] => COLOMBO
[1] => KANDY
[2] => GALLE
[3] => PANADURA
)
Array
(
[COLOMBO
] => COLOMBO
[KANDY
] => KANDY
[GALLE
] => GALLE
[PANADURA] => PANADURA
)
Note : I have used explode("\n",$str) insted of explode('\n',$str). I faced
problems using explode('\n',$str) for new-line.