转换stdClass介绍数组

时间:2016-08-18 07:21:46

标签: php arrays object stdclass

stdClass Object
(
    [CountyId] => 3
    [Name] => Alba
    [Abbreviation] => AB
)
stdClass Object
(
    [CountyId] => 4
    [Name] => Arad
    [Abbreviation] => AR
)
stdClass Object
(
    [CountyId] => 5
    [Name] => Arges
    [Abbreviation] => AG
)

我想将此stdClass Object集合转换为仅包含CountyId的数组,例如

[CountyId[0] => 3, CountyId[1] => 4, CountyId[2] => 5,...].

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:0)

试试这个:

$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);