0

Good Day,

im just confused as to why my data structure doesnt render array, its displays list of objects instead, the code below aims to store days of months on array structure,

$calendar = [];
for ($m=1; $m<=12; $m++) {
    $s=date ("w", mktime (0,0,0,$month,1,$year));

    for ($ds=1;$ds<=$s;$ds++) {

     $row_day = new stdClass();
     $row_day->day = $d;
     $row_day->is_holiday = $is_holiday;
     $row_day->is_paydate = $is_paydate;
     //day of the week
     $row_day->dotw = date("w",mktime (0,0,0,$month,$d,$year));
     $row_day->is_funds_due = $is_funds_due;

    $calendar[$m][] = $row_day;

    }

}

echo json_encode(array('type'=>2,'data'=>$calendar));

but when I console.log on my js it produces this structure.. which clearly is not an array.. enter image description here

am I wrong on building my structure?

its just that its easier to manipulate if its on array mode. since you can just use data.length, but in my case it doesnt work..

thanks for reading, Have a good Day!

melvnberd
  • 3,093
  • 6
  • 32
  • 69
  • ahh just for type identification on response so I could format accordingly – melvnberd Oct 10 '17 at 02:19
  • I'm not a PHP guy, but can't you just use `json_encode($calendar)`? You seem to be calling `array()` with arguments that would create an object rather than an array. – nnnnnn Oct 10 '17 at 02:20
  • I actually need to add parameters on my returned value for additional conditions.. I really think that what I need is possible its just that I have no idea yet on how to achieve this.. thanks for passing by anyways.. – melvnberd Oct 10 '17 at 02:35

2 Answers2

1

stdClass() is an object, not an array.

There is an easy (lazy?) way to take your object and turn it into an array found here

The other option would be to instantiate your data as an array in the first place.

$row_day['day'] = $d;
$row_day['is_holiday'] = $is_holiday;
$row_day['is_paydate'] = $is_paydate;
Luke
  • 640
  • 3
  • 10
1

use array Instead of std class (it's an object)

$calendar = [];
for ($m=1; $m<=12; $m++) {
    $s=date ("w", mktime (0,0,0,$month,1,$year));

    for ($ds=1;$ds<=$s;$ds++) {
        $row_day = array();
        $row_day['day'] = $d;
        $row_day['is_holiday'] = $is_holiday;
        $row_day['is_paydate'] = $is_paydate;
        //day of the week
        $row_day['dotw'] = date("w",mktime (0,0,0,$month,$d,$year));
        $row_day['is_funds_due'] = $is_funds_due;

        $calendar[$m] = $row_day;
    }
}

echo json_encode(array('type'=>2,'data'=>$calendar));
Sivaraj S
  • 340
  • 3
  • 14