Reputation: 7599
I'm wondering if it's possible to define an array of parameters (sorry dunno how it's called) like in jQuery also in PHP:
{myCaption: 'bla', myValue: 123 }
Upvotes: 1
Views: 218
Reputation: 21969
The snippet you posted is an object literal (as named a literal form of defining an object), this can also be done more explicitly in Javascript and as mentioned before, jQuery is a framework/library, and not a language.
To define the same array in PHP you would do:
$array = array('myCaption' => 'bla', 'myValue' => 123);
An object literal is almost directly conforment to JSON and as such which minor modifications can be passed through to the json_decode
method, allowing you get an array out of this.
It works like this (modifications included):
$js_str = '{"myCaption": "bla", "myValue": "123"}';
$obj = json_decode($js_str, true);
You can also go back the other way like so:
$array = array('myCaption' => 'bla', 'myValue' => 123);
$js_str = json_encode($array);
Upvotes: 0
Reputation: 5694
You want to create an object? Easy:
$obj = array(
'key' => 'someValue',
'anotherKey' => 23
);
Is 'equivalent' to:
var obj = {
key: 'someValue',
anotherKey: 23
};
Note however, you are refering to 'as in jQuery'. Don't forget jQuery is just a library!
You could also create class in PHP which holds your values:
class A
{
public $myValue;
public function __construct($myValue)
{
$this->myValue = $myValue;
}
}
$obj = new A(23);
echo $obj->myValue; // 23
See the following link for some more information on classes.
Upvotes: 1
Reputation: 39389
You have two options: an associative array or an object. A lot of the previous examples show you have to create an array:
$obj = array(
'key' => 'someValue',
'foo' => 'bar',
'baz' => 'somethingElse'
);
Or you can create a true object:
$obj = new stdClass();
$obj->key = 'someValue';
$obj->foo = 'bar';
$obj->baz = 'somethingElse';
Upvotes: 0
Reputation: 1022
I think you'll get the same result using in PHP:
$A=array ("myCaption"=>"bla","myValue"=>123);
http://php.net/manual/en/language.types.array.php
you could always take objects in PHP and turn them into JSON
http://php.net/manual/en/function.json-encode.php
and from JSON into PHP with:
http://php.net/manual/en/function.json-decode.php
Upvotes: 3
Reputation: 57268
Yes and no, in Javascript this is called a object literal, now it can be created within php but it cannot be used like an array, you can create a object literal using json_encode
, for example:
//Create an array
$json = array(
"myCaption" => 'bla',
"myValue" => 123
);
//Convert it to a PHP Object
$data = (object)$data;
//Get the results in an json stricg:
$result = json_encode($data);
echo $result;
this should return something along the lines of: {myCaption: 'bla', myValue: 123 }
, it amy wrapped in an array thought, not 100% sure but you may get [{myCaption: 'bla', myValue: 123 }]
instead.
Upvotes: 0
Reputation: 537
Theoretically you can. It's something like JSON syntax so you could do something like that:
var_dump(json_decode("{'myCaption': 'bla', 'myValue': 123 }", true));
But that's ugly way :)
Upvotes: 0
Reputation: 943216
In JavaScript, it is called an object. jQuery is just a JS library, it isn't a language.
The PHP equivalent is an associative array.
$foo = array( "myCaption" => 'bla', "myValue" => 123 );
Upvotes: 0