building arrays
am 28.08.2007 05:07:13 von pipe.jack
hello
Is there any simple method in PHP to create arrays from this database
table:
NAME | VALUE
nameA | 10
nameB | 12
nameB | 10
nameB | 10
nameC | 87
nameC | 56
....
so when I do print_r(nameB) I get:
nameB => array([0]=12, [1]=10, [2]=10)
thanks,
Jack
Re: building arrays
am 28.08.2007 05:19:51 von Steve
"JackpipE" wrote in message
news:1188270433.059195.109550@50g2000hsm.googlegroups.com...
| hello
|
| Is there any simple method in PHP to create arrays from this database
| table:
|
| NAME | VALUE
| nameA | 10
| nameB | 12
| nameB | 10
| nameB | 10
| nameC | 87
| nameC | 56
| ...
|
| so when I do print_r(nameB) I get:
| nameB => array([0]=12, [1]=10, [2]=10)
$example = array();
foreach ($records as $record)
{
$example[$record['NAME']][] = $record;
}
print_r($example['nameB']);
Re: building arrays
am 28.08.2007 09:20:40 von Toby A Inkster
JackpipE wrote:
> NAME | VALUE
> nameA | 10
> nameB | 12
> nameB | 10
> nameB | 10
> nameC | 87
> nameC | 56
$myarray = array();
while ($row = sql_fetch_row($results))
{
list($name, $value) = $row;
if (!isset($myarray[$name]))
$myarray[$name] = array();
$myarray[$name][] = $value;
}
extract($myarray);
Where sql_fetch_row() is whatever function is provided by your database to
retrieve an enumerated array -- e.g. pg_fetch_row() or mssql_fetch_row().
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 68 days, 10:49.]
TrivialEncoder/0.2
http://tobyinkster.co.uk/blog/2007/08/19/trivial-encoder/
Re: building arrays
am 28.08.2007 09:58:26 von gosha bine
On 28.08.2007 05:07 JackpipE wrote:
> hello
>
> Is there any simple method in PHP to create arrays from this database
> table:
>
> NAME | VALUE
> nameA | 10
> nameB | 12
> nameB | 10
> nameB | 10
> nameC | 87
> nameC | 56
> ...
>
> so when I do print_r(nameB) I get:
> nameB => array([0]=12, [1]=10, [2]=10)
>
> thanks,
> Jack
>
while($row = mysql_fetch_assoc(...))
$result[ $row['name'] ] []= $row['value'];
hope this helps ;)
--
gosha bine
makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Re: building arrays
am 28.08.2007 10:58:45 von Jerry Stuckle
JackpipE wrote:
> hello
>
> Is there any simple method in PHP to create arrays from this database
> table:
>
> NAME | VALUE
> nameA | 10
> nameB | 12
> nameB | 10
> nameB | 10
> nameC | 87
> nameC | 56
> ...
>
> so when I do print_r(nameB) I get:
> nameB => array([0]=12, [1]=10, [2]=10)
>
> thanks,
> Jack
>
Jack,
Are you sure you want a variable called $nameB? Or do you just wan an
array such as $data['nameB']?
Normally it's not a good idea to have variable names dependent on data,
but that's what it looks like you want.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: building arrays
am 28.08.2007 16:45:01 von pipe.jack
This is great stuff guys. Thank you.