Tolong pilih kategori sesuai, jenis posting (pertanyaan atau bukan) dan sertakan tag/topik yang sesuai misal komputer, php, mysql, dll.
Promosi atau posting tidak pada tempatnya akan kami hapus.
Klik link berikut untuk informasi cara menyisipkan kode program supaya tampil rapi dan terformat dengan baik di diskusiweb.com: http://www.diskusiweb.com/discussion/50415/cara-menyisipkan-kode-program-di-diskusiweb-com

Baca cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

(SOLVED) Gabung 2 array dengan 1 ID yang sama

Siang master,
Saya mau nanya untuk masalah penggabungan array.

Array 1 :
Array (
[0] => stdClass Object ( [id] => 30 [nik] => 436 )
[1] => stdClass Object ( [id] => 31 [nik] => 547 )
)

Array 2 :
Array (
[0] => stdClass Object ( [id] => 35 [id_struktur_gaji] => 30 [tgl] => 2017-03-16 [ot] => 04:00:00 )
[1] => stdClass Object ( [id] => 33 [id_struktur_gaji] => 30 [tgl] => 2017-03-18 [ot] => 08:00:00 )
[2] => stdClass Object ( [id] => 37 [id_struktur_gaji] => 30 [tgl] => 2017-03-20 [ot] => 02:00:00 )
[3] => stdClass Object ( [id] => 39 [id_struktur_gaji] => 31 [tgl] => 2017-03-20 [ot] => 02:00:00 )
[4] => stdClass Object ( [id] => 36 [id_struktur_gaji] => 30 [tgl] => 2017-03-21 [ot] => 02:00:00 )
[5] => stdClass Object ( [id] => 34 [id_struktur_gaji] => 30 [tgl] => 2017-03-24 [ot] => 05:00:00 )
[6] => stdClass Object ( [id] => 32 [id_struktur_gaji] => 30 [tgl] => 2017-03-25 [ot] => 12:00:00 )
[7] => stdClass Object ( [id] => 38 [id_struktur_gaji] => 30 [tgl] => 2017-03-26 [ot] => 13:00:00 )
)

id_struktur_gaji di array 2 saling terkait dengan id di array 1

Hasil akhir yang saya mau :
Array (
[0] => stdClass Object ( [id] => 30 [nik] => 436 )
array(
([0] => [id] => 35 [id_struktur_gaji] => 30 [tgl] => 2017-03-16 [lama_ot] => 04:00:00)
([1] => [id] => 33 [id_struktur_gaji] => 30 [tgl] => 2017-03-18 [lama_ot] => 08:00:00)
([2] => [id] => 37 [id_struktur_gaji] => 30 [tgl] => 2017-03-20 [lama_ot] => 02:00:00)
([3] => [id] => 36 [id_struktur_gaji] => 30 [tgl] => 2017-03-21 [lama_ot] => 02:00:00)
([4] => [id] => 34 [id_struktur_gaji] => 30 [tgl] => 2017-03-24 [lama_ot] => 05:00:00)
([5] => [id] => 32 [id_struktur_gaji] => 30 [tgl] => 2017-03-25 [lama_ot] => 12:00:00)
([6] => [id] => 38 [id_struktur_gaji] => 30 [tgl] => 2017-03-26 [lama_ot] => 13:00:00)
)
[1] => stdClass Object ( [id] => 31 [nik] => 547 )
array(
([0] => [id] => 39 [id_struktur_gaji] => 31 [tgl] => 2017-03-20 [lama_ot] => 02:00:00)
)

)

Minta bantuannya master o:)

