array_splice() or how to move up array elements?
Moderator: General Moderators
-
visionmaster
- Forum Contributor
- Posts: 139
- Joined: Wed Jul 14, 2004 4:06 am
array_splice() or how to move up array elements?
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 )
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
actually, reset doesn't reindex the array, it only resets the internal pointer php.net/reset
i think i have used array_splice before to re-index an array but there's probably a better way.
i think i have used array_splice before to re-index an array but there's probably a better way.
oops i thought it reindexed too. guess not.
this should work on an array that is just numerically indexed
this should work on an array that is just numerically indexed
Code: Select all
$result_array = array_values($result_array);-
visionmaster
- Forum Contributor
- Posts: 139
- Joined: Wed Jul 14, 2004 4:06 am
rehfeld wrote:oops i thought it reindexed too. guess not.
this should work on an array that is just numerically indexed
Code: Select all
$result_array = array_values($result_array);
I already was wondering...
Thanks for your helpful hint!
Just checked documentation and found following user comment:
"If you have a numerically indexed array with some keys missing, ie 1, 2, 4, 5 and you want to reindex it so it's 1,2,3,4 *without changing the positions of the values* (ie sort()) then you can use this function to do it."
-
visionmaster
- Forum Contributor
- Posts: 139
- Joined: Wed Jul 14, 2004 4:06 am
Going on with RegExps
Hi,
My further questions concern RegExps.
I packed the address into an array. With if (preg_match...) I can walktrough the array element for element and search for a string that fits to a pattern holding "Telefon", "Tel.", "Telephone" and "Fon".
O.k., lets assume I found an array-element matching the telephone-pattern, e.g.
Tel. (0 81) 11 22
=> Now I want to extract only numbers. I assume that works the best and most flexible with RegExps? Also characters such as ( ), or - or any other characters should be omitted. Also "Telefon", "Tel.", "Telephone" and "Fon" should be omitted.
Tel. (0 81) 11 22
should become
0811122
The same question for
Internet: http://www.musterfirma.de Some text not of interest
becomes
http://www.musterfirma.de
Appreciate your help!
My further questions concern RegExps.
I packed the address into an array. With if (preg_match...) I can walktrough the array element for element and search for a string that fits to a pattern holding "Telefon", "Tel.", "Telephone" and "Fon".
O.k., lets assume I found an array-element matching the telephone-pattern, e.g.
Tel. (0 81) 11 22
=> Now I want to extract only numbers. I assume that works the best and most flexible with RegExps? Also characters such as ( ), or - or any other characters should be omitted. Also "Telefon", "Tel.", "Telephone" and "Fon" should be omitted.
Tel. (0 81) 11 22
should become
0811122
The same question for
Internet: http://www.musterfirma.de Some text not of interest
becomes
http://www.musterfirma.de
Appreciate your help!
-
visionmaster
- Forum Contributor
- Posts: 139
- Joined: Wed Jul 14, 2004 4:06 am