如何在多维PHP数组中获得next()?
我有一个如下所示的数组:如何在多维PHP数组中获得next()?,php,arrays,multidimensional-array,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
$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()函数完成这项工作。查找阵列中相邻元素的最简单方法是什么?(如果我在“艺术家”页面,我将如何链接到“射箭”和“羽毛球”)array yp\u next(string$domain,string$map,string$key)
返回指定键之后命名映射中的下一个键值对
听起来你需要的是javascript。它将能够在客户端切换产品,因此您可以存储for循环中的变量,当单击按钮时,您可以调用函数将其切换到下一个您可以遍历数组以查找当前键,然后再执行一个元素。一个经过测试的示例:
$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)。您不能在关联数组上执行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)
);
echo custom_array_pagination($products);
您仍然可以遍历此结构并从键“name”获取当前项的名称。基于数组数据,您可以使用以下函数。我使用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;
}
我想您需要实现某种分页并使用url重写。另外,你应该使用MySQL数据库。@PedroLobito绝对应该使用数据库,我完全同意,这就足够了,直到我们能够实现一个数据库驱动的解决方案。我不会花时间在临时解决方案上。你可以知道你需要什么,但你必须为此安装一个扩展:除非我误解了,这与NIS域有关,而不是OP需要的,即使安装了扩展。