array_splice() or how to move up array elements?
Posted: Sat Dec 18, 2004 5:22 am
Hi,
A text block ist passed over to the function extractAdress.
This text block ist splitteg with preg_split with regexps.
array_unique gets rid of double values in the array.
So, to get rid of array-elements holding whitespace characters I use trim() in the foreach loop. If the arra-element ist empty, I just delete this single array-element.
=> Actually works fine, but the integer keys are not successive. Not 0,1,2,3 but 0,4,6,7,8,9... since empty array-elements werde deleted by unset.
I want the array to "move up". So if for example the array-element 3 holds an empty element then element 4 should become 3 and 5 should become 4 and so on. Would array_splice the right solution? If yes, now to?
Thanks!
PHP code:
<?php
function extractAddress($arrParsedBlocks,$i)
{
$string = $arrParsedBlocks[$i];
$keywords = preg_split("/(<br>|;|\n|\|)+/", $string);
$result_array = array_unique($keywords);
# Get rid of empty array elements.
foreach ($result_array as $key => $value)
{
$value = trim($value);
if (empty($value)) unset($result_array[$key]);
}
print_r($result_array);
}
?>
Ausgabe:
-------------
Array ( [0] => Kontakt [4] => Fa. Musterfirma GmbH [6] => Dr. Jochen [7] => Mustermann [8] => IT-Abteilung [9] => Musterstr. 9 [10] => 82393 Musterort [11] => Germany [12] => Tel. (0 81) 11 22 [13] => Handy (0 1 72) 99 0 [14] => Internet: http://www.musterfirma.de [15] => E-Mail: mustermann@musterfirma.de )
A text block ist passed over to the function extractAdress.
This text block ist splitteg with preg_split with regexps.
array_unique gets rid of double values in the array.
So, to get rid of array-elements holding whitespace characters I use trim() in the foreach loop. If the arra-element ist empty, I just delete this single array-element.
=> Actually works fine, but the integer keys are not successive. Not 0,1,2,3 but 0,4,6,7,8,9... since empty array-elements werde deleted by unset.
I want the array to "move up". So if for example the array-element 3 holds an empty element then element 4 should become 3 and 5 should become 4 and so on. Would array_splice the right solution? If yes, now to?
Thanks!
PHP code:
<?php
function extractAddress($arrParsedBlocks,$i)
{
$string = $arrParsedBlocks[$i];
$keywords = preg_split("/(<br>|;|\n|\|)+/", $string);
$result_array = array_unique($keywords);
# Get rid of empty array elements.
foreach ($result_array as $key => $value)
{
$value = trim($value);
if (empty($value)) unset($result_array[$key]);
}
print_r($result_array);
}
?>
Ausgabe:
-------------
Array ( [0] => Kontakt [4] => Fa. Musterfirma GmbH [6] => Dr. Jochen [7] => Mustermann [8] => IT-Abteilung [9] => Musterstr. 9 [10] => 82393 Musterort [11] => Germany [12] => Tel. (0 81) 11 22 [13] => Handy (0 1 72) 99 0 [14] => Internet: http://www.musterfirma.de [15] => E-Mail: mustermann@musterfirma.de )