0

This could probably sound silly, but my question is about arrays and their syntax:

Isn't redundant to declare an array with this syntax?

$data[] = array(
    'ct_id' => $row->ct_id,
    'association' => $row->association_name,
    'designation' => $row->designation_name,
    'license_number' => $row->license_number,
    'license_date' => $row->license_date ? date("jS F, Y", strtotime($row->license_date)) : '',
    'date_added' => date("jS F, Y", strtotime($row->date_added))
);

Should the declaration of the array be sufficient to define an array?

This code happens in a foreach loop like that:

foreach ($this->something->result() as $row) {..}
2
  • 1
    $data[] means "add new value to array named $data". Commented Jun 12, 2018 at 17:56
  • 1
    Same as array_push($data, array('ct_id' => $row->ct_id, 'association' => $row->association_name, 'designation' => $row->designation_name, 'license_number' => $row->license_number, 'license_date' => $row->license_date ? date("jS F, Y", strtotime($row->license_date)) : '', 'date_added' => date("jS F, Y", strtotime($row->date_added)))); Commented Jun 12, 2018 at 18:00

2 Answers 2

4

There are two things going on here.

array(...)

is one syntax to define an array in PHP.

$data[] = ...

takes whatever is to the right of the equals sign and appends it to the array contained in $data.

So your result would look like:

$data => array(
    array(
        ...
    )
)
Sign up to request clarification or add additional context in comments.

Comments

0

You should declared array without 【】

If you want a new element to add in array use 【】 like below,

$data【"test key"】= "test value";

So array() is for array initialization and 【】is for add new element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.