PHP多维数组算法?

发布于 2024-12-16 21:20:34 字数 836 浏览 0 评论 0原文

我需要形成一个如下格式的多维数组,但是我无法弄清楚

array(
      [0]=>array(
            "id"=>"1","data"=>array([0]=>array("kid"=>"434","k"=>"Ali","m"=>"msj1"), [1]=>array("kid"=>"344","k"=>"Dali","m"=>"msj3")),
      [1]=>array(
            "id"=>"2","data"=>array([0]=>array("kid"=>"347","k"=>"Cenk","m"=>"msj2"), [1]=>array("kid"=>"345","k"=>"Tan","m"=>"msj4")))

数据来自 mysql 查询的算法,如下所示:

从表1中选择孩子,k,m,id,其中rid=1 ORDER BY(id)

示例数据:

id   kid   k    m
1    434  Ali  msj1
2    347  Cenk msj2
1    344  Dali msj3
2    345  Tan  msj4

php循环如下:

do {
//whatever I tried here failed :(
} while ($t = mysql_fetch_assoc($r_tav));

我希望通过这个示例我能更好地理解多维数组

I need to form an multidimensional array as below format however I could not figure out the algorithm

array(
      [0]=>array(
            "id"=>"1","data"=>array([0]=>array("kid"=>"434","k"=>"Ali","m"=>"msj1"), [1]=>array("kid"=>"344","k"=>"Dali","m"=>"msj3")),
      [1]=>array(
            "id"=>"2","data"=>array([0]=>array("kid"=>"347","k"=>"Cenk","m"=>"msj2"), [1]=>array("kid"=>"345","k"=>"Tan","m"=>"msj4")))

the data comes from mysql query like below:

SELECT kid, k, m, id FROM table1 WHERE rid=1 ORDER BY (id)

sample data:

id   kid   k    m
1    434  Ali  msj1
2    347  Cenk msj2
1    344  Dali msj3
2    345  Tan  msj4

php loop is as below:

do {
//whatever I tried here failed :(
} while ($t = mysql_fetch_assoc($r_tav));

I hope that I will understand multidimensional arrays better with this sample

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2024-12-23 21:20:34
$arrRows = mysql_fetch_array($r_tav);
$arrRowData = array();
$arrRowDataFinal = array();
foreach ($arrRows as $key => $value){
    $arrRowData[$value['id']][] = array("kid"=>$value['kid'],"k"=>$value['k'],"m"=>$value['m']);
}
foreach($arrRowData as $key => $value){
    $arrRowDataFinal[] = array('id' => $key, 'data' => $value);
}
$arrRows = mysql_fetch_array($r_tav);
$arrRowData = array();
$arrRowDataFinal = array();
foreach ($arrRows as $key => $value){
    $arrRowData[$value['id']][] = array("kid"=>$value['kid'],"k"=>$value['k'],"m"=>$value['m']);
}
foreach($arrRowData as $key => $value){
    $arrRowDataFinal[] = array('id' => $key, 'data' => $value);
}
浅浅淡淡 2024-12-23 21:20:34

看起来你想要的只是:

while ($t = mysql_fetch_assoc($r_tav)){
    $arr[] = $t;
}

并且 $arr 将包含你的数组

重新阅读问题后,它似乎不完全是你想要的结构,但它可以简单地变成:

while ($t = mysql_fetch_assoc($r_tav)){
    $arr[] = array('id' => $r_tav['id'],
                   'data' => array('kid' => $r_tav['kid'],
                                    'k' => $r_tav['k'],
                                    'm' => $r_tav['m']));
}

It seemed like all you want is:

while ($t = mysql_fetch_assoc($r_tav)){
    $arr[] = $t;
}

and $arr will contain your array

After re-reading the question, it seems that it is not exactly the structure you want, but it can simply be turned into:

while ($t = mysql_fetch_assoc($r_tav)){
    $arr[] = array('id' => $r_tav['id'],
                   'data' => array('kid' => $r_tav['kid'],
                                    'k' => $r_tav['k'],
                                    'm' => $r_tav['m']));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文