简体   繁体   中英

how to get next and prev item on array with custom key in php

I have an array with custom key value, not incremental eg

$array_same_cat = array("SF124" => "value", "XA127" => "value2", "AT257" => "value3");

Now what I am working on is to search having a key value (XA127) the previous key and the next key and their value.

Here below the code that generate the array:

$array_same_cat = array();
if($loop_arrows->have_posts()){
    while($loop_arrows->have_posts()){
        $loop_arrows->the_post();
        $current_id = get_the_ID();
        $this_prod_sku = get_post_meta( $current_id, '_sku', true );
        $array_same_cat[$this_prod_sku] = esc_url(get_permalink(intval($this_prod_sku)));
    }               
}

Added this With this foreach I found the exact position of my element. Now I have to find how to do prev and next.

foreach($array_same_cat as $ar){
    if($ar == $array_same_cat[$current_sku]){
        echo 'found';
    }
}

What i understand from your question:-

  1. You have array

  2. You have a key, which you want to search in array and then you need to find-out prev and next value after it

Now you can do it like below:-

<?php

$array_same_cat = array("SF124" => "value", "XA127" => "value2", "AT257" => "value3");

$search_key = 'XA127';

$keys_array = array_keys($array_same_cat);//get keys array from original array

$get_search_value_key_from_value_array = array_search($search_key,$keys_array); // get index of given key inside keys array

$prev_key = $keys_array[$get_search_value_key_from_value_array -1]; // get previous key based on searched key index

$next_key = $keys_array[$get_search_value_key_from_value_array+1];// get next key based on searched key index

echo "Current key = ".$search_key." and current value = ".$array_same_cat[$search_key]."\n";

echo "Next key = ".$next_key." and next value = ".$array_same_cat[$next_key]."\n";

echo "Prev key = ".$prev_key." and prev value = ".$array_same_cat[$prev_key]."\n";

Output:- https://eval.in/873723

PHP doesn't have a way of setting the array pointer to a specific key. For your use case, this would work:

//...some code where $array_same_cat is defined
$key = 'XA127';
$next = null;
$prev = null;
if(isset($array_same_cat[$key])){
    reset($array_same_cat); //reset the array pointer to the beginning
    //Iterate over the array, setting $key to the current array key
    while( ($key = key(current($array_same_cat))) !== null ){
        if($key == $search){
            prev($array_same_cat);
            $prev = array('key' => key($array_same_cat), 'value' => current($array_same_cat));

            next(next($array_same_cat));
            $next = array('key' => key($array_same_cat), 'value' => current($array_same_cat));
            break;
        } 
        next($array_same_cat);
    }
}

if( !empty($prev) ){
    echo "Previous element: key=" . $prev['key'] . ', value= ' . $prev['value'] . PHP_EOL;
}
if( isset($array_same_cat[$search] ){
    echo "current element: key=" . $search . ', value= ' . $array_same_cat[$search] . PHP_EOL;
}
if( !empty($next) ){
    echo "Next element: key=" . $next['key'] . ', value= ' . $next['value'] . PHP_EOL;
}

This code will output something like:

Previous element: key=SF124, value=value
Current element: key=XA127,  value=value2
Next element: key=AT257, value=value3

Note, there is likely a better way to structure your data so that you can avoid having to do something like this. The above method is not very efficient since it requires that you loop over the entire array until you find the key you're looking for.

A better approach might involve storing the next and previous values along with the current value as you loop through your posts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM