0

Here's the my SQL table:

order_id | product_id | deal_title | currency_code | total | date_modified

My Question is how do I make an array in PHP like in the below:

Array
(
    [product_id_num1] => Array
        (
            [order_id]
            [deal_title]
            [currency_code]
            [total]
            [date_modified]
        )
    [product_id_num2] => Array
        (
            [order_id]
            [deal_title]
            [currency_code]
            [total]
            [date_modified]
        )
    [product_id_num3] => Array
        (
            [order_id]
            [deal_title]
            [currency_code]
            [total]
            [date_modified]
        )
etc....
)

Thanks!

2

1 Answer 1

2

You can try something like below

$arr = array();
while($row = mysqli_fetch_assoc($query_result)) {
    $id = $row['product_id'];
    unset($row['product_id']);
    $arr[$id] = $row;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@EatCodePlaySleep : Are you using mysqli_* or mysql_* functions? What is the error it shows? If no error is thrown use print_r($arr) after the while block to understand the structure of $arr array
nice! got it working now. just forgot one syntax. :)

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.