0

Let's say i do a query:

select * from branches

Now, I get a result:

ID | CODE | Name  | etc...
1  | ABC  | One   | etc...
2  | BED  | Two   | etc...
3  | CPE  | Three | etc...
4  | FEE  | Four  | etc...

I will end up with an array that can be used like this:

$data[0]->name;

I want to change this array so I can call it like this:

$data['ABC']->name;

What is the quickest way to convert an array so that it's key is one of the items in the array? I.e.Preferably without looping and assigning?

rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • 1
    Apparently what you are dealing with is not `array`, but an object. You will have to convert it manually with a foreach loop. – MightyPork Aug 05 '13 at 11:05
  • Well, its an array of objects, yes. But I was hoping there would be a simple way to do this using something like "array_keys" or something similar. – rockstardev Aug 05 '13 at 11:07
  • So you want to be able to access other columns by using one column as an index? –  Aug 05 '13 at 11:09
  • 1
    You can cast object to array, according to this question: http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array - but it's not a magic wand, you still have to use a foreach loop. – MightyPork Aug 05 '13 at 11:10

3 Answers3

4

Simplest solution for this I think.

$assoc = array();
$length = count($data);

for($i = 0; $i < $length; $i++) {
    $assoc[$data[$i]->CODE] = $data[$i]; 
}

print_r($assoc);

Although I believe there is a one line solution. Can't remember it off the top of my head however.

EDIT:

Changed for loop condition, good call Raymond.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • That's the way I'm doing it now, but I was hoping there'd be a quicker way to do it. – rockstardev Aug 05 '13 at 11:12
  • @coderama Then see if MightyPork knows it, the one liner I was thinking of may have indeed involved `array_map` however it will still involve iteration,an annonymous function instead of a loop. – Flosculus Aug 05 '13 at 11:13
  • You could do this with `array_map` or `array_walk`, but it won't be "better" or "faster". The solution in this answer is fine. – Mahn Aug 05 '13 at 11:30
1

Here is an in-line solution, if you really want to use compact code.

However, I think this is way messier than a simple foreach loop.

$arr = array(your obj array);

$assoc = array();
array_walk($arr, function($v) {global $assoc; $assoc[$v->CODE]=$v;});

print_r($assoc);
MightyPork
  • 18,270
  • 10
  • 79
  • 133
0

Somewhere in your script you must be processing through a resultset and building this array so why not look back at that.

What you are probably doing!

while ( $row = yourFetch ) {
    $data[] = $row;
endwhile;
return $data;

You could do this instead :-

while ( $row = yourFetch ) {
    $data[$row->CODE] = $row;
endwhile;
return $data;

Then you dont have to add any processing after the event.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • You don't necessarily have to have a while loop to fetch the results, OP could be calling something similar to [PDO's fetchAll](http://php.net/manual/en/pdostatement.fetchall.php) – Mahn Aug 05 '13 at 11:29
  • @Mahn Well in that case, if he wants the result he is after he can change it and save himself the expence of reformatting an array. – RiggsFolly Aug 05 '13 at 11:30