0

First, I have tried to find solution within the source here but couldn't find what I am looking so posting as a new question. Thanks for your help

I want to convert Array to Object. Here is what I am getting output

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [username] => robustsolver
            [first_name] => John
            [last_name] => Smith
        )

    [1] => stdClass Object
        (
            [id] => 2
            [username] => pickypacker
            [first_name] => Peter
            [last_name] => Packer
        )

)

So if I want any column for all users than I have to write code $users[0]->first_name; which gives me only one item. But what I am looking is to use $users->first_name and this should return an array of all user's column (here is first_name)

Hope I have explain in better way. :S

6
  • try get_object_vars() Commented Jun 11, 2014 at 7:42
  • 2
    so you have an array of objects and you want to convert it to an object of arrays, is that right? Commented Jun 11, 2014 at 7:44
  • @aneesh getting error Message: get_object_vars() expects parameter 1 to be object, array given Commented Jun 11, 2014 at 7:45
  • @Ibrahim.I Yes, that's what I am trying to do so. Could you help me please? And yeah thank to learn me the terminology for such array. Like an array of objects I was confused how to say it in words. Thanks again.. :) Commented Jun 11, 2014 at 7:45
  • first you know the structure of your objects, right? I mean you know all the properties of the object Commented Jun 11, 2014 at 7:48

4 Answers 4

1

You can try this where $arr is your array:

function filter_callback($element) {
    return $element->first_name;
}
$result= array_map('filter_callback', $arr);

From a quick test, this seems to work. It keeps objects of the array without your wanted property but its value is set to NULL. Not sure if that's what you want but you can edit the filter_callback to remove such elements.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I think I just figured the question out... Not sure if this is doable with a single array_map call though...
I got the output as an array. Let me try others too.. Thanks
Is there any way to set column name first_name as a parameter somewhere so I can set as per my need?
1

Maybe something like this will work (not tested):

$arr = array(...); // array of objects (in)
$obj = new Object; // object of arrays (out)

foreach($arr as $a) {
    foreach(get_object_vars($a) as $k=>$v) {
        if(!property_exists($obj, $k)) {
            $obj->{$k} = array();
        }
        $obj->{$k}[] = $v;
    }
}

Comments

1

consider defining a new class:

class User_objects
{
    public $first_name;
    public $username;
    public $last_name;

    public function __construct()
    {
        $this->first_name = array();
        $this->username = array();
        $this->last_name = array();
    }
}

then: consider $array_of_objects to be your array of objects (input).

$users = new User_objects();

foreach ($array_of_objects as $object)
{
     $users->first_name[] = $object->first_name; // append to array
     $users->last_name[] = $object->last_name;
     $users->username[] = $object->username;
}

then you can get your array from $users->first_name

Comments

1

You can write a simple helper function that will aid in the columns you wish to select:

function prop_selector($prop)
{
    return function($item) use ($prop) {
        return $item->{$prop};
    };
}

$first_names = array_map(prop_selector('first_name'), $users);
$last_names = array_map(prop_selector('last_name'), $users);

4 Comments

Its giving Message: Undefined variable: prop any idea
@JatinSoni I thought you wanted to have the property name as a parameter, so a bit surprised to see you've accepted the other answer instead ...
Not actually, sorry about the confusion. but I wanted an object as a return value with having ability to set column name as parameter if needed.
@JatinSoni Then it seems that DarkSide's answer is what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.