How can I access an object attribute that starts with a number?
What about this :
$Beeblebrox->{'2ndhead'}
Actually, you can do this for pretty much any kind of variable -- even for ones that are not class properties.
For example, you could think about a variable's name that contains spaces ; the following syntax will work :
${"My test var"} = 10;
echo ${"My test var"};
Even if, obviously, you would not be able to do anything like this :
$My test var = 10;
echo $My test var;
No idea how it's working internally, though... And after a bit of searching, I cannot find anything about this in the PHP manual.
Only thing I can find about {}
and variables is in here : Variable parsing -- but not quite related to the current subject...
But here's an article that shows a couple of other possiblities, and goes farther than the examples I posted here : PHP Variable Names: Curly Brace Madness
And here's another one that gives some additionnal informations about the way those are parsed : PHP grammar notes
Javascript: access an object property whose name starts with a number
Use square brackets:
var usedStorage = result.accounts["4Sync"].usedStorage;
Property identifers can begin with a number, but member expressions with the .
character will only allow valid variable identifiers (since anything else is ambiguous). To get around this, you can use the square bracket syntax, which is equivalent but allows the use of any string.
If you're interested, here is the grammar:
MemberExpression :
PrimaryExpression
FunctionExpression
MemberExpression[
Expression]
MemberExpression.
IdentifierName
Notice how square brackets can contain any expression, but the .
can only be followed by an IdentifierName (basically, any valid identifier, plus reserved words in ES5).
Getting object properties that start with a number
You can use bracket access for properties that aren't valid JavaScript identifiers, that goes for property names with spaces or other language symbols like +
, *
window.alert(stats.LOAD_AVG["1_MIN"]);
You can use bracket access anywhere really
window.alert(stats["COUNTS"]["TOTAL"]);
Can I get a javascript object property name that starts with a number?
You can use the following syntax to do what you describe using bracket notation:
myObject["myProperty"]
Bracket notation differs from dot notation (e.g. myObject.myProperty
) in that it can be used to access properties whose names are illegal. Illegal meaning that with dot notation, you're limited to using property names that are alphanumeric (plus the underscore _
and dollar sign $
), and don't begin with a number. Bracket notation allows us to use a string to access a property and bypass this.
myObject.1 // fails, properties cannot begin with numbers
myObject.& // fails, properties must be alphanumeric (or $ or _)
myObject["1"] // succeeds
myObject["&"] // succeeds
This also means we can use string variables to look up and set properties on objects:
var myEdgyPropertyName = "||~~(_o__o_)~~||";
myEdgyObject[myEdgyPropertyName] = "who's there?";
myEdgyObject[myEdgyPropertyName] // "who's there?";
You can read more about dot and bracket notation here, on MDN.
How to access an PHP object when the key begins with a Number
Use braces and quotes: $myArray->{'24h_sel'};
.
How to access object property beginning with a number (SyntaxError: Unexpected identifier)
data.snow['3h'];
Properties accessed with dot notation can't begin with a number.
snow: Object {3h: 1.3}
could be refactored to snow: {3h: 1.3}
. It is redundant to type Object
.
Also, if you wrap your property names in quotes, you can use bizarre property names like:
var myObj = {
'^': 'foo'
};
console.log(myObj['^']);
but, I generally stick to more standard names that I can access with dot notation.
Javascript Object - Key beginning with number, allowed?
You can use any key if you use [string]
to access the key, even key with space. All of these are valid:
myObj['key with space']
myObj['12345']
But if you want to use dot .
operator to access the key then the key must be a valid identifier which means they can not begin with a number or contain space.
Object property name as number
You can reference the object's properties as you would an array and use either me[123]
or me["123"]
How to access object in php when the identifier starts with a @ symbol?
Keep element name in one variable like $test
and use $var->$test
to get value like this
<?php
$arr = json_decode(json_encode(["@id"=> "1EBEF5DA"]));
$obj = '@id';
print_r($arr->$obj);
?>
Demo : https://eval.in/844662
Or another way is print_r($arr->{'@id'});
Demo : https://eval.in/844662
How to access a numeric property?
This should work:
myObject["0"]
(myObject["propertyName"]
is an alternative syntax for myObject.propertyName
.)
You're getting the error because, in JavaScript, identifiers can't begin with a numeral. From the Variables page at the Mozilla Developer Centre:
A JavaScript identifier must start
with a letter, underscore (_), or
dollar sign ($); subsequent characters
can also be digits (0-9). Because
JavaScript is case sensitive, letters
include the characters "A" through "Z"
(uppercase) and the characters "a"
through "z" (lowercase).
Related Topics
Best Way to Get Files from a Dir Filtered by Certain Extension in PHP
Multiple JavaScript/CSS Files: Best Practices
How to Do Error Logging in Codeigniter (Php)
When to Use HTMLspecialchars() Function
Why Does the Laravel API Return a 419 Status Code on Post and Put Methods
Transpose 2D Array, Join Second Level with Commas, and Join First Level with Pipes
How to Run a PHP Script Using Windows Schedule Task
PHP Session Side-Effect Warning with Global Variables as a Source of Data
How to Use "Dependency Injection" in Simple PHP Functions, and Should I Bother
Get Custom Product Attributes in Woocommerce
Destroy or Unset Session When User Close the Browser Without Clicking on Logout
Special Characters in PHP/Mysql