如何在 foreach 循环中添加到多维数组上的新索引?

发布于 2024-10-08 17:52:55 字数 2035 浏览 0 评论 0原文

再次,我想知道在 foreach 循环内如何将数据添加到新的数组索引?

我的 atm 代码是,

// Connect to the database to gather all data pertaiing to the link in question
$assoResult = mysql_query("SELECT * FROM associate_users");
while ($assoRow = mysql_fetch_field($assoResult)) {
    $resultArray[] = $assoRow->name;
}

// Connect to the database to gather all data pertaiing to the link in question
$assoResult2 = mysql_query("SELECT * FROM associate_users WHERE id='$getID'");
while ($assoRow2 = mysql_fetch_object($assoResult2)) {

    foreach ($resultArray as $row) { 
        $array = array(array( 1 => $assoRow2->$row, 2 => $row, ),);
        echo "<br />"; print_r($array);
    }               
}

下面是来自“echo "br />”; print_r($array);” 的输出数据线。

=================================================== ===============

Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [0] => Array ( [1] => Bob[2] => contactName ) )
Array ( [0] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [0] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [0] => Array ( [1] => XXXXXX [2] => postcode ) )

正如您所看到的,数组正在一遍又一遍地创建,我需要的是上述数据在每个循环上增加第一维索引键,所以看起来像...

============================================== =====================

Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [1] => Array ( [1] => Bob[2] => contactName ) )
Array ( [2] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [3] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [4] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [5] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [6] => Array ( [1] => XXXXXX [2] => postcode ) )

提前谢谢你,我没有办法让这个工作和绝望。

担。

Hell once again, I am wondering how do you go about adding data to a new array index when inside a foreach loop?

the code I have atm is,

// Connect to the database to gather all data pertaiing to the link in question
$assoResult = mysql_query("SELECT * FROM associate_users");
while ($assoRow = mysql_fetch_field($assoResult)) {
    $resultArray[] = $assoRow->name;
}

// Connect to the database to gather all data pertaiing to the link in question
$assoResult2 = mysql_query("SELECT * FROM associate_users WHERE id='$getID'");
while ($assoRow2 = mysql_fetch_object($assoResult2)) {

    foreach ($resultArray as $row) { 
        $array = array(array( 1 => $assoRow2->$row, 2 => $row, ),);
        echo "<br />"; print_r($array);
    }               
}

Below is the outputted data that comes from the "echo "br />"; print_r($array);" line.

=================================================================

Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [0] => Array ( [1] => Bob[2] => contactName ) )
Array ( [0] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [0] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [0] => Array ( [1] => XXXXXX [2] => postcode ) )

As you can see the array is being created a new over and over, what I need is for the above data to increment the 1st dimension index key on every loop, so it looks like...

=================================================================

Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [1] => Array ( [1] => Bob[2] => contactName ) )
Array ( [2] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [3] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [4] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [5] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [6] => Array ( [1] => XXXXXX [2] => postcode ) )

Thank you in advance I am out of all options in getting this to work and desperate.

Dan.

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

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

发布评论

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

评论(3

傾旎 2024-10-15 17:52:55

更改分配代码部分的值

    $count=0; 
    foreach ($resultArray as $row) { 
            $array[$count][1] = $assoRow2->$row
            $array[$count][2]=$row;
            $count++;
            echo "<br />"; print_r($array);
        }   

Change the values assigning part of your code to

    $count=0; 
    foreach ($resultArray as $row) { 
            $array[$count][1] = $assoRow2->$row
            $array[$count][2]=$row;
            $count++;
            echo "<br />"; print_r($array);
        }   
○闲身 2024-10-15 17:52:55

这段代码可以为您提供所需的输出,而无需低效地使用两个查询:

// Connect to the database to gather all data pertaining to the link in question
$result = mysql_query("SELECT * FROM associate_users WHERE id=" . (int)$getID);

$resultArray = array();
$resultCount = 0;
$row = mysql_fetch_assoc($result);

$count = 0;
foreach ($row as $key => $value) {
    $temp = array();
    $temp[$count] = array(1 => $value, 2 => $key);
    $count++;

    echo "<br />"; print_r($temp);
}

为什么您想要这样的输出,我不知道。

This code gets you the output you asked for without inefficiently using two queries:

// Connect to the database to gather all data pertaining to the link in question
$result = mysql_query("SELECT * FROM associate_users WHERE id=" . (int)$getID);

$resultArray = array();
$resultCount = 0;
$row = mysql_fetch_assoc($result);

$count = 0;
foreach ($row as $key => $value) {
    $temp = array();
    $temp[$count] = array(1 => $value, 2 => $key);
    $count++;

    echo "<br />"; print_r($temp);
}

Why you want it like this, I have no idea.

幸福%小乖 2024-10-15 17:52:55
    //extra code.declaring array
$array = array();
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
    foreach ($resultArray as $row) { 
// 1st parameter is the array name, here array name is array .give gd name according to use
       array_push($array,array( 1 => $assoRow2->$row, 2 => $row, ));
        echo "<br />"; print_r($array);
    }               
}
    //extra code.declaring array
$array = array();
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
    foreach ($resultArray as $row) { 
// 1st parameter is the array name, here array name is array .give gd name according to use
       array_push($array,array( 1 => $assoRow2->$row, 2 => $row, ));
        echo "<br />"; print_r($array);
    }               
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文