到目前为止我的代码
<?php
foreach($sub_category as $row2){
$url = 'https://myurl.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
echo $name." data = "."<pre>"; print_r($data);
}
?>这是$data输出
Array
(
[0] => stdClass Object
(
[course_title] => 60+ Space Facts To Get An A In Astronomy
[description] => 60+ Space Facts To Get An "A" In Astronomy
[image] => pexels-pixabay-39896_20210220014840929292735.jpg
)
[1] => stdClass Object
(
[course_title] => Learn Chinese
[description] => Learn Chinese
[image] => Chinoabc_20211227114307692406780.png
)
)我想把它转换成如下所示
Array
(
[0] => array
(
[course_title] => 60+ Space Facts To Get An A In Astronomy
[description] => 60+ Space Facts To Get An "A" In Astronomy
[image] => pexels-pixabay-39896_20210220014840929292735.jpg
)
[1] => array
(
[course_title] => Learn Chinese
[description] => Learn Chinese
[image] => Chinoabc_20211227114307692406780.png
)
)我在这里尝试过很多答案,converting array of objects to simple array,但它对我没有用
我已经尝试过从已经发布的问题中得到了很多答案。我的问题与这里发布的任何其他相关问题不同。
发布于 2022-01-10 17:36:31
可以使用(array)类型强制转换将对象转换为等效的关联数组。
$data = array_map(function($x) { return (array)$x; }, $data);如果原始的对象数组来自使用json_decode(),您可以告诉它返回关联数组而不是对象,方法是给它一个true第二个参数。
$data = json_decode($data, true);https://stackoverflow.com/questions/70656613
复制相似问题