Inline object/array creation?

Yes I searched:

  • PHP.net docs (proof)
  • Stackoverflow
  • Google
  • Yahoo

And did not find the answer.

In JavaScript I create an array of 5 objects like so:

this.pay_types =
[
    {name:'Cash'},
    {name:'Mastercard'},
    {name:'Visa'},
    {name:'Discovery'},
    {name:'Debit'},
    ...
];

I can't find any help on how to do this in PHP without hundreds of lines of code and a for-loop lol. What am I missing here?

1 comments · 100% upvoted · other discussions

comments

[deleted]

[deleted]

angdev [OP]

I'm not sure I understand. I want to create an array of objects, not an object of arrays! The following code works but is fairly ugly:

$json->payments = [];
foreach (array('Cash','Mastercard','Visa','Discovery','Debit','COD','Gift Card','Account','Cheque') as $i)
{
    $x = new stdClass();
    $x->name = $i;
    $json->payments[] = $x;
}

And we only use PHP as a trampoline between JS and MySQL, we do not use PHP for any type of logic. I would get rid of PHP entirely if I could call MySQL directly from JS!