How to Access This Object Property With an Illegal Name

How do I access this object property with an illegal name?

<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);

So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way:

echo $x->{'todo-list'};

Another possibility:

$todolist = 'todo-list';
echo $x->$todolist;

If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.

public function toArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof StdClass) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}

How to access object property with invalid characters

In JavaScript, object properties can be accessed by dot notation or bracket notation. Dot notation is often cleaner, but has restrictions. As you have noticed, your property contains an invalid character and therefore can't be accessed via dot notation. The solution, then, is to access the property using bracket notation like this: total['ga:newVisits'] so that your complete code will be {{total['ga:newVisits']}}. Live demo here (click).

Another nice feature about bracket notation is that it allows you to use a variable name as a property:

var myObj {
bar: '123'
};
var foo = 'bar';

console.log(myObj[foo]); //logs '123'

How to access object property when property name contains - (hyphen)

This is what you need:

$myobject->{'myweird-name'};

Referencing an object property that has dashes in the name

Your approach with the curly braces wasn't too wrong, but you need a string between the braces:

var_dump($child->{'item--1'});

Access dynamic property with / in name

Since it's an ExpandoObject, you should be able to get properties with an indexer like you would an IDictionary:

if(record["Town/Area"] == "foo") 

Barring that, you can cast it to an IDictionary, and treat it as such.

((IDictionary<String, Object>)record)

Then use linq for even more overengineered goodness:

if( record.First(kvp => kvp.Key == "City/Town").Value == foo ) 

How to create an object in javascript with illegal key name?

I've got a few spare minutes. So, code-review hat goes on.

var data = new Array();
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah;
data['val-name'] = 'blah;

Firstly, I think you have some typographic errors in this code. The last two values have oddly paired quotes.

var data = new Array();
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah';
data['val-name'] = 'blah';

Next, at the moment, you're assigning keys to an array. Which probably isn't what you mean (summary of the issue here; short version is that some collection methods will give you unexpected results). You likely mean to start an empty object as data.

var data = {};
data['__type'] = 'urn:inin.com:connection:icAuthConnectionRequestSettings';
data['applicationName'] = 'test';
data['userID'] = 'blah';
data['password'] = 'blah';
data['val-name'] = 'blah';

Finally, you can use data literals in JS, rather than serial assignment.

var data = {
'__type': 'urn:inin.com:connection:icAuthConnectionRequestSettings',
'applicationName': 'test',
'userID': 'blah',
'password': 'blah',
'val-name': 'blah'
}

As part of this, you've created an object with a slot name that has a - in it. There's nothing illegal about this, but it does prevent you from accessing that slot with dot notation.

console> data['val-name']
'blah'
console> data.val-name
NaN

That has nothing to do with the key being illegal, and everything to do with the access being parsed as a subtraction. That is, data.val-name gets interpreted as "Subtract the value of name from the value of data.val" rather than "Access the slot val-name of the object data".

How to get the property of an object which has a dash (-) in between name

Same as always.

$row->{'Employee-ID'}

php object attribute with dot in name

Specify aliases in your SQL query like SELECT column AS nameWithoutDots ...

or access these properties with $object->{'operation.name'}

or cast the object to array like this: $obj = (array)$obj; echo $obj['operation.name'].



Related Topics



Leave a reply



Submit