This could come into play if you want to separate a string based on more than one element...

Linki


» Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.
»
STRING VARIABLE CPEB 15 DS; STRING VARIABLE CPART1 15 DS; ...
»
Drugi element, ktry bdzie mia wpyw na polsk polityk celn, to system preferencji handlowych, udzielanych przez Uni Europejsk krajom Afryki, Karaibw i Pacyfiku...
»
prawym przyciskiem myszy element, który ma zmieniæ miejsce, a nastêpnie z wy- œwietlonego menu wybierz polecenie Przenieœ w górê lub Przenieœ w dó³...
»
Po wejciu Wooda do zespou, cho na razie tylko tymczasowym, zaczo si wyczuwa obecno elementu przyjani, o ktrej w przypadku Taylora trudno waciwie byo mwi...
»
Teorię Conwaya wspierał również całkowity brak przezroczystych elementów kon- strukcyjnych, a w szczególności iluminatorów, chociaż one akurat...
»
Test rnicCzsto obserwujemy zmian wartoci cechy u tego samego elementu w dwch rnych sytuacjach: np...
»
word into a deque<string>...
»
Po wykonaniu instrukcji String arr[][] = new String [3][]; odnoœnik arr identyfikuje 3-elementow¹ tablicê odnoœników do tablic...
»
miotem sporów...
»
– Przecież to mój fach, uczyłem się tego...

Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.

Say you had a string you needed as an array, the elements of which could be separated by either a new line or a tab. The following would do the trick:
//note there is a tab between 524 and 879
//and a tab between 879 and 321
$items = “524 879 321
444
221”;
$array = split(“[\n\t]”, “$items”);
NOTE
split() is more flexible than explode(), but it’s also slower.
PREG_SPLIT( )
This works like split(), only it uses a Perl regular expression as
the pattern.
array preg_split (string pattern, string subject [, int limit [, int flags]])
3537-4 ch06.f.qc 12/15/00 15:22 Page 143
Chapter 6: PHP’s Built-in Functions
143
Note that if the flag is PREG_SPLIT_NO_EMPTY, empty items will not be placed in the array.
Again, if explode() can do it, make sure to use it.
TYPE CONVERSION FUNCTIONS NOT USED IN THIS BOOK
In addition to the functions in the previous section, you can make use of spliti(), which uses a case-insensitive pattern match.
Array functions
I am a big fan of the array functions available in PHP 4. Just about anything you’d like to do to an array you can do with a built-in function. The developers of PHP have done a good job of making sure you have to loop though arrays very infrequently.
In the PHP 4.0.2 manual there are exactly 47 listed array functions. It’s likely that by the time you read this chapter, there will be several more. So make sure you scan the manual to see the full range of available array functions.
See Chapter 5 for a discussion of how to create, add to, and walk through an array.
XREF
ARRAY FUNCTIONS USED IN THIS BOOK
When you’re dealing with database applications, much of your logic should come within your SQL statements. Thus, in the applications presented in this book fairly few array functions were necessary. Here’s a rundown of the ones we used.
ARRAY_FLIP( )
This function, which is useful with associative arrays, exchanges the keys and values. That is, the keys become the values and the values become the keys.
array array_flip (array trans)
This comes up once in the course of the book, in the following code: $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
$title = strtr($title, $trans);
Before the array_flip() function, the array will return many elements. Here are a couple of examples:
[(c)] => &copy
[(r)] => &reg
3537-4 ch06.f.qc 12/15/00 15:22 Page 144
144
Part II: Working with PHP Once the array is flipped, these entries will look like this:
[$copy] => (c)
[&reg] => (r)
Then strtr() replaces each value to its key. So in the end this code will make sure that any character that needs to be represented by an HTML entity will be.
Note that if an array has two identical values before being flipped, only one can survive in the flipped array. You can’t have two array elements with same key. If there is a conflict the element in the right-most position will be maintained.
ARRAY_MERGE( )
As you can probably guess, this function merges, or concate-
nates, two or more arrays.
array array_merge (array array1, array array2 [, array ...])
If any of the arrays contain the same associative keys, the elements in the rightmost array will be preserved.
ARRAY_SPLICE( )
This function takes the array indicated in the first argument and
removes all elements following the offset specified in the second argument. It can then insert additional elements.
array array_splice (array input, int offset [, int length [, array
replacement]])
If the offset is a positive number, the elements will be counted from the left; if the offset is a negative number, all items to the left of the indicated number will be deleted. The optional third argument can indicate how many elements after the offset you wish to delete. For example:
$knicks_array = array (“Childs”, “Sprewell”, “Ewing”,
“Johnson”,”Houston”);
array_splice($knicks_array, 2,1);
will remove elements starting at offset 2 and remove only one element. So “Ewing”
will be deleted from this array. array_splice() also gives you the ability replace the deleted portion with another array. So, to account for trades, you can do this.
$knicks_array = array (“Childs”, “Sprewell”, “Ewing”,
“Johnson”,”Houston”);
$new_knicks = array ( “Longley”,”Rice”);
array_splice($knicks_array, 2,1,$new_knicks);
3537-4 ch06.f.qc 12/15/00 15:22 Page 145
Chapter 6: PHP’s Built-in Functions
145
Following this code, $knicks_array would contain six elements: Childs, Sprewell, Longley, Rice, Johnson, Houston.
Note that the value returned by this function is an array of the deleted items. In the code that follows, $traded_knicks will be an array with one element, “Ewing”.
$traded_knicks = array_splice($knicks_array, 2,1);
COUNT( )
This returns the number of elements in an array, and is frequently used with loops.
int count (mixed var)
For example:
$array = array(1,2,3,4,5);
for($i=0; $i<count($array); $i++)
{
echo $array[$i] . “<br>\n”;
}
Note that sizeof() is a synonym for count().
ARRAY FUNCTIONS NOT USED IN THIS BOOK
Again, there are many great array functions in PHP 4. Here are some of the highlights (from my point of view, anyway).
ARRAY_COUNT_VALUES( )
This nifty function will return an associative array, the
keys of which will be all of the unique values within the array.
array array_count_values (array input)
The values of the resulting array will be an integer representing the number of times the value appears within the array.

Powered by MyScript