Change array structure in PHP
Refers to my previous question : Show values in TDropDownList in PRADO. ok fine the array i receive from query is an object array like :
ContactRecord Object ( [id] => 1 [name] => leo [_recordState:protected] => 1 [_connection:protected] => [_invalidFinderResult:protected] => [_e:TComponent:private] => Array ( ) )
ContactRecord Object ( [id] => 2 [name] => ganda [_recordState:protected] => 1 [_connection:protected] => [_invali开发者_C百科dFinderResult:protected] => [_e:TComponent:private] => Array ( ) )
If I convert it in to array like:
Array ( [key 1] => leo [key 2] => ganda )
then I can populate values into TDropDownList.
Now can anyone help me to convert array structure which I need ... ?
Again thanks
If you don't care about the keys:
array_map(function (ContactRecord $o) { return $o->name; }, $origArray)
Otherwise:
$res = array();
foreach ($origArray as $obj) {
$res[$o->id] = $o->name;
}
If I remember well, foreach
in PHP works with objects as well. try the following:
$ret = array();
foreach ($object as $val) $ret[] = $val;
also, you can retrieve a property in PHP as $object->$propertyName
. so if you can get hold of the property names, you just loop through them, retrieve the values and push them to an array.
greetz
back2dos
精彩评论