Home > database >  concatenate 2 arrays obtained from loop in php
concatenate 2 arrays obtained from loop in php

Time:12-02

can you correction my code

$tongkol = $dataSurat->pilih_kepala;
$integerIDs = array_map('intval', explode(',', $tongkol));
for ($i = 0; $i < sizeof($integerIDs); $i  ) {
    $this->pegawai->groupStart();
    $this->pegawai->where("find_in_set($integerIDs[$i], id_pegawai)");

    $this->pegawai->groupEnd();
    $query = $this->pegawai->get()->getResult();

    var_dump($query);

i want result like this, 2 object inside 1 array

array(2) { 
    [0]=> object(stdClass)#105 (18) { ["id_pegawai"]=> string(1) "6" ["nip"]=> string(3) "123" ["nama"]=> string(13) "RENNY, SH., M" ["jenis_pegawai"]=> string(3) "PNS" ["id_golongan"]=> string(1) "4" ["id_pangkat"]=> string(1) "5" ["id_jabatan"]=> string(1) "3" ["jenis_kelamin"]=> string(6) "wanita" ["alamat"]=> string(7) "Jakarta" ["agama"]=> string(5) "Islam" ["tempat_lahir"]=> string(7) "Jakarta" ["tanggal_lahir"]=> string(10) "1984-02-20" ["no_hp"]=> string(12) "081200000000" ["email"]=> string(15) "[email protected]" ["pasfoto"]=> string(0) "" ["created_at"]=> string(10) "2021-11-22" ["updated_at"]=> string(10) "2021-11-28" ["slug"]=> string(0) "" } 
    [1]=> object(stdClass)#106 (18) { ["id_pegawai"]=> string(1) "7" ["nip"]=> string(3) "456" ["nama"]=> string(16) "MUHAMMAD SYAFI'I" ["jenis_pegawai"]=> string(7) "HONORER" ["id_golongan"]=> string(1) "4" ["id_pangkat"]=> string(1) "5" ["id_jabatan"]=> string(1) "3" ["jenis_kelamin"]=> string(4) "pria" ["alamat"]=> string(7) "Bandung" ["agama"]=> string(5) "Islam" ["tempat_lahir"]=> string(8) "Malaysia" ["tanggal_lahir"]=> string(10) "1960-10-31" ["no_hp"]=> string(5) "14045" ["email"]=> string(15) "[email protected]" ["pasfoto"]=> string(0) "" ["created_at"]=> string(10) "2021-11-22" ["updated_at"]=> string(10) "2021-11-28" ["slug"]=> string(0) "" } } 

but i got like this

array(1) { 
    [0]=> object(stdClass)#108 (18) { ["id_pegawai"]=> string(1) "6" ["nip"]=> string(3) "123" ["nama"]=> string(13) "RENNY, SH., M" ["jenis_pegawai"]=> string(3) "PNS" ["id_golongan"]=> string(1) "4" ["id_pangkat"]=> string(1) "5" ["id_jabatan"]=> string(1) "3" ["jenis_kelamin"]=> string(6) "wanita" ["alamat"]=> string(7) "Jakarta" ["agama"]=> string(5) "Islam" ["tempat_lahir"]=> string(7) "Jakarta" ["tanggal_lahir"]=> string(10) "1984-02-20" ["no_hp"]=> string(12) "081200000000" ["email"]=> string(15) "[email protected]" ["pasfoto"]=> string(0) "" ["created_at"]=> string(10) "2021-11-22" ["updated_at"]=> string(10) "2021-11-28" ["slug"]=> string(0) "" } } 
array(1) { 
    [0]=> object(stdClass)#110 (18) { ["id_pegawai"]=> string(1) "7" ["nip"]=> string(3) "456" ["nama"]=> string(16) "MUHAMMAD SYAFI'I" ["jenis_pegawai"]=> string(7) "HONORER" ["id_golongan"]=> string(1) "4" ["id_pangkat"]=> string(1) "5" ["id_jabatan"]=> string(1) "3" ["jenis_kelamin"]=> string(4) "pria" ["alamat"]=> string(7) "Bandung" ["agama"]=> string(5) "Islam" ["tempat_lahir"]=> string(8) "Malaysia" ["tanggal_lahir"]=> string(10) "1960-10-31" ["no_hp"]=> string(5) "14045" ["email"]=> string(15) "[email protected]" ["pasfoto"]=> string(0) "" ["created_at"]=> string(10) "2021-11-22" ["updated_at"]=> string(10) "2021-11-28" ["slug"]=> string(0) "" } }

CodePudding user response:

Try using array_merge

$result = array();
$tongkol = $dataSurat->pilih_kepala;
$integerIDs = array_map('intval', explode(',', $tongkol));
for ($i = 0; $i < sizeof($integerIDs); $i  ) {
    $this->pegawai->groupStart();
    $this->pegawai->where("find_in_set($integerIDs[$i], id_pegawai)");

    $this->pegawai->groupEnd();
    $result = array_merge($result, $this->pegawai->get()->getResult());

    var_dump($result);
  • Related