【问题标题】:PHP - Output Specific Line from ArrayPHP - 从数组中输出特定的行
【发布时间】:2012-12-14 22:04:22
【问题描述】:

我正在使用表单来收款并使用 print_r,我可以在输出中看到以下内容...

[customer] => ChargifyCustomer Object
    (
        [email] => test@aol.com
        [first_name] => John
        [last_name] => Doe
        [organization] => 
        [reference] => 
        [id] => 2588487
        [created_at] => 2012-12-14T08:50:45-05:00
        [updated_at] => 2012-12-14T08:50:45-05:00
    )

生成此输出的 PHP 如下所示...

try {
    $new_subscription = $subscription->create();
    if ($new_subscription != '') {
        // session_unset();
        echo '<pre>';
        print_r($new_subscription);
        echo '</pre>';
        foreach ($new_subscription as $item) {
            echo 'First Name' . $item->customer->first_name . '.';
        }
    }
}

虽然每次我加载页面时,名字都不会被回显。相反,它只是说“名字”。好几次。

请帮忙确定我的错误在哪里。

【问题讨论】:

  • 为什么不在foreach 循环中尝试print_r($item),看看它是什么样子的。我猜你快到了

标签: php chargify


【解决方案1】:

您需要检查密钥:

foreach($new_subscription as $key => $item) {
    if($key == 'first_name') {
        echo "First Name: " . $item . '.';
    }
}

但我不知道你为什么要做一个 foreach,因为 $new_subscription 不是一个对象数组。实际上你应该说:

echo "First Name: " . $new_subscription->first_name. '.';

【讨论】:

  • $new_subscription['customer']-&gt;first_name 而不是 $new_subscription-&gt;first_name
  • 他应该复制整个 print_r,如果它实际上是一个数组,它的开头应该是:Array ( [customer] =&gt; ChargifyCustomer Object ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 2015-06-23
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多