使用数据库中的数据填充PHP数组

时间:2013-10-17 07:27:53

标签: php mysql arrays

我有这种数组,我希望它用MySQL db中的值填充。例如,如果数据库中有5行,它应该将数组与其中的值一起循环5次。

这是我当前的数组(用JSON解析)的样子:

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;

所以我做了一个while循环,根据我的数据库打印出(print_r())数组。它看起来像这样:

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] . . .
    .
    .
    .
)

我想要实现的是将第一个数组中的值(在JSON中解析)替换为数组二中的值。像这样,

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

这只是我想要实现的想法的草图。但我无法绕过这个让它发挥作用。

知道如何根据第二个数组的值填充第一个数组吗?

0 个答案:

没有答案