Comments

  • @uyung coba copas test di sini
    <?php
    $first = array(
    array("id" => "30","nik" => "436"),
    array("id" => "31","nik" => "547"),
    );
     
    $second = array(
    array("id" => "35","id_struktur_gaji" => "30","tgl" => "2017-03-16","ot" => "04:00:00"),
    array("id" => "33","id_struktur_gaji" => "30","tgl" => "2017-03-18","ot" => "08:00:00"),
    array("id" => "37","id_struktur_gaji" => "30","tgl" => "2017-03-20","ot" => "02:00:00"),
    array("id" => "37","id_struktur_gaji" => "30","tgl" => "2017-03-20","ot" => "02:00:00"),
    array("id" => "39","id_struktur_gaji" => "31","tgl" => "2017-03-21","ot" => "02:00:00"),
    );
     
    $third = array();
     
    foreach($first as $a)
    {
    $array = array();
    foreach($second as $z)
    {
    if($a['id'] == $z["id_struktur_gaji"])
    {
    $array[] = $z;
    }
    }
    $xyz = $a;
    array_push($xyz,$array);
    $thrid[]=$xyz;
    }
    var_dump($thrid);exit;


  • saya cetak di browser ada warning @blackarch ,
    A PHP Error was encountered

    Severity: Warning
    Message: array_push() expects parameter 1 to be array, object given
    Filename: views/report_payroll_pdf_slip.php
    Line Number: 54


    $third = array();
    foreach($lap_pdf as $a)
    {
    $array = array();
    foreach($lap_pdf_ot as $z)
    {
    if($a->id == $z->id_struktur_gaji)
    {
    $array[] = $z;
    }
    }
    $xyz = $a;
    array_push($xyz,$array);
    $thrid[]=$xyz;
    }

    yg warning di line ini:
    array_push($xyz,$array);
  • @uyung Apakah ini digunakan di dalam function? jika iya harus di deklarasi global,
    kalo ngga coba ganti array_push($xyz,$array); jadi $xyz[] = $array;
  • Array saya jadi begini mas :
    Array (
    [0] => Array (
    [0] => Array ( [id] => 30 [nik] => 436 )
    [1] => Array (
    ( [0] => Array ( [id] => 35 [id_struktur_gaji] => 30 [tgl] => 2017-03-16 [ot] => 04:00:00 )
    [1] => Array ( [id] => 33 [id_struktur_gaji] => 30 [tgl] => 2017-03-18 [ot] => 08:00:00 )
    [2] => Array ( [id] => 37 [id_struktur_gaji] => 30 [tgl] => 2017-03-20 [ot] => 02:00:00 )
    [3] => Array ( [id] => 37 [id_struktur_gaji] => 30 [tgl] => 2017-03-20 [ot] => 02:00:00 ) ) ) )
    [1] => Array (
    [0] => Array ( [id] => 31 [nik] => 547 )
    [1] => Array (
    ( [0] => Array ( [id] => 39 [id_struktur_gaji] => 31 [tgl] => 2017-03-21 [ot] => 02:00:00 ) ) ) )
    )

    <?php
    $third = array();
    foreach($lap_pdf as $a)
    {
    $array = array();
    foreach($lap_pdf_ot as $z)
    {
    if($a->id == $z->id_struktur_gaji)
    {
    $array[] = $z;
    }
    }
    $xyz[] = $a;
    $xyz[] = $array;
    $thrid[]=$xyz;
    }
    ?>
  • Saya pake funtion mas.
  • edited April 2017
    @uyung walah kenapa jd $xyz[] = $a; ?
    <?php
    $third = array();
    foreach($lap_pdf as $a)
    {
    $array = array();
    foreach($lap_pdf_ot as $z)
    {
    if($a->id == $z->id_struktur_gaji)
    {
    $array[] = $z;
    }
    }
    $xyz = $a;
    $xyz[] = $array;
    $thrid[]=$xyz;
    }
    ?>
  • ia mas, ok uda fix. Makasih yaa... o:)
    Kalau bisa bantuin lagi di postingan sy yg ini, lagi bingung mas :#
    diskusiweb.com/discussion/49833/hitung-value-append
Sign In or Register to comment.