douqujin2767 2014-06-06 00:48
浏览 62

数组中的foreach不起作用,如何将值添加到数组?

I have this complicated array.

    <?php

require_once('core/connect.php');
require_once('core/database.class.php');
require_once('core/controller.class.php');
require_once('core/settings.class.php');
$database   = new Database($db);
$controller = new Controller($db);
$settings   = new Settings($db);

$database->selectAll('SELECT * FROM bookings_calendar');
$result = $database->fetchAll();


$count = 0;
$arr = array();
foreach($result as $row)
{
    $arr['booking_date'][$count] = $row['booking_date']; 
    $arr['new'][$count]          = $row['new']; 
    $arr['completed'][$count]    = $row['completed']; 
    $arr['accepted'][$count]     = $row['accepted']; 
    $arr['cancelled'][$count]    = $row['cancelled']; 
    $count++;
}


    header("content-type: application/json"); 
    $year = date('Y');
    $month = date('m');

    echo json_encode(array(

        array(
            'id' => 111,
            'title' => $arr['new'][0] . ' new',
            'start' => $arr['booking_date'][0],
            'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][0]),
            'color' => '#F7F8E0',
            'textColor' => 'black'
        ),

        array(
            'id' => 111,
            'title' => $arr['new'][1] . ' new',
            'start' => $arr['booking_date'][1],
            'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][1]),
            'color' => '#F7F8E0',
            'textColor' => 'black'
        ),

        array(
            'id' => 111,
            'title' => $arr['new'][2] . ' new',
            'start' => $arr['booking_date'][2],
            'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][2]),
            'color' => '#F7F8E0',
            'textColor' => 'black'
        ),

    ));



?>

As you can see i can only put values manually by changing index, however i'd like to put all elements into that array automatically, but unfortunately i cannot use a foreach loop within an array. And my php skills are not that good, so im searching for some help.

Any help really appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • donglinxin8765 2014-06-06 01:10
    关注

    Get rid of the foreach and use a for loop.

    $arr = array();
    for($i=0; $i < count($result); $i++) {
        $arr[$i]['title'] = $result[$i]['new'] . ' new';
        $arr[$i]['whatever'] = $result[$i]['whatever'];
    }
    
    return json_encode($arr);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数