对不起,我在PHP中制作这个数组时遇到了麻烦
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
)
)
)
使它像这样
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
)
)
)
就像上面一样,但正如你所看到的,我不知道为什么它会停止并且缺少许多元素。请帮助我,我花了几个小时来解决这个问题,但它没有解决。
这里是我的代码
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;
谢谢
答案 0 :(得分:1)
这是array_map
的标准任务。假设您在$array
变量中有一个初始数组:
$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());
希望它有所帮助。