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?

4
  • 1
    Apparently what you are dealing with is not array, but an object. You will have to convert it manually with a foreach loop. Commented Aug 5, 2013 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. Commented Aug 5, 2013 at 11:07
  • So you want to be able to access other columns by using one column as an index? Commented Aug 5, 2013 at 11:09
  • 1
    You can cast object to array, according to this question: stackoverflow.com/questions/4345554/… - but it's not a magic wand, you still have to use a foreach loop. Commented Aug 5, 2013 at 11:10

3 Answers 3

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.

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

3 Comments

That's the way I'm doing it now, but I was hoping there'd be a quicker way to do it.
@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.
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.
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);

Comments

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.

2 Comments

You don't necessarily have to have a while loop to fetch the results, OP could be calling something similar to PDO's fetchAll
@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.

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.