如何在PHP中遍历对象数组

6

我对PHP非常陌生,需要您的帮助! 我需要为我的应用程序编写后端,接收json post并将数据写入json文件。但是我在遍历数组时卡住了。

$postData = file_get_contents("php://input");
$request = json_decode($postData);
var_damp($request)

显示数组:

array(2) {
  [0]=>
  object(stdClass)#1 (8) {
    ["name"]=>
    string(11) "Alex Jordan"
    ["email"]=>
    string(14) "alex@gmail.com"
    ["phone"]=>
    int(123456789)
    ["street"]=>
    string(12) "street, str."
    ["city"]=>
    string(7) "Chicago"
    ["state"]=>
    string(7) "Chicago"
    ["zip"]=>
    string(5) "07202"
    ["$$hashKey"]=>
    string(8) "object:3"
  }
  [1]=>
  object(stdClass)#2 (8) {
    ["name"]=>
    string(15) "Michael Jonhson"
    ["email"]=>
    string(17) "michael@gmail.com"
    ["phone"]=>
    float(11987654321)
    ["street"]=>
    string(12) "street, str."
    ["city"]=>
    string(11) "Los Angeles"
   ["state"]=>
   string(10) "California"
   ["zip"]=>
   string(5) "27222"
   ["$$hashKey"]=>
   string(8) "object:4"
 }
}

我正在尝试循环遍历对象,但是出现了错误:

无法将stdClass类的对象转换为字符串

这是我的代码:

    foreach($request as $i => $i_value) {
        echo $i_value;
    }

1
echo $i_value->name; 等等... 或者遍历对象。 - AbraCadaver
3个回答

13

$i_value 这个对象。因为它是一个对象,所以你不能像在JavaScript中一样将任何对象转换为字符串并进行echo操作。

你可以echo对象的特定属性:

foreach($request as $i => $i_value) {
    echo $i_value->name;
}
当然,你也可以再次使用var_dump来转储每个对象。print_r同样有效。
只有实现了__toString()魔术方法的对象才能像你所做的那样强制转换为字符串,但由json_decode创建的对象只是简单的StdClass对象,它们没有实现这个方法。这可能根本不是你的意图,但如果你好奇,可以查看将json_decode转换为自定义类以了解如何使用自定义类而不是StdClass。

1
我最近遇到了同样的问题。我使用了一个for循环来遍历数组,但是出现了“undefined offset”的错误。使用isset()解决了“undefined offset”的错误。我知道回答这个问题已经太晚了,但是对于将来可能正在寻找答案的人,这里是答案。
$postData = file_get_contents("php://input");
$request = json_decode($postData);
// or you can do $postData = json_decode(file_get_contents("php://input"));
$arrayOfUsers = $request->data; // I used a key of data while sending the array via fetch API

for($x = 0; $x < count($arrayOfUsers); $x++)
{
    if(isset($arrayOfUsers[$x]))
    {
        $name = $arrayOfUsers[$x]->name;
        $email = $arrayOfUsers[$x]->email;
        $phone = $arrayOfUsers[$x]->phone;
        $street = $arrayOfUSers[$x]->street;
        // .... and so on
       // then you can do whatever you want with the data
    } else {
      echo "No Data Found";
    }
}

0

你也可以使用array_map(?callable $callback, array $array, array ...$arrays): array

  1. 获取每个对象所需的属性;
  2. 甚至更改其数据。

此代码涵盖了两种情况(sandbox_url):

<?php
$arr_of_objects[] = (object)['field_name' => 'test2', 'id' => 2];
$arr_of_objects[] = (object)['field_name' => 'test', 'id' => 1];

$array_of_names = array_map(function ($el) {
    return $el->field_name;
}, $arr_of_objects);

echo 'get field_name from array of objects' . "\n";
var_dump($array_of_names);

$replace_data_with = [
    1 => ['field_name' => 'replaced_name_1', 'id' => 777],
    2 => ['field_name' => 'replaced_name_2', 'id' => 999]
];

$changed_values = array_map(function ($el) use ($replace_data_with) {
    $el->field_name = $replace_data_with[$el->id]['field_name'];
    $el->id = $replace_data_with[$el->id]['id'];
    return $el;
}, $arr_of_objects);

echo 'alter data of each object' . "\n";
var_dump($changed_values);

对象的基本数组:

array(2) {
  [0]=>
  object(stdClass)#1 (2) {
    ["field_name"]=>
    string(5) "test2"
    ["id"]=>
    int(2)
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["field_name"]=>
    string(4) "test"
    ["id"]=>
    int(1)
  }
}

输出:

get field_name from array of objects
array(2) {
  [0]=>
  string(5) "test2"
  [1]=>
  string(4) "test"
}
alter data of each object
array(2) {
  [0]=>
  object(stdClass)#1 (2) {
    ["field_name"]=>
    string(15) "replaced_name_2"
    ["id"]=>
    int(999)
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["field_name"]=>
    string(15) "replaced_name_1"
    ["id"]=>
    int(777)
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接