Php 需要创建多维数组吗
我已经尝试了下面的代码来制作多维数组。但它不会创建我想要的数组Php 需要创建多维数组吗,php,arrays,multidimensional-array,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 = 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
)
)
尝试在下面的代码中进行更改,这将对您有所帮助
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);
}
}
更新的已解决代码
$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);
“请解决我的问题”?到目前为止你试过什么?:)所以这不是免费的编码服务。请试着自己解决这个问题。如果你不能让它工作,张贴你尝试,我们将帮助你修复它。如果你只是乞求别人为你工作,你就永远不会成为一名真正的程序员。@Barmar:如果我能解决它,那么我就不会把我的问题放在这上面。谢谢,很明显。但你仍然可以尝试解决它。你是一名计算机程序员,那是你的工作。@Barmar:我终于解决了。谢谢你提醒我我的技能让我试试。谢谢。如果我的答案对你有用,那么请将其标记为正确,以帮助面临相同问题的其他人。很乐意帮忙。