【问题标题】:array_search() doesn't match on the last keyarray_search() 与最后一个键不匹配
【发布时间】:2017-05-22 01:17:25
【问题描述】:

我正在尝试创建一个动态抓取特定页面的所有子页面的函数。然后它会检查当前页面是什么,并抓取接下来的三个页面以显示为预览。

最后一页 ID 是 6120。 您可以对 $pagesArray() 以及 $lastKey 和 $matches 进行硬编码以查看结果。

$pagesArray(
[0] => 6135
[1] => 6139
[2] => 6176
[3] => 6178
[4] => 6163
[5] => 6152
[6] => 6167
[7] => 6183
[8] => 6201
[9] => 6190
[10] => 6172
[11] => 6197
[12] => 6205
[13] => 6154
[14] => 6120
);
$lastKey = 14;
$matches = 14;

当我转到最后一页时,页面 ID 是 6120,这是数组的第 14 个 ID。 它应该返回前 3 个页面 ID,但它不会匹配。请帮忙。这是代码。 作为参考,$matches == $lastKey 应该是设置 $nextPagesArray() 的条件的一部分。他们都是 14 岁,但不匹配。

function getNext3PortfolioPages(){

global $post;

$currentPage = $post->ID;
$last = '';
$lastKey = '';
$pagesArray = array();
$nextPagesArray = array();

$args = array(
    'child_of' => '6115',
    'offset' => $post->ID
);

// get all the page id's that are children of page 6115
$result = get_pages($args);

// add the page id's to the $pagesArray
foreach ( $result as $page ){
    array_push($pagesArray, $page->ID);
}

//find the last page id in the array
$last = end($pagesArray);

// find the index of the last array value
$lastKey = key($pagesArray);

// find the index of the current page displayed
$matches = array_search($currentPage, $pagesArray);

// check to see if the current page index is the same as the end of the array 
// and set up the $nextPagesArray with the next 3
if ( (int)$matches == (int)$lastkey ) {
    //print_r('1');
    //print_r($pagesArray);
    array_push($nextPagesArray, $pagesArray[0]);
    array_push($nextPagesArray, $pagesArray[1]);
    array_push($nextPagesArray, $pagesArray[2]);

} elseif ( $matches == $lastkey - 1 ) {
    //print_r('2');
    array_push($nextPagesArray, $pagesArray[$lastKey]);
    array_push($nextPagesArray, $pagesArray[0]);
    array_push($nextPagesArray, $pagesArray[1]);

} elseif ( $matches == $lastkey - 2 ) {
    //print_r('3');
    array_push($nextPagesArray, $pagesArray[$lastKey - 1]);
    array_push($nextPagesArray, $pagesArray[$lastKey]);
    array_push($nextPagesArray, $pagesArray[0]);

} else {
    //print_r('4');
    array_push($nextPagesArray, $pagesArray[$matches + 1]);
    array_push($nextPagesArray, $pagesArray[$matches + 2]);
    array_push($nextPagesArray, $pagesArray[$matches + 3]);

}

return $nextPagesArray;
}

【问题讨论】:

    标签: php arrays search


    【解决方案1】:

    如果你想让你的数组“环绕”可以这么说,你可以使用前三个元素来扩展你的数组,如下所示:

    输入:

    $pages=[6135,6139,6176,6178,6163,6152,6167,6183,6201,6190,6172,6197,6205,6154,6120];
    

    方法(Demo):

    $extended_pages=array_merge($pages,array_slice($pages,0,3));
    
    if($x=array_search(6120,$pages)){
        $next3=array_slice($extended_pages,$x+1,3);
        var_export($next3);
    }
    

    输出:

    array (
      0 => 6135,
      1 => 6139,
      2 => 6176,
    )
    

    这将使您不必进行大量条件检查,并且无论搜索中使用的页码如何(当然,只要该页码存在于数组中)都可以工作。

    【讨论】:

    • @JohnDavid 乐于助人。如果您想让我澄清任何事情,请告诉我。
    • 我不会撒谎,我充其量是中级,但我什至没有想到这一点,这完全有道理。没有条件句只是从末尾滚动到前面。
    • @JohnDavid 在过去的 3 个月里,我才真正活跃在 SO 上,但我学到的东西数量惊人。这个地方是学习聪明技术和分享最佳实践的最佳场所。向前支付。编码愉快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多