私は、キーと値のペアの配列の次のキーを見つけるにはどうすればよいです

ユーザーエージェント :

私の前処理機能では、私は、現在のノードID、およびコンテンツタイプと呼ばれるレシピのためのすべてのノードIDの配列を持っています:

$current_node_id = \Drupal::routeMatch()->getParameter('node');
$nids = \Drupal::entityQuery('node')->condition('type','recipes')->execute();
$recipes_id =  \Drupal\node\Entity\Node::loadMultiple($nids);

絵に次のように$ recipes_idの結果は次のとおりです。

ここでは、画像の説明を入力します。

次に、私はすべてのIDをループにしたいと電流idに等しいIDを取得します:

 $i = 0;
 foreach($recipes_id as $key => $value) {
    if($key == $current_node_id->id()) {

    }
    $i++;
 }

if文では、私は$ key変数の後にあるIDをアクセスしたいです。現在のidが150である場合ので、私は次のように追加if文の内部で、そのためになど、159にアクセスする必要があります。

$variables['node'] = $recipes_id[$i];

私はかかわらず、ページを表示すると、私はNULLを参照してください。

{{ kint(node) }}

ここに私の全体の機能は次のとおりです。

function amarula_preprocess_page(&$variables) {

    /**
     * get current node id
     */ 
    $current_node_id = \Drupal::routeMatch()->getParameter('node');

    /**
     * check the current language
     */
    $current_lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $variables['current_lang'] = $current_lang; //current language code

    /**
     * get all recipes node id
     */
    $nids = \Drupal::entityQuery('node')->condition('type','recipes')->execute();
    $recipes_id =  \Drupal\node\Entity\Node::loadMultiple($nids);
    $variables['recipes_id'] = $recipes_id; // recipes node ID

    $i = 0;
    foreach($recipes_id as $key => $value)
    {
        if($key == $current_node_id->id())
        {
            $variables['node'] = $recipes_id[$i + 1];
            break;
        }
        $i++;
    }
}

現在のIDを持って、どのように私は、アレイ内のその後IDを見つけることができますか?

オマルアッバス:

ここでは、次のキーを取得する方法です。

$keys = array_keys($recipes_id);
$i = 0;
foreach($keys as $value)
{
    if($value == $current_node_id->id()){
        $variables['node'] = $keys[$i + 1];
        break;
    }
    $i++;
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=294561&siteId=1