Search code examples
phparraysmultidimensional-arraytranspose

Transpose Multidimensional Array


I'm having trouble transposing this array:

  Array
(
    [0] => Array
        (
            [hari] => Array
                (
                    [0] => senin
                    [1] => senin
                    [2] => selasa
                    [3] => selasa
                    [4] => rabu
                    [5] => rabu
                )

            [mata_pelajaran] => Array
                (
                    [0] => Bahasa Orang Pluto
                    [1] => Bahasa Orang Pluto
                    [2] => Bahasa Orang Pluto
                    [3] => Bahasa Orang Pluto
                    [4] => Bahasa Orang Pluto
                    [5] => Bahasa Orang Pluto
                )

            [jam] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                    [4] => 2
                    [5] => 3
                )

        )

    [1] => Array
        (
            [hari] => Array
                (
                    [0] => senin
                    [1] => senin
                    [2] => senin
                    [3] => selasa
                    [4] => selasa
                    [5] => jumat
                    [6] => jumat
                    [7] => jumat
                )

            [mata_pelajaran] => Array
                (
                    [0] => Antropologi
                    [1] => Antropologi
                    [2] => Antropologi
                    [3] => Antropologi
                    [4] => Antropologi
                    [5] => Antropologi
                    [6] => Antropologi
                    [7] => Antropologi
                )

            [jam] => Array
                (
                    [0] => 1
                    [1] => 5
                    [2] => 6
                    [3] => 2
                    [4] => 3
                    [5] => 2
                    [6] => 3
                    [7] => 4
                )

        )

)

To make it like this

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id_jadwal] => 1
                    [hari] => senin
                    [jam] => 1
                    [mata_pelajaran] => Bahasa Orang Pluto
                )

        )
[1] => Array
    (
        [0] => Array
            (
                [id_jadwal] => 1
                [hari] => senin
                [jam] => 2
                [mata_pelajaran] => Bahasa Orang Pluto
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id_jadwal] => 1
                [hari] => selasa
                [jam] => 3
                [mata_pelajaran] => Bahasa Orang Pluto
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [id_jadwal] => 1
                [hari] => selasa
                [jam] => 2
                [mata_pelajaran] => Antropologi
            )

    )

[4] => Array
    (
        [0] => Array
            (
                [id_jadwal] => 1
                [hari] => selasa
                [jam] => 3
                [mata_pelajaran] => Antropologi
            )

    )

[5] => Array
    (
        [0] => Array
            (
                [id_jadwal] => 1
                [hari] => jumat
                [jam] => 2
                [mata_pelajaran] => Antropologi
            )

    )

)

just like above but as you see, i don't know why it just stop and many element missing. Please help me i have spent hours to fix this but it's not resolved .

here my code

foreach ($ketersediaan as $key => $value) {
    $detail = $this->ketersediaan->getKetersediaanmengajarDetailByHeader($value->id)->result();

    foreach ($detail as $d) {
        $dataguru[$value->guru]['hari'][] = $d->hari;
        $dataguru[$value->guru]['jam'][]  = $d->jam;
    }
}

$i = 0;
$mp = '';

foreach ($dataguru as $k => $d) {  
    foreach ($d['hari'] as $v) {
        $final[$i]['hari'][]  = $v;
    }

    for ($y = 0; $y <count($d['hari']); $y++) {
        $mp = $this->guru->getPelajaranByGuru($k);
        $final[$i]['mata_pelajaran'][]  = $mp->nama;
    }

    foreach ($d['jam'] as $z) {
        $final[$i]['jam'][]  = $z;
    }

    $i++;
}

//print_debug($final);

$count = 0;
foreach ($final as $y=>$x) { 
    foreach ($x as $h) {
        $jadwal[] = array(array('id_jadwal' => $id_jadwal, 'hari' => $x['hari'][$count], 'jam' => $x['jam'][$count], 'mata_pelajaran' => $x['mata_pelajaran'][$count]));
        $count++;
    } 
}

$jadwal_detail = array();
$count = 0;
$count2 = 0;

echo "<pre>";
print_r($jadwal);
exit;

Thank you


Solution

  • This is the standard task for an array_map. Assuming you have an initial array in $array variable:

    $result = array_reduce(array_map(function($elem) {
      $a = array();
      foreach($elem['hari'] as $k => $v) {
        $a[] = array(
          'id_jadwal'      => 1,
          'hari'           => $v,
          'jam'            => $elem['jam'][$k],
          'mata_pelajaran' => $elem['mata_pelajaran'][$k]
        );
      }
      return $a;
    }, $array), function($memo, $item) {
      return $memo = array_merge($memo, $item);
    }, array());
    

    Hope it helps.