简体   繁体   中英

how to count spesific values in multidimensional array in codeigniter

this is my multidimensional array where i get it from 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.

PS all the restaurant i got it from the rating of score. 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?

PS i only show 13 multidimensional array, actually i put into 17 limit

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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