更改 PHP 中的数组结构

发布于 2024-09-01 23:38:02 字数 680 浏览 8 评论 0原文

参考我之前的问题:Show Values in TDropDownList in PRADO。 好吧,我从查询中收到的数组是一个对象数组,例如:

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] => [_invalidFinderResult:protected] => [_e:TComponent:private] => Array ( ) ) 

如果我将其转换为数组,例如:

Array ( [key 1] => leo [key 2] => ganda )

那么我可以将值填充到 TDropDownList 中。

现在谁能帮我转换我需要的数组结构......?

再次感谢

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] => [_invalidFinderResult: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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪推晚风 2024-09-08 23:38:02

如果您不关心密钥:

array_map(function (ContactRecord $o) { return $o->name; }, $origArray)

否则:

$res = array();
foreach ($origArray as $obj) {
    $res[$o->id] = $o->name;
}

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;
}
猫性小仙女 2024-09-08 23:38:02

如果我没记错的话,PHP 中的 foreach 也适用于对象。尝试以下操作:

$ret = array();
foreach ($object as $val) $ret[] = $val;

此外,您还可以在 PHP 中以 $object->$propertyName 的形式检索属性。因此,如果您可以获取属性名称,您只需循环遍历它们,检索值并将它们推送到数组中即可。

问候
后退2dos

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文