user756659
user756659

Reputation: 3512

php count of array of arrays - different value than expected

I am having a brain fart on this one because I can't figure out how to get this count to return as expected :

$ajax_result['data'] = array('ID' => 0, 'Timestamp' => $_POST['end_date'] * 1000, 'Total' => 0);

echo count($ajax_result['data']);

The above returns 3 - I am expecting it to return 1 or would like it to (1 array in the array). $ajax_result['data'] can possibly contain many arrays and that is the total I need to get.

The format of this needs to stay the same because these results are used by a plugin that needs them in this format.

Upvotes: 1

Views: 37

Answers (1)

Nick
Nick

Reputation: 147286

To get your desired result (and probably to have your code work correctly anyway), you need to make $ajax_result['data'] a multi-dimensional array:

$ajax_result['data'] = array(array('ID' => 0, 'Timestamp' => $_POST['end_date'] * 1000, 'Total' => 0));

count($ajax_result['data']) will now return 1.

Then you can push new values into the array e.g.:

$ajax_result['data'][] = array('ID' => 1, 'Timestamp' => $_POST['end_date'] * 1000, 'Total' => 2);

Demo on 3v4l.org

Upvotes: 1

Related Questions