gpt4 book ai didi

php - 数组的特殊工作

转载 作者:行者123 更新时间:2023-11-29 23:18:44 24 4
gpt4 key购买 nike

我想用 PHP 数组做一些事情。我写了一个函数来做一些我会说的事情。我的输入数组是:

Array
(
[0] => stdClass Object
(
[message] => Automated XML Site Map Generator
http://pastebin.com/xjJe38dp
[id] => 103114753133019_371405176303974
[from] => stdClass Object
(
[id] => 735588533186829
[name] => Mohammad Mostafa Shahreki
)

[created_time] => 2013-04-26
[updated_time] => 2013-04-26
)

[1] => stdClass Object
(
[message] => Simple but powerful DB class
http://pastebin.com/1qgxUrwX
[id] => 103114753133019_371404696304022
[from] => stdClass Object
(
[id] => 735588533186829
[name] => Mohammad Mostafa Shahreki
)

[created_time] => 2013-04-26
[updated_time] => 2013-04-26
)

[2] => stdClass Object
(
[message] => Convert Existing DB to Unicode
http://pastebin.com/pHu08cPs
[id] => 103114753133019_371404609637364
[from] => stdClass Object
(
[id] => 735588533186829
[name] => Mohammad Mostafa Shahreki
)

[created_time] => 2013-04-26
[updated_time] => 2013-04-26
)

[3] => stdClass Object
(
[message] => thanks mosthafa ,,,, for adding me to this group
[id] => 103114753133019_103333343111160
[from] => stdClass Object
(
[id] => 10155092057165556
[name] => HalF PixeL
)

[created_time] => 2011-10-11
[updated_time] => 2011-11-21
)
)

我想将每个数据保存在我的 MySQL 数据库中。例如,我想通过函数保存 message,id,from->name,created_time 。我尝试编写一个 foreach 循环,但找不到添加 from->name 的方法,它是一个 stdClass,不能与 ['from']['name'] 一起使用。你能告诉我该怎么做吗?

最佳答案

对象的属性通过“->”运算符访问(除非您实现 ArrayAccess 接口(interface))。您应该像这样获取发件人姓名:

$var[0]->from->name

您可以选择将其转换为数组:

$from = (array) $var[0]->from;
echo $from['name'];

关于php - 数组的特殊工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27531199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com