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!
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);