如何在php中将对象数组转换为该对象的单个属性的数组?

时间:2021-10-21 20:21:33

Say I have an array of Person objects and the person objects have properties firstName, lastName, and age. Now suppose I want an array of the firstnames of all these person objects. How can I convert this array of Person objects into an array of firstname strings?

假设我有一个Person对象数组,person对象具有属性firstName,lastName和age。现在假设我想要一个所有这些人物对象的第一个名字的数组。如何将此Person对象数组转换为firstname字符串数组?

Is there some combination of array functions I can use to do it or do I just have use a for loop and create a new array?

是否有一些我可以使用的数组函数组合,或者我只是使用for循环并创建一个新数组?

4 个解决方案

#1


4  

You can use array_map

您可以使用array_map

    class Person
{
    public $firstName ;
    public $lastName ;
    public $age ;

    function __construct($firstName,$lastName,$age)
    {
        $this->firstName = $firstName ;
        $this->lastName = $lastName ;
        $this->age = $age ;
    }
}

$list = array();
$list[] = new Person("John", "Smith", 22);
$list[] = new Person("Jon", "Doe", 19);
$list[] = new Person("Jane", "Stack", 21);

$lastNames = array_map(function($var){return $var->lastName ; } ,$list);
var_dump($lastNames);

Output

array
  0 => string 'Smith' (length=5)
  1 => string 'Doe' (length=3)
  2 => string 'Stack' (length=5)

#2


0  

You can loop through the array of person objects, and then loop through the person object and create array from the firstname property. See an example, which coincidently uses a person class as an example:

您可以循环遍历person对象数组,然后遍历person对象并从firstname属性创建数组。查看一个示例,它巧合地使用person类作为示例:

http://www.tuxradar.com/practicalphp/6/7/6

#3


0  

A simple loop would be the best:

一个简单的循环将是最好的:

$persons = getPersons();

$firstnames = array();

foreach($persons as $person) {
    $firstnames[] = $person->getFirstName();    
}

#4


0  

You have to loop through the array of objects. Either use the normal foreach or use array_map.

你必须循环遍历对象数组。使用普通的foreach或使用array_map。

function get_first_name($person)
{
    return $person->firstName;
}
$firstNames = array_map('get_first_name', $people);

#1


4  

You can use array_map

您可以使用array_map

    class Person
{
    public $firstName ;
    public $lastName ;
    public $age ;

    function __construct($firstName,$lastName,$age)
    {
        $this->firstName = $firstName ;
        $this->lastName = $lastName ;
        $this->age = $age ;
    }
}

$list = array();
$list[] = new Person("John", "Smith", 22);
$list[] = new Person("Jon", "Doe", 19);
$list[] = new Person("Jane", "Stack", 21);

$lastNames = array_map(function($var){return $var->lastName ; } ,$list);
var_dump($lastNames);

Output

array
  0 => string 'Smith' (length=5)
  1 => string 'Doe' (length=3)
  2 => string 'Stack' (length=5)

#2


0  

You can loop through the array of person objects, and then loop through the person object and create array from the firstname property. See an example, which coincidently uses a person class as an example:

您可以循环遍历person对象数组,然后遍历person对象并从firstname属性创建数组。查看一个示例,它巧合地使用person类作为示例:

http://www.tuxradar.com/practicalphp/6/7/6

#3


0  

A simple loop would be the best:

一个简单的循环将是最好的:

$persons = getPersons();

$firstnames = array();

foreach($persons as $person) {
    $firstnames[] = $person->getFirstName();    
}

#4


0  

You have to loop through the array of objects. Either use the normal foreach or use array_map.

你必须循环遍历对象数组。使用普通的foreach或使用array_map。

function get_first_name($person)
{
    return $person->firstName;
}
$firstNames = array_map('get_first_name', $people);