你如何在多维数学PHP数组中得到next()?

时间:2015-10-03 16:59:42

标签: php arrays multidimensional-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"和" next"将链接到阵列中相邻产品的按钮。我的PHP技能很简陋,而且我试图用prev()和next()函数来完成这项任务是没有运气的。找到数组中相邻元素的最简单方法是什么? (如果我在"艺术家"页面上我将如何链接," Archery"和#34;羽毛球。")

5 个答案:

答案 0 :(得分:1)

array yp_next(string $ domain,string $ map,string $ key) 返回指定键后命名映射中的下一个键值对。

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

答案 1 :(得分:1)

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

$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(如果找不到当前项或我们在开头/结束时)数组,没有上一个/下一个可用)。

答案 2 :(得分:1)

根据您的阵列数据,您可以使用以下功能。我使用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);

答案 3 :(得分:0)

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

答案 4 :(得分:0)

您不能在关联数组上执行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)
);

您仍然可以遍历此结构并从键&#34;名称&#34;获取当前项目的名称。