Remove first 2 from Array
I have this simple array and I'd like to know how I can stop the first 2 segments from showing.
if(preg_match_all('/<td[^>]*class="sourceNameCell">(.*?)<\/t开发者_开发知识库d>/si', $printable, $matches, PREG_SET_ORDER));{
foreach($matches as $match) {
$data = "$match[1]";
$array = array();
preg_match( '/src="([^"]*)"/i', $data, $array ) ;
print_r("$array[1]") ;
}
Any help would be great, Thanks!
Use array_slice
:
$output = array_slice($input, 2);
Stop from showing or removing?
for removing:
$array = array();
preg_match( '/src="([^"]*)"/i', $data, $array ) ;
// The following lines will remove values from the first two indexes.
unset($array[0]);
unset($array[1]);
// This line will re-set the indexes (the above just nullifies the values...) and make a new array without the original first two slots.
$array = array_values($array);
// The following line will show the new content of the array
var_dump($array);
Hope this helps!
Use array_slice($array, 2, count($array))
or make your regexp skip the first two if possible.
You could also invoke array_shift() twice on the array. This might be more optimal since it shouldn't need to make a copy of the array.
You could use array_splice
to remove elements from array.
array_splice — Remove a portion of the array and replace it with something else
Elements are removed from original array and returned
$input = array("red", "green", "blue", "yellow", "black");
$part = array_splice($input, 1, 2);
var_dump($input);
var_dump($part);
See PHP fiddle
精彩评论