2

TLDR; My question is different from PHP. Is it possible to use array_column with an array of objects. I want to only change the keys within the array and keep the objects, not having the objects' values stored in a separate array like the given answer.

I would like to set the keys, of an array with objects, to a value of the object. So this array:

$array = Array
(
    [0] => stdClass Object
        (
            [id] = 12234
            [value] = some value
        )

    [1] => stdClass Object
        (
            [id] = 12994
            [value] = some value
        )

)

Should become:

$array = Array
(
    [12234] => stdClass Object
        (
            [id] = 12234
            [value] = some value
        )

    [12994] => stdClass Object
        (
            [id] = 12994
            [value] = some value
        )

)

Now I could loop over the array, but I would prefer a more cleaner solution. I thought this should work:

$newArray = array_column($array, null, 'id');

The only problem is I'm having an array of objects instead of an array of arrays and I'm not using PHP7 yet. Now I found a similar question over here PHP. Is it possible to use array_column with an array of objects

But the thing is it doesn't return what I expected. Cause this:

$newArray = array_map(function($o) {
        return is_object($o) ? $o->id : $o['id'];
    }, $array);

Returns

Array
(
    [0] => 12234
    [1] => 12994
)

Anyone who knows a clean solution (so without a for or foreach loop) for this?

2
  • What's your question? I showed my current array and how I would prefer it to be. And I showed you what I tried and what didn't work, so what do you need more exactly if I may ask? Commented Jul 8, 2016 at 10:04
  • 1
    @Oyeme did you even read? I already mentioned that link and why that answer didn't work for me.... Commented Jul 8, 2016 at 10:08

2 Answers 2

8
$array = array_combine(array_map(function ($o) { return $o->id; }, $array), $array);

Whether this is really a lot better than a simple foreach loop, aside from "but, but, functional programming...!", is debatable.

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

3 Comments

Well it sure does look a lot cleaner to me ;), and it works perfectly. Thanks a lot!
Do note that this is probably a lot slower than a foreach, due to incurring function call overhead, temporary values and multiple iterations instead of one. It probably doesn't matter much at all to the bottomline, but be aware nonetheless.
Alright, good to know! Thanks for the info :)
1
// your data

$array = array(
    (object) array(
        "id" => "12234", 
        "value" => "some value", 
        ),
    (object) array(
        "id" => "12235", 
        "value" => "some value", 
        ),
    (object) array(
        "id" => "12236", 
        "value" => "some value", 
        ), 
    );

// let's see what we have

print_r($array);


// here comes the magic ;-)

function key_flip_array($array, $keyname){
    $keys = array_map(function($item, $keyname){
        return (is_object($item) && isset($item->{$keyname}) ? $item->{$keyname} : (is_array($item) && isset($item[$keyname]) ? $item[$keyname] : null));
        }, $array, array_fill(0, count($array), $keyname));

    return array_combine($keys, $array);
    }


$array = key_flip_array($array, "id");

// i hope this is what you wish to see

print_r($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.