array_values in php

PHP also provides a (generally less useful) function to retrieve an array of just the values in an array, array_values(): $arrayOfValues = array_values(array); $person = array(‘name’ => “Fred”, ‘age’ => 35, ‘wife’ => “Wilma”); $values = array_values($person); // $values is array(“Fred”, 35, “Wilma”)

array_keys in php

The array_keys() function returns an array consisting of only the keys in the array in internal order: $arrayOfKeys = array_keys(array); Here’s an example $person = array(‘name’ => “Fred”, ‘age’ => 35, ‘wife’ => “Wilma”);$keys = array_keys($person); // $keys is array(“name”, “age”, “wife”)

array_chunk in php

To divide an array into smaller, evenly sized arrays, use the array_chunk() function: $chunks = array_chunk(array, size [, preserve_keys]); The function returns an array of the smaller arrays. The third argument, pre serve_keys, is a Boolean value that determines whether the elements of the new arrays have the same keys as in the original (useful … Read more

array_slice in php

To extract only a subset of the array, use the array_slice() function $subset = array_slice(array, offset, length); The array_slice() function returns a new array consisting of a consecutive series of values from the original array. The offset parameter identifies the initial element to copy (0 represents the first element in the array), and the length … Read more

php list

To copy all of an array’s values into variables, use the list() construct list ($variable, …) = $array; The array’s values are copied into the listed variables in the array’s internal order. By default that’s the order in which they were inserted, but the sort functions described later let you change that. Here’s an example: … Read more

preg_replace php

The preg_replace() function behaves like the search-and-replace operation in your text editor. It finds all occurrences of a pattern in a string and changes those occurrences to something else: $new = preg_replace(pattern, replacement, subject [, limit ]); The most common usage has all the argument strings except for the integer limit. The limit is the … Read more

Quantifiers and Greed

Regular expression quantifiers are typically greedy. That is, when faced with a quantifier, the engine matches as much as it can while still satisfying the rest of the pattern. For instance: The regular expression matches from the first less-than sign to the last greater-than sign. In effect, the .* matches everything after the first less-than … Read more

Delimiters in php

Perl-style regular expressions emulate the Perl syntax for patterns, which means that each pattern must be enclosed in a pair of delimiters. Traditionally, the slash (/) character is used; for example, /pattern/. However, any nonalphanumeric character other than the backslash character () can be used to delimit a Perl-style pattern. This is useful when matching … Read more

implode function in php

The implode() function does the exact opposite of explode()—it creates a large string from an array of smaller strings $string = implode(separator, array); The first argument, separator, is the string to put between the elements of the second argument, array. To reconstruct the simple comma-separated value string, simply say $fields = array(‘Fred’, ’25’, ‘Wilma’);$string = … Read more

substr in php

If you know where the data that you are interested in lies in a larger string, you can copy it out with the substr() function: $piece = substr(string, start [, length ]); The start argument is the position in string at which to begin copying, with 0 meaning the start of the string. The length … Read more