A
A
Andrew2011-09-18 23:53:07
PHP
Andrew, 2011-09-18 23:53:07

Simple declaration of data structures in php

Good afternoon!
A question for those who know and understand. Maybe some of you are familiar with js and know how easy it is to declare arrays, objects and fill them with data. So the question is ripening - is there a similar possibility for php? There is an idea with json_decode(), but it's way too slow :(

var _a={a:{a:'a', b:'b', c:'c'}, b:[1, 2, 3, 4]}

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Sergey Savostin, 2011-09-19
@savostin

Sorry, maybe I didn't understand the question:

$MyObject = array(
'one' => 
   array (
      'some' => 12, 
      'other' => 13.5, 
      'another' => true, 
      'complex' => array (1,2,3,4,5), 
      'simple' => 'value',
      'object' => new MyObjectType(42)
      ),
'two' => 
   array (
      'ITthinkYouGotIt' => 'right?'
      )
   );

S
Sergey Beresnev, 2011-09-19
@sectus

Something like this appeared in 5.4k
. New Syntax
- Short array syntax in 5.4.0
$a = [1, 2, 3, 4];
$a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
$a = ['one' => 1, 2, 'three' => 3, 4];

R
rakot, 2011-09-19
@rakot

I don't see any problem. Use array('key'=>'value'), for objects you can use transformation (object)array('key'=>'value'). Yes, write a little more, but not critical.

A
Alexey Lebedev, 2011-09-19
@swanrnd

var myArr = new Array();
myArr ["str1"] = "1111";
myArr ["str2"] = "2222";
myArr .push("33");

Practice. JS is a pretty simple language.

A
Andrew, 2011-09-19
@xmdy

Comrades, if you do not read the question and look at the keywords, please do not answer!
agree that writing {a:{a:'a', b:'b', c:'c'}, b:[1, 2, 3, 4]} is much simpler and faster than all the options you suggested above.

M
Mikhail Osher, 2011-09-20
@miraage

json_decode() has not been canceled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question