【问题标题】:Special work with array数组的特殊工作
【发布时间】:2014-12-17 17:18:45
【问题描述】:

我想用 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'] 一起使用。你能告诉我怎么做吗?

【问题讨论】:

  • ['from']->名字,名字是一个属性
  • 嘿。我有一个这样的数组!!!!如你所知,我不能在他们附近互相使用。我该怎么办?

标签: php mysql arrays facebook-php-sdk


【解决方案1】:

对象的属性使用'->' 操作符访问(除非你实现了ArrayAccess 接口)。你应该像这样得到你的发件人姓名:

$var[0]->from->name

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

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

【讨论】:

  • 我都知道。如您所知,我想编写一个动态函数,它提供一个数组并将其添加到 mysql 数据库中,那么我现在该怎么办?
【解决方案2】:

假设您将整个数组保存为变量$array,请尝试:

$array[0]->from->name;

你的函数可能看起来像这样:

for ($i = 0; $i < $inputArray.count(); $i++) {
  $message = $inputArray[$i]->message;
  $id = $inputArray[$i]->id;
  $name = $inputArray[$i]->from->name;
  $created = $inputArray[$i]->created_time;

  // Store the record as you want.
}

// Alternatively, a foreach loop
// See: http://php.net/manual/en/control-structures.foreach.php
foreach ($inputArray as $entry) {
  $message = $entry->message;
  $id = $entry->id;
  $name = $entry->from->name;
  $created = $entry->created_time;

  // Store the record as you want.
}      

【讨论】:

  • 我告诉过你我想写一个函数来保存所有数据。这可行,但我不能在函数中互相保存
  • 您可以使用for 循环遍历数组的元素。我已经编辑了我的答案以包含一个示例。
  • 嘿,亲爱的。它不仅仅是 ** message,id,form->name,created_time ** 它是一个动态函数。 :|有什么办法吗:|
  • 对不起,我不太明白你的意思。你是说数组中的每个对象可以有可变数量的属性?
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 2011-03-27
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多