0

I have this kind of array which I want it to be populated with values from MySQL db. For example if there are 5 rows in the database, it should loop the array 5 times together with the values in it.

This is how my current array (to be parsed in JSON) looks like:

array(
    1 => array(
        "id" => 1,
        "category" => "For men",
        "items" => array(
            array(
                "id" => 1,
                "name" => "Baju",
                "price" => "5:52"
            ),
            array(
                "id" => 2,
                "name" => "The Canyon",
                "price" => "3:01"
            )
        )
    ),
    2 => array(
        "id" => 2,
        "category" => "Adele 21",
        "items" => array(
            array(
                "id" => 1, 
                "name" => "Rolling In The Deep", 
                "price" => "03:48"
            ),
            array(
                "id" => 2,
                "name" => "Rumour Has It",
                "price" => "03:43"
            )
        )
    )
),
.
.
.
. //more arrays here;

So I did a while loop to print out (print_r()) the array according from my database. It looks like this:

Array (
    [0] => Array (
        [id] => 1
        [item_photo] => gambar1
        [item_name] => Kotak
        [item_description] => Desc 1 - Lorem ipsum blablabla
        [user_id] => 6
        [category_id] => 2
    )
    [1] => Array (
        [id] => 2
        [item_photo] => gambar2
        [item_name] => Botol
        [item_description] => Desc 2 - Lorem ipsum blablabla
        [user_id] => 6
        [category_id] => 3
    )
    [n] . . .
    .
    .
    .
)

What I wanna achieve is to replace the values in the first array (to be parsed in JSON) with the one in array two. Like so,

array(
    $category_id => array(
        "id" => $i++,
        "category" => $category_name,
        "items" => array(
            array(
                "id" => $j++,
                "user_name" => $user_name,
                "name" => $item_name,
                "price" => $price,
                "item_description" => $item_description
                )
            )
        ),
); //notice the variables

This is only just a sketching of the idea of what I want to achieve. But I couldn't wrap my head around this to make it work.

Any idea how to populate the first array according to the second array's value?

2
  • 2
    write a correct mysql query and achieve this by loop... Commented Oct 17, 2013 at 7:34
  • Really? Can you show me how? Like, is there a way to make it look like array one in the example I've given? Commented Oct 17, 2013 at 8:18

0

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.