需要创建多维数组

时间:2015-04-24 07:11:51

标签: php arrays multidimensional-array

我已经尝试过以下代码来制作多维数组。但它不会创建我想要制作的数组。

$data = array();
while ($row_data = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $data[$row_data['in_order_id']] = array(
        'in_order_id' => $row_data['in_order_id'],
        'st_cust_first_name' => $row_data['st_cust_first_name'],
        'st_cust_last_name' => $row_data['st_cust_last_name'],
    );

    while ($row_products = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $data[$row_products['in_order_id']]['products'][] = array(
            'in_product_id' => $row_products['in_product_id'],
            'st_product_name' => $row_products['st_product_name'],
            'st_product_description' => $row_products['st_product_description'],
            'st_product_image' => $row_products['st_product_image']
        );
    }
}

我有不同信息的单维数组

Array
(
    [in_order_id] => 1
    [st_cust_first_name] => Scott
    [st_cust_last_name] => Tiger
    [in_product_id] => 1
    [st_product_name] => Theme 1
    [st_product_description] => Theme 1 for testing purpose only.
    [st_product_image] => theme_1.jpg
)
Array
(
    [in_order_id] => 1
    [st_cust_first_name] => Scott
    [st_cust_last_name] => Tiger
    [in_product_id] => 3
    [st_product_name] => Theme 3
    [st_product_description] => Theme 3 for testing purpose only.
    [st_product_image] => theme_3.jpg
)
Array
(
    [in_order_id] => 2
    [st_cust_first_name] => Raj
    [st_cust_last_name] => Agarwal
    [in_product_id] => 7
    [st_product_name] => Theme 7
    [st_product_description] => Theme 7 for testing purpose only.
    [st_product_image] => theme_7.jpg
)
Array
(
    [in_order_id] => 2
    [st_cust_first_name] => Raj
    [st_cust_last_name] => Agarwal
    [in_product_id] => 10
    [st_product_name] => Theme 10
    [st_product_description] => Theme 10 for testing purpose only.
    [st_product_image] => theme_10.jpg
)

我想创建下面显示的多维数组。请解决我的问题。

Array
(
    [in_order_id] => 1
    [st_cust_first_name] => Scott
    [st_cust_last_name] => Tiger
    [products] => Array(
        [0] => array
        (
            [in_product_id] => 1
            [st_product_name] => Theme 1
            [st_product_description] => Theme 1 for testing purpose only.
            [st_product_image] => theme_1.jpg
        )
        [1] => array
        (
            [in_product_id] => 3
            [st_product_name] => Theme 3
            [st_product_description] => Theme 3 for testing purpose only.
            [st_product_image] => theme_3.jpg
        )
    )
)

Array
(
    [in_order_id] => 2
    [st_cust_first_name] => Raj
    [st_cust_last_name] => Agarwal
    [products] => Array(
        [0] => array
        (
            [in_product_id] => 7
            [st_product_name] => Theme 7
            [st_product_description] => Theme 7 for testing purpose only.
            [st_product_image] => theme_7.jpg
        )
        [1] => array
        (
            [in_product_id] => 10
            [st_product_name] => Theme 10
            [st_product_description] => Theme 10 for testing purpose only.
            [st_product_image] => theme_10.jpg
        )
)

2 个答案:

答案 0 :(得分:1)

更新已解决的代码

$id = '';
while ($row_data = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($id == '' || $id != $row_data['in_order_id']) {
        $data = array(
            'in_order_id' => $row_data['in_order_id'],
            'in_order_date' => $row_data['in_order_date'],
            'in_discount' => $row_data['in_discount'],
            'in_total' => $row_data['in_total'],
            'in_grand_total' => $row_data['in_grand_total'],
            'st_cust_first_name' => $row_data['st_cust_first_name'],
            'st_cust_last_name' => $row_data['st_cust_last_name']
        );
    }

    $data['products'][] = array(
        'in_product_id' => $row_data['in_product_id'],
        'st_product_name' => $row_data['st_product_name'],
        'st_product_description' => $row_data['st_product_description'],
        'st_product_image' => $row_data['st_product_image']
    );

    $id = $row_data['in_order_id'];
}

print_r($data);

答案 1 :(得分:0)

尝试在下面的代码中进行更改,它会对您有所帮助。

function find_index(&$arr, $ind){
    if(empty($arr))
        return false;
    else {
        foreach($arr as &$aa){
            if($aa['id']==$ind['sub_id']) {
                $key=count($aa['sub_ar']);
                $aa['sub_ar'][$key]=$ind;
                $aa['sub_ar'][$key]['sub_ar']=array();
                return true;
            }
            else{
                $res=find_index($aa['sub_ar'],$ind);
                if($res)
                    return true;
            }
        }
        return false;
    }
}

$res=array();
foreach($data as $key=>$dat){
    if($dat['sub_id']==$dat['id']){
        $res[$key]=$dat;
        $res[$key]['sub_ar']=array();
    }
    else {
        $ret=find_index($res,$dat);
    }
}