Php 将stdClass简介转换为数组
我想将这个stdClass对象集合转换成一个只包含CountyId的数组,例如 [CountyId[0]=>3,CountyId[1]=>4,CountyId[2]=>5,…] 有人能帮我吗?试试这个:Php 将stdClass简介转换为数组,php,arrays,object,stdclass,Php,Arrays,Object,Stdclass,我想将这个stdClass对象集合转换成一个只包含CountyId的数组,例如 [CountyId[0]=>3,CountyId[1]=>4,CountyId[2]=>5,…] 有人能帮我吗?试试这个: stdClass Object ( [CountyId] => 3 [Name] => Alba [Abbreviation] => AB ) stdClass Object ( [CountyId] => 4 [Name] =&g
stdClass Object
(
[CountyId] => 3
[Name] => Alba
[Abbreviation] => AB
)
stdClass Object
(
[CountyId] => 4
[Name] => Arad
[Abbreviation] => AR
)
stdClass Object
(
[CountyId] => 5
[Name] => Arges
[Abbreviation] => AG
)
更新:
参考答案没有多大帮助。为什么关闭?他需要这样的东西:[CountyId[0]=>3,CountyId[0]=>4,CountyId[0]=>5,…]你有一个对象数组吗?你想转换成['CountyId'=>[3],'CountyId'=>[4],'CountyId'=>[5],…]?你不能用所有CountryId的相同索引直接实现这一点。我不清楚输出应该是什么。我正在更新您的答案。我认为当post是对象时,array_列不起作用。首先使用array_-map将文章转换为数组?OP已经将对象转换为数组,所以我没有使用@托尔比奥rnStabo@devpro我认为数组_列才是OP真正想要的。这一行令人困惑且不必要:$CountyIdArr['CountryID']=$CountyIdArr@Progrock:评论告诉一切,兄弟。
$array = (array)$stdClassObject; // type casting
// convert your object into array using type casting
$array = (array) $stdClassObject;
// use array_column for specific index
$CountyIdArr = array_column($array, 'CountyId');
// note that, you can not use same index name for all values, you need to use as
$CountyIdArr['CountryID'] = $CountyIdArr;
echo "<pre>";
print_r($CountyIdArr);