473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dynamically adding to multidimensional array

$sql= sql query
$i=0;

while
($a2=mysql_fetch_array($a1)){array_push($temparray ,$a2[0],$a2['ad'],$a2[1],$a2[1]);
{
I want this array to be the value of an asssoc. array $results[$i]
$results[$i] =$temparray doesnt work
$results[$i] =$temparray[] doesnt work
$results[$i][] =$temparray doesnt work
$results[$i] =>$temparray doesnt work

then $i++;
}
so what does ?

Dec 16 '05 #1
4 12046
try so
$results[$i] .= array($temparray)

maybe work :)

Dec 16 '05 #2
pauld wrote:
$sql= sql query
$i=0;

while ($a2=mysql_fetch_array($a1)) {
array_push($temparray,$a2[0],$a2['ad'],$a2[1],$a2[1]);
{ I want this array to be the value of an asssoc. array $results[$i]
$results[$i] =$temparray doesnt work

<snip>

Why doesn't this work?
What's the error message?
What did you expect and what's the outcome?

Try inserting these two lines at the top of your script
ini_set('display_errors', '1');
error_reporting(E_ALL);

Dec 16 '05 #3
Thanks. adding the error reporting gives me a load of stuff about array
indiceds not defined. i thought PHP dynamically sorted out array
lengths. is there a 'better' way to do it than just let the scriptsort
it out ?

I want my results to end up as

results[0] => date,ID,value
results[1] => date,ID,value
results[2] => date,ID,value
etc etc

( there is probably a better way of resetting the temparray but ive
not found it !)

$i=0;
while ($a2=mysql_fetch_assoc($a1))
{$temparray=array();
array_push($temparray,$a2['ID'].$a2['date'],$a2['value']);
$results[$i] = array($temparray);
if (isset($temparray)) {reset($temparray);}
print '<br>'.$i.':';
print_r($results[$i]).'<br>';
$i=$i++;
}
gives

0:Array ( [0] => Array ( [0] => 2005-10-07 [1] => 350 [2] => 21 ) )

0:Array ( [0] => Array ( [0] => 2005-10-28 [1] => 473 [2] => NC2 ) )

0:Array ( [0] => Array ( [0] => 2005-10-03 [1] => 337 [2] => NC4 ) )

dont know why the $i counter is incrementing the array

Dec 20 '05 #4
pauld wrote:
is there a 'better' way to do it than just let the scriptsort
it out ?
Yes :)
I want my results to end up as

results[0] => date,ID,value
results[1] => date,ID,value
results[2] => date,ID,value
etc etc

( there is probably a better way of resetting the temparray but ive
not found it !)
see below (*)
$i=0;
while ($a2=mysql_fetch_assoc($a1))
{$temparray=array();
array_push($temparray,$a2['ID'].$a2['date'],$a2['value']);
$results[$i] = array($temparray);
if (isset($temparray)) {reset($temparray);}
print '<br>'.$i.':';
print_r($results[$i]).'<br>';
$i=$i++;

<snip>

Don't do this!
$var++ already increments it just by itself.
You really shouldn't assign that to the same var.

Try this (can you predict the output before running the script?):
<?php
$a = 1;
$a++;
echo $a, "<br/>\n";

echo $a++, "<br>\n";
$b = $a++;
echo $b, "<br>\n";
$c = ++$a;
echo $c, "<br>\n";
?>

(*) how to put the return from the database into the $results array

<?php
// ...
$results = array(); // initialize $results
while ($a2 = mysql_fetch_assoc($a1)) {
// this construct adds an element to the $results array
$results[] = array($a2['date'], $a2['ID'], $a2['value']);
}
// DEBUGGING
echo '<pre>'; print_r($results); echo '</pre>';
// ...
?>

Dec 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
11
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row =...
3
by: StevenT | last post by:
Hello, I am trying to dynamically create a table based on the information I have in my cookie for a shopping cart. I can create it and display it and all is good. I put the contents of the...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.