Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:00:07+00:00 2026-06-15T13:00:07+00:00

I often encounter a situation where I have to get an array of values

  • 0

I often encounter a situation where I have to get an array of values that come from multiple objects. Let me clarify that with an fictional situation and some code.

Imagine I have created a class…

class MyObject {
    public $x = 0;
    public $y = 0;
}

I have an array of instances…

$instances = array(
    new Object,
    new Object,
    new Object
);

and I want to join all x properties as strings, devided with comma’s… I would have to use a loop…

$array = array();

foreach($instances as $instance) array_push($array, $instance->x);

$str = implode(',', $array); // holds an array of all x values

That would totally work, but I am very much against unnecessary loops and pro native implementations because I have a little obsession for performance – I dislike looping through huge arrays only to get a single property off every instance.

Is there a native way to get an array of property values from multiple objects? If so, how? If not, any idea why not?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:00:09+00:00Added an answer on June 15, 2026 at 1:00 pm

    A simple easy way is to use array_map

    class MyObject {
        public $x = 0;
        public $y = 0;
    }
    
    $instances = array(
            new MyObject,
            new MyObject,
            new MyObject
    );
    
    
    $str = implode(",",array_map(function($v){ return $v->x;},$instances));
    echo $str ; 
    

    Or array_reduce

    $str = trim(array_reduce($instances, function($a,$b){ $a .= $b->x . "," ; return $a;}),",");
    echo $str ;
    

    The both would output

    0,0,0
    

    You can also modify your class to use __toString() Example

    class MyObject {
        public $x = 0;
        public $y = 0;
        function __toString() {
            return $this->x . "," . $this->y;
        }
    }
    
    $instances = array(new MyObject(),new MyObject(),new MyObject());
    echo implode("\n", $instances);
    

    Output

    0,0
    0,0
    0,0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When working on code from multiple authors, I often encounter the issue of curly-brace
Sorry for the long title. Here is a sample situation that I encounter often
I have often encountered an error such as cannot convert from 'method group' to
Often, when searching for answers, I have found that certain websites will allow you
I'm currently doing some oracle sql analysis and often encounter valid sql statement that
Couple of examples that I encounter quite often. Indenting some code E.g. I want
As a developer, we often encounter that exception: NullReferenceException with the well known error
I have often seen Makefiles that start commands with an @ symbol to suppress
Very often I encounter situation where I need to see or manually compare two
Often I have this situation: var obj = { field: 2 }; and in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.