Mufidz
Mufidz

Reputation: 5

PHP Foreach only get one data

i want to loop all the data i call but that runs only one data I use Codeigniter framework

Model:

$this->db->where('group_user', 'upt_perpus')->order_by('idmodul', 'DESC')->get('menu')->result_array();

Controller:

  $data = array(
     'about' => array(
        'sejarah' => 'Sejarah',
        'visi_misi' => 'Visi, Misi & Tujuan',
        'struktur' => 'Struktur Organisasi'
     ),
     'services' => array(),
     'peraturan' => array(
        'umum' => 'Umum',
        'khusus' => 'Khusus',
        'pelayanan_perpus' => 'Pelayanan Perpustakaan'
     )
  );
  $konten = $this->kontens->getPerpus();
  foreach ($konten as $key) {
     $data['services'] = array(
        $key['slug'] => $key['judul']
     );
  }
  return $data;

Upvotes: 0

Views: 716

Answers (1)

Barmar
Barmar

Reputation: 780724

If you want an associative array in $data['services'], you need to assign to a key of the array.

  foreach ($konten as $key) {
     $data['services'][$key['slug']] = $key['judul'];
  }

Upvotes: 1

Related Questions