php在数组中使用foreach

时间:2016-07-11 06:50:42

标签: php arrays loops

我想在数组中使用foreach,而这个foreach使用数组。代码在这里:

$records["data"][] = array(
  $id,
$value['name'],
foreach($kichen_organ as $x => $x_value){  if($value['organ']==$x) '<span class="badge badge-success">'echo $x_value;'</span>' },
$value['type'] ,
$value['name_responsible'] ,
$value['family_responsible'] ,
$value['office'],
$value['fax'],
$value['mobile']);

$ kichen_organ是另一个数组,我通过重命名$ x和$ s_value来解析它的键和值 但我收到了这个错误:

syntax error, unexpected 'foreach' (T_FOREACH), expecting ')' in /var/www/html/...

在数组中使用循环的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

$temp = [];

foreach($kichen_organ as $x => $x_value) {
    if ($value['organ'] == $x) {
        $temp[] = '<span class="badge badge-success">'.$x_value.'</span>';
    };
}

$records["data"][] = array_merge($temp,
    [
        $id,
        $value['name'],
        $value['type'] ,
        $value['name_responsible'] ,
        $value['family_responsible'] ,
        $value['office'],
        $value['fax'],
        $value['mobile']
    ]);