简体   繁体   English

如何在Codeigniter中计算多维数组中的特殊值

[英]how to count spesific values in multidimensional array in codeigniter

this is my multidimensional array where i get it from var_dump : 这是我从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"
} 
}

this is my helper 这是我的帮手

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;
}

and this is my model : 这是我的模型:

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;

}

and this is my view for link the 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;?>

guys can you help me please? 你们可以帮我吗? i am trying to make a group of cuisines name like, if the cuisines name is breakfast, there is many restaurant have cuisines with breakfast too, i want in my view, the same cuisines name will not display again and again, so if there are 7 restaurant with breakfast, it will display only 1 breakfast, than if i click the breakfast, it will show the restaurant who has breakfast. 我正在尝试命名一组美食名称,例如,如果美食名称是早餐,那么有很多餐厅也提供含早餐的美食,我想在我看来,相同的美食名称不会一次又一次显示,因此如果7间含早餐的餐厅,它将仅显示1份早餐,而不是如果我单击早餐,它将显示谁在吃早餐的餐厅。

PS all the restaurant i got it from the rating of score. PS所有的餐厅,我从得分的评分。 and there is some problem to, when i refreshing my page the cuisines names changes like if deals at first, sometimes when i refresh it will be jump into 3 or last can u help me on there? 并且有一些问题,当我刷新页面时,菜式名称会发生​​变化,例如如果开始时会打折,有时当我刷新时,它会跳到3或最后一次,您能帮我吗?

PS i only show 13 multidimensional array, actually i put into 17 limit PS我只显示13个多维数组,实际上我放入了17个极限

thank you 谢谢

You're needing to create a new array structure so you can easily iterate over it. 您需要创建一个新的数组结构,以便可以轻松地对其进行迭代。

<?php
    $cuisine_stack = array();

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

This will produce all the results that share the same cuisine_name_1 in the same array. 这将产生在同一数组中共享相同cuisine_name_1所有结果。

For example 例如

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'
    )
    ....
  )
)

Then you can simply loop that and display the li , which given the current dummy data you will end up with 4 li's as such Deals, Italian Food, Breakfast, Western food 然后,您可以简单地循环并显示li ,在给出当前虚拟数据的情况下,您将最终获得4 li的优惠,例如Deals,意大利美食,早餐,西餐

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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