469,908 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

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 11866
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Charles Banas | last post: by
1 post views Thread by Mark Smith | last post: by
2 posts views Thread by chris | last post: by
2 posts views Thread by xhunga | last post: by
11 posts views Thread by dennis.sprengers | last post: by
5 posts views Thread by LittleCake | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.