Page 1 of 1

array_splice() or how to move up array elements?

Posted: Sat Dec 18, 2004 5:22 am
by visionmaster
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 )

Posted: Sat Dec 18, 2004 11:09 am
by rehfeld
try reset() on the array after your done unsetting all the stuff you dont want

it will re-index for you.

Posted: Sat Dec 18, 2004 9:07 pm
by andre_c
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.

Posted: Sat Dec 18, 2004 9:29 pm
by rehfeld
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);

Posted: Sun Dec 19, 2004 11:18 am
by visionmaster
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."

Going on with RegExps

Posted: Sun Dec 19, 2004 12:19 pm
by visionmaster
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!

Posted: Sun Dec 19, 2004 12:58 pm
by timvw
the easiest (imho) is to replace everyting that is not a decimal, by ''

Posted: Sun Dec 19, 2004 2:33 pm
by visionmaster
Thanks for your response. Any specific example?

Posted: Sun Dec 19, 2004 2:37 pm
by timvw
\d
Matches any decimal digit; this is equivalent to the class [0-9].

\D
Matches any non-digit character; this is equivalent to the class [^0-9]