PHP:将 CURL 结果放入数组。在数组中的 A-Z 值之后停止,忽略后面的内容
PHP: CURL result into array. Stop after A-Z values in array, disregard what follows
在对页面进行 CURL 处理并在数组中创建项目列表之后。我无法控制标记。我正在尝试过滤掉字母 Z 之后不跟在字母表后面的最后剩余项目。
在这种情况下,我想忽略索引 7 及以后的索引。
Array
(
[0] => Apple
[1] => Acorn
[2] => Banana
[3] => Cucumber
[4] => Date
[5] => Zombify
[6] => Zoo // last item
[7] => Umbrella // disregard
[8] => Kangaroo // disregard
[9] => Apple // disregard
[10] => Star // disregard
[11] => Umbrella // disregard
[12] => Kangaroo // disregard
[13] => Apple // disregard
)
我想不通的是字母 Z 处的截止点的适当解决方案。
$letters = range('A', 'Z');
foreach($listContent as $listItem) {
foreach($letters as $letter) {
if (substr($listItem, 0, 1) == $letter) {
$newArray[] = $listItem;
}
}
}
这个怎么样:
$last= "A";
foreach($listContent as $listItem) {
$current = strtoupper(substr($listItem, 0,1));
// only add the element if it is alphabetically sorted behind the last element and is a character
if ($current < $last || $current < 'A' || $current > 'Z') break;
$last = $current;
$newArray[] = $listItem;
}
已对此进行测试并正常工作 - 根据我对您的要求的理解:
$letters = range('A', 'Z');
$item_prev_char = "";
foreach( $listContent as $listItem )
{
$item_first_char = substr($listItem, 0, 1);
if ( $item_prev_char <= $item_first_char )
{
foreach( $letters as $letter )
{
if ($item_first_char == $letter )
{
$new_array[] = $listItem;
$item_prev_char = $item_first_char;
echo $listItem."<br/>";
break;
}
}
}
}
这将处理您发布的更新数组。例如,它会保留 Zombie 和 Zoo,但它会结束。
$letters = range('A', 'Z');
$newArray = array();
foreach ($listContent as $listItem) {
$firstLetter = substr($listItem, 0, 1);
if (!in_array($firstLetter, $letters)) {
break; // edited to break instead of continue.
}
$position = array_search($firstLetter, $letters);
$letters = array_slice($letters, $position);
$newArray[] = $listItem;
}
在对页面进行 CURL 处理并在数组中创建项目列表之后。我无法控制标记。我正在尝试过滤掉字母 Z 之后不跟在字母表后面的最后剩余项目。
在这种情况下,我想忽略索引 7 及以后的索引。
Array
(
[0] => Apple
[1] => Acorn
[2] => Banana
[3] => Cucumber
[4] => Date
[5] => Zombify
[6] => Zoo // last item
[7] => Umbrella // disregard
[8] => Kangaroo // disregard
[9] => Apple // disregard
[10] => Star // disregard
[11] => Umbrella // disregard
[12] => Kangaroo // disregard
[13] => Apple // disregard
)
我想不通的是字母 Z 处的截止点的适当解决方案。
$letters = range('A', 'Z');
foreach($listContent as $listItem) {
foreach($letters as $letter) {
if (substr($listItem, 0, 1) == $letter) {
$newArray[] = $listItem;
}
}
}
这个怎么样:
$last= "A";
foreach($listContent as $listItem) {
$current = strtoupper(substr($listItem, 0,1));
// only add the element if it is alphabetically sorted behind the last element and is a character
if ($current < $last || $current < 'A' || $current > 'Z') break;
$last = $current;
$newArray[] = $listItem;
}
已对此进行测试并正常工作 - 根据我对您的要求的理解:
$letters = range('A', 'Z');
$item_prev_char = "";
foreach( $listContent as $listItem )
{
$item_first_char = substr($listItem, 0, 1);
if ( $item_prev_char <= $item_first_char )
{
foreach( $letters as $letter )
{
if ($item_first_char == $letter )
{
$new_array[] = $listItem;
$item_prev_char = $item_first_char;
echo $listItem."<br/>";
break;
}
}
}
}
这将处理您发布的更新数组。例如,它会保留 Zombie 和 Zoo,但它会结束。
$letters = range('A', 'Z');
$newArray = array();
foreach ($listContent as $listItem) {
$firstLetter = substr($listItem, 0, 1);
if (!in_array($firstLetter, $letters)) {
break; // edited to break instead of continue.
}
$position = array_search($firstLetter, $letters);
$letters = array_slice($letters, $position);
$newArray[] = $listItem;
}