【问题标题】:how to get correct data from this array?如何从此数组中获取正确的数据?
【发布时间】:2014-04-28 17:04:33
【问题描述】:

这个答案可能看起来很简单,但我是 PHP 新手,花了太多时间试图弄清楚这一点。

我的数组如下所示:

        $phone_number[] = array(
            "type" => "home",
            "value" => $customer_phone                    
        );

我也有这个:

 $customer = array(
                "first_name" => $first_name,
                "phone_numbers" => $phone_numbers,
                "emails" => $emails,

我想检索每个客户的号码。我试过这个:

     for ($j = 0; $j <= count($customer_entries) - 1; $j++) {


      var_dump($customer_entries[$j]->phone_numbers->value);
     }

但它给了我一个错误。它说我正在尝试获取非对象的属性,然后它说 null。

但是当我使用时:

 var_dump($customer_entries[$j]->phone_numbers;

它给了我这样的东西:

array(1) {
[0]=>
object(stdClass)#744 (2) {
["type"]=>
string(4) "home"
["value"]=>
string(14) "(555) 555-5555"
 }
}

如何让它只给我名为“value”的字符串的结果?

谢谢。

【问题讨论】:

  • 首先,你说你有一个数组,但你试图将它作为一个对象来访问。其次,您有问题的数组名为$customer,但在您的循环中,您尝试使用$customer_entries

标签: php arrays string


【解决方案1】:
$customer_entries[$j]['phone_numbers']['value']

将其用作数组,而不是对象。

【讨论】:

  • 我试过了,它说:致命错误:不能使用 stdClass 类型的对象作为数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
  • 2017-06-09
  • 2021-08-03
  • 2014-02-12
  • 2015-07-05
相关资源
最近更新 更多