php 从数组中删除前 2 个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6723247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:04:29  来源:igfitidea点击:

Remove first 2 from Array

php

提问by karim79

I have this simple array and I'd like to know how I can stop the first 2segments from showing.

我有这个简单的数组,我想知道如何阻止前 2 个片段的显示。

if(preg_match_all('/<td[^>]*class="sourceNameCell">(.*?)<\/td>/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!

任何帮助都会很棒,谢谢!

采纳答案by Moshe Eshel

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!

希望这可以帮助!

回答by karim79

Use array_slice:

使用array_slice

$output = array_slice($input, 2); 

回答by jishi

Use array_slice($array, 2, count($array))or make your regexp skip the first two if possible.

array_slice($array, 2, count($array))如果可能,使用或让您的正则表达式跳过前两个。

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.

您还可以在数组上调用 array_shift() 两次。这可能更优化,因为它不需要复制数组。