如何在多维PHP数组中获得next() ?


How do you get next() in a multidimentional PHP array?

我有一个像下面这样的数组:

$products = array();
$products["Archery"] = array(
    "name" => "Archery",
    "img" => "img/wire/100-Archery.jpg",
    "desc" => "Archer aiming to shoot",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products["Artist"] = array(
    "name" => "Artist",
    "img" => "img/wire/101-Artist.jpg",
    "desc" => "Artist with palette & easel",
    "prices" => array($price3,$price6),
    "paypal" => $paypal5,
    "sizes" => array($size1, $size2)
);
$products["Badminton"] = array(
    "name" => "Badminton",
    "img" => "img/wire/102-Badminton.jpg",
    "desc" => "About to hit bird above head",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products["Baseball-Bat-Stance"] = array(
    "name" => "BASEBALL -Bat - Stance",
    "img" => "img/wire/103a-Baseball-Stance.jpg",
    "desc" => "Waiting for pitch",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products["Baseball-Bat-Swing"] = array(
    "name" => "BASEBALL - Bat - Swing",
    "img" => "img/wire/103b-Baseball-Swing.jpg",
    "desc" => "Just hit ball",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);

我有一个从这个数组加载单个产品的页面,我试图使"prev"answers"next"按钮链接到数组中相邻的产品。我的PHP技能很初级,而且我也没能成功地用prev()和next()函数完成这个任务。找到数组中相邻元素最简单的方法是什么?(如果我在"艺术家"页面,我该如何链接到"射箭"answers"羽毛球"呢?)

数组yp_next(字符串$domain,字符串$map,字符串$key)返回指定键之后命名映射中的下一个键值对。

http://php.net/manual/en/function.yp-next.php

您可以遍历数组以找到当前键,然后再遍历一个元素。经过测试的示例:

$current_page = 'Artist'; // as an example
$prev = $next = false; // the keys you're trying to find
$last = false; // store the value of the last iteration in case the next element matches
// flag if we've found the current element so that we can store the key on the next iteration
// we can't just use $last here because if the element is found on the first iteration it'll still be false
$found = false;
foreach ($products as $key => $value) {
    // if we found the current key in the previous iteration
    if ($found) {
        $next = $key;
        break; // no need to continue
    }
    // this is the current key
    if ($key == $current_page) {
        $found = true;
        $prev = $last;
    }
    $last = $key; // store this iteration's key for possible use in the next iteration
}

在此脚本的末尾,$prev和$next将包含上一项/下一项的键,或者为false(如果没有找到当前项,或者我们在数组的最开始/末尾,并且没有prev/next可用)。

基于您的数组数据,您可以使用以下函数。我使用array_keys()函数创建了一个仅包含初始数组的键的数组,所有的工作都使用新数组完成。

function custom_array_pagination($data = array()) {
    $current_page = 'Baseball-Bat-Swing';    //$current_page = $_GET['product']; //Change to this when you have set up your final code
    $data_keys = array_keys($data);  //This is the array all the work is done
    $s = '';
    if (!in_array($current_page, $data_keys)) {  //If there is no such element as the $_GET element in the array
        return $s;
    }
    $is_first = false;
    $is_last = false;
    $found_prev = false;
    $found_next = false;
    $next_text = '';
    if ($current_page == $data_keys[0]) {
        $is_first = true;
    }
    if ($current_page == end($data_keys)) {
        $is_last = true;
    }
    $s .= '<ul class="pagination">';
    if ($is_first) {    //If it is the first element then show only text
        $s .= '<li class="first">First'.'</li>';
    } else {
        $s .= '<li class="first"><a href="'.$data_keys[0].'">First</a>'.'</li>';
    }
    foreach($data_keys as $key => $value) {
        if ($is_first && !$found_prev) {   //If it is the first element then show only text
            $found_prev = true;
            $s .= '<li class="prev">Prev'.'</li>';
        } else {
            if (!$found_prev) { //If prev has not been found yet
                $prev = $data_keys[array_search($current_page, $data_keys) - 1];
                $found_prev = true;
                $s .= '<li class="prev"><a href="'.$prev.'">Prev</a>'.'</li>';
            }
        }
        if ($current_page == $value) {
            $s .= '<li class="current">'.$data[$value]['name'].'</li>';
        } else {
            $s .= '<li class="current"><a href="'.$value.'">'.$data[$value]['name'].'</a>'.'</li>';
        }
        if ($is_last && !$found_next) {   //If it is the last element then show only text
            $found_next = true;
            $next_text = '<li class="next">Next'.'</li>';
        } else {
            if (!$found_next) { //If next has not been found yet
                if ($value == $data_keys[count($data_keys) - 1]) {    //If this value is the last value in the table
                    $found_next = true;
                    $next = $data_keys[array_search($current_page, $data_keys) + 1];
                    $next_text = '<li class="next"><a href="'.$next.'">Next</a>'.'</li>';
                }
            }
        }
    }
    $s .= $next_text;
    if ($is_last) { //If it is the last element then show only text
        $s .= '<li class="last">Last</li>';
    } else {
        $s .= '<li class="last"><a href="'.$data_keys[count($data_keys) - 1].'">Last</a>'.'</li>';
    }
    return $s;
}

你可以这样使用这个函数:

echo custom_array_pagination($products);

听起来你需要的是javascript。它将能够在客户端切换产品,因此您可以存储来自for循环的变量,当单击按钮时,您可以调用函数将其切换到下一个

不能对关联数组执行next()和prev()操作。你需要的是另一个数组结构,像这样:

$products = array();
$products[0] = array(
    "name" => "Archery",
    "img" => "img/wire/100-Archery.jpg",
    "desc" => "Archer aiming to shoot",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products[1] = array(
    "name" => "Artist",
    "img" => "img/wire/101-Artist.jpg",
    "desc" => "Artist with palette & easel",
    "prices" => array($price3,$price6),
    "paypal" => $paypal5,
    "sizes" => array($size1, $size2)
);
$products[2] = array(
    "name" => "Badminton",
    "img" => "img/wire/102-Badminton.jpg",
    "desc" => "About to hit bird above head",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products[3] = array(
    "name" => "BASEBALL -Bat - Stance",
    "img" => "img/wire/103a-Baseball-Stance.jpg",
    "desc" => "Waiting for pitch",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);
$products[4] = array(
    "name" => "BASEBALL - Bat - Swing",
    "img" => "img/wire/103b-Baseball-Swing.jpg",
    "desc" => "Just hit ball",
    "prices" => array($price1,$price3),
    "paypal" => $paypal2,
    "sizes" => array($size1, $size2)
);

您仍然可以遍历该结构并从键"name"获得当前项的名称。