Connecting Tech Pros Worldwide Forums | Help | Site Map

building arrays

JackpipE
Guest
 
Posts: n/a
#1: Aug 28 '07
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


Steve
Guest
 
Posts: n/a
#2: Aug 28 '07

re: building arrays



"JackpipE" <pipe.jack@gmail.comwrote in message
news:1188270433.059195.109550@50g2000hsm.googlegro ups.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']);


gosha bine
Guest
 
Posts: n/a
#3: Aug 28 '07

re: building arrays


On 28.08.2007 05:07 JackpipE wrote:
Quote:
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
Toby A Inkster
Guest
 
Posts: n/a
#4: Aug 28 '07

re: building arrays


JackpipE wrote:
Quote:
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/0...ivial-encoder/
Jerry Stuckle
Guest
 
Posts: n/a
#5: Aug 28 '07

re: building arrays


JackpipE wrote:
Quote:
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
==================
JackpipE
Guest
 
Posts: n/a
#6: Aug 28 '07

re: building arrays


This is great stuff guys. Thank you.


Closed Thread