3

I queried my database and it stores the results into an array. It looks like this:

Array
(
[0] => Array
    (
        [Submission_ID] => 111
        [First_Name] => Dylan
        [Last_Name] => Taylor
        [Abstract_Title] => Research 1
    )

[1] => Array
    (
        [Submission_ID] => 222
        [First_Name] => Michael
        [Last_Name] => Jones
        [Abstract_Title] => Research 2
    )

[2] => Array
    (
        [Submission_ID] => 333
        [First_Name] => Wills
        [Last_Name] => Adams
        [Abstract_Title] => Research 3
    )
)

This is all placed in a variable called $results. Currently, I'm displaying bits of data on my page like so:

echo $results[0][Abstract_Title]

It all works, however, it be convenient if the index keys were replaced with the Submission ID:

Array
(
[111] => Array
    (
        [Submission_ID] => 111
        [First_Name] => Dylan
        [Last_Name] => Taylor
        [Abstract_Title] => Research 1
    )

[222] => Array
    (
        [Submission_ID] => 222
        [First_Name] => Michael
        [Last_Name] => Jones
        [Abstract_Title] => Research 2
    )

[333] => Array
    (
        [Submission_ID] => 333
        [First_Name] => Wills
        [Last_Name] => Adams
        [Abstract_Title] => Research 3
    )
)

So I can do this instead (otherwise I would have to print the array just to look up the index key every time):

echo $results[111][Abstract_Title]

Any ideas? (I'm new to programming.) The closest answer I've found is: php replace array id keys but I can't get my head around the solution. I figured a forloop is the best option? Should the new array be placed in the same variable or new variable?

0

1 Answer 1

4

You can simply use array_combine and array_column like as

$final_result = array_combine(array_column($result,'Submission_ID'),$result);
print_r($final_result);
Sign up to request clarification or add additional context in comments.

Comments

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.