You are here: Re: dynamically adding to multidimensional array « PHP Programming Language « IT news, forums, messages
Re: dynamically adding to multidimensional array

Posted by Mara Guida on 12/20/05 16:32

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>';
// ...
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация