-2

I have an array of objects, each object consists of an id and an organisation. It looks like this

Array ( 
  [0] => stdClass Object ( 
      [id] => 2 [organisation] => org1
  )   
  [1] => stdClass Object ( 
      [id] => 4 [organisation] => org2
   ) 
  [2] => stdClass Object ( 
      [id] => 1 [organisation] => org3
  ) 
) 

I need to convert it into a simple associative array ([id]=>organisation,...) so the above example would look like this

Array (
    [2] => org1
    [4] => org2
    [1] => org3
)

Greatful for any thoughts

1
  • So what is wrong with leaving the objects as they are and using $arry[$x]->organisation or foreach ($array as $obj ) { echo $obj->organisation; } Its much easier to read later Commented Aug 19, 2014 at 13:21

2 Answers 2

3

Loop through it using a foreach statement and append it to another array.

$finished = [];

foreach($array as $arr) {
    $finished[$arr->id] = $arr->organisation;
}
Sign up to request clarification or add additional context in comments.

Comments

2
$result = array();
foreach($array as $arr) {
    $result[$arr->id] = $arr->organisation;
}
echo "<pre>";print_r($result);

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.