如何在codeigniter中计算多维数组中的特定值

时间:2016-06-10 02:03:31

标签: php arrays codeigniter multidimensional-array

这是我的多维数组,我从var_dump得到它:

array(17) {
[0]=>
array(4) {
["restaurant_id"]=>
string(3) "246"
["restaurant_name"]=>
string(7) "gresto5"
["cuisines_id"]=>
string(1) "5"
["cuisines_name_1"]=>
string(5) "Deals"
}
[1]=>
array(4) {
["restaurant_id"]=>
string(3) "177"
["restaurant_name"]=>
string(10) "Mamma Rosy"
["cuisines_id"]=>
string(2) "10"
["cuisines_name_1"]=>
string(12) "Italian Food"
}
[2]=>
array(4) {
["restaurant_id"]=>
string(3) "250"
["restaurant_name"]=>
string(8) "Yukihira"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[3]=>
array(4) {
["restaurant_id"]=>
string(3) "285"
["restaurant_name"]=>
string(3) "123"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[4]=>
array(4) {
["restaurant_id"]=>
string(3) "194"
["restaurant_name"]=>
string(17) "Batagor Cuplis   "
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[5]=>
array(4) {
["restaurant_id"]=>
string(3) "283"
["restaurant_name"]=>
string(3) "123"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[6]=>
array(4) {
["restaurant_id"]=>
string(3) "282"
["restaurant_name"]=>
string(3) "123"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[7]=>
array(4) {
["restaurant_id"]=>
string(3) "243"
["restaurant_name"]=>
string(7) "gresto1"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[8]=>
array(4) {
["restaurant_id"]=>
string(3) "161"
["restaurant_name"]=>
string(8) "Pepenero"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[9]=>
array(4) {
["restaurant_id"]=>
string(3) "248"
["restaurant_name"]=>
string(8) "Paparoti"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[10]=>
array(4) {
["restaurant_id"]=>
string(3) "182"
["restaurant_name"]=>
string(11) "Doner Kebab"
["cuisines_id"]=>
string(2) "25"
["cuisines_name_1"]=>
string(12) "Western food"
}
[11]=>
array(4) {
["restaurant_id"]=>
string(3) "284"
["restaurant_name"]=>
string(3) "123"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[12]=>
array(4) {
["restaurant_id"]=>
string(3) "286"
["restaurant_name"]=>
string(3) "123"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
}
[13]=>
array(4) {
["restaurant_id"]=>
string(3) "249"
["restaurant_name"]=>
string(9) "Cita Rasa"
["cuisines_id"]=>
string(1) "1"
["cuisines_name_1"]=>
string(9) "Breakfast"
} 
}

这是我的帮手

function cuisines_popular(){
$CI=& get_instance();
$CI->load->model('/ADMIN/Restaurants_m');
$popular_cuisines = $CI->Restaurants_m->m_get_popular_cuisines(17);shuffle($popular_cuisines);
foreach($popular_cuisines as $test => $rows){

}
return $popular_cuisines;
}

这是我的模特:

function m_get_popular_cuisines($num){
    $this->db->select("t1.restaurant_id,t1.restaurant_name,t2.cuisines_id, t3.cuisines_name_1");
    $this->db->order_by("score","desc");
    $this->db->limit($num);
    $this->db->from("uhd_restaurant as t1");
    $this->db->join("uhd_restaurant_cuisines as t2","t2.restaurant_id = t1.restaurant_id");
    $this->db->join("uhd_cuisines as t3","t2.cuisines_id = t3.cuisines_id");
    $this->db->group_by("restaurant_id");
    $query = $this->db->get()->result_array();
    return $query;

}

这是我链接cuisines_name

的观点
<ul class="unstyled">
                <?php foreach($popular_cuisines as $row => $results):?>
                <li>
                    <a href='/restaurants/cuisines/<?=$results["cuisines_name_1"]?>'><?=$results["cuisines_name_1"]?></a>
                </li>
                <?php endforeach;?>
你可以帮助我吗?我正在尝试制作一组​​菜肴名称,如果菜肴名称是早餐,有很多餐厅也有早餐菜肴,我想在我看来,同样的菜肴名称不会一次又一次地显示,所以如果有的话7间餐厅提供早餐,仅显示1份早餐,如果点击早餐,则会显示有早餐的餐厅。

P.S所有的餐厅我从得分的评分中得到了它。并且有一些问题,当我刷新我的页面时,菜肴名称的变化就像最初的交易一样,有时当我刷新它会跳到3或最后可以帮助我吗?

P.S我只显示13个多维数组,实际上我放入了17个限制

谢谢

1 个答案:

答案 0 :(得分:0)

您需要创建一个新的数组结构,以便轻松迭代它。

<?php
    $cuisine_stack = array();

    foreach($cuisines as $shop) {
        $cuisine_stack[trim($shop['cuisines_name_1'])][] = $shop;
    }
?>

这将生成在同一数组中共享相同cuisine_name_1的所有结果。

例如

array(
  'Deals' => array(
    array(
      'restaurant_id' => '246',
      'restaurant_name' => 'gresto5',
      'cuisines_id' => '5',
      'cuisines_name_1' => 'Deals'
    )
  'Italian Food' => array(
    array(
      'restaurant_id' => '177',
      'restaurant_name' => 'Mamma Rosy',
      'cuisines_id' => '10',
      'cuisines_name_1' => 'Italian Food'
  )
  'Breakfast' => array(
    array(
      'restaurant_id' => '250',
      'restaurant_name' => 'Yukihira',
      'cuisines_id' => '1',
      'cuisines_name_1' => 'Breakfast'
    ),
    array(
      'restaurant_id' => '285',
      'restaurant_name' => '123',
      'cuisines_id' => '1',
      'cuisines_name_1' => 'Breakfast'
    )
    ....
  )
)

然后你可以简单地循环并显示li,其中给出了当前的虚拟数据,你将最终得到4个li,因为特惠,意大利食品,早餐,西餐 < / p>

<ul class="unstyled">
    <?php foreach($cuisine_stack as $cuisine => $values): ?>
        <li>
            <a href='/restaurants/cuisines/<?php echo urlencode($cuisine); ?>'><?php echo $cuisine; ?></a>
        </li>
    <?php endforeach;?>
</ul>