开发者

How do you add to a new index on a Multi-dimensional array within a foreach loop?

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 />开发者_JAVA百科"; 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.


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);
        }   


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.


    //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);
    }               
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