By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Associative Array with ID as Key and other associative array as value?

P: n/a
Hi,

I want to create an associative Array with a PHP variable (article ID)
as Key
and another associative array as it's value.

How do I instanciate it, how can I fill it?

I want something like this:

$articleTable= array( $articleNr => array("name" => $name, "price " =>
$price ,"amount" => 0 );

( Amount will be filled later in a form.)

At the end I want an Array that contains data coming from the
database,
something like this:

ArticleNr ArticleName Price Amount
12345 tooth bruth 10 3
24939 stuff 2 1
19492 tooth paste 8 0
29491 water 1 4
So how can this be done?

Thanks,

Stefan
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

Stefan Richter wrote:
Hi,

I want to create an associative Array with a PHP variable (article ID) as Key
and another associative array as it's value.

How do I instanciate it, how can I fill it?

I want something like this:

$articleTable= array( $articleNr => array("name" => $name, "price " => $price ,"amount" => 0 );


You can do it like you show, except you need another closing ")". Or
you can do it like this:

$articleTable[$articleNr]['name'] = $name;
$articleTable[$articleNr]['price'] = $price;
$articleTable[$articleNr]['amount'] = 0;

While you're debugging it, use
echo '<pre>';print_r($articleTable);echo '</pre>';
to see the contents.

Ken

Jul 17 '05 #2

P: n/a
Stefan Richter wrote:
Hi,

I want to create an associative Array with a PHP variable (article ID)
as Key
and another associative array as it's value.

How do I instanciate it, how can I fill it?

I want something like this:

$articleTable= array( $articleNr => array("name" => $name, "price " =>
$price ,"amount" => 0 );

( Amount will be filled later in a form.)

At the end I want an Array that contains data coming from the
database,
something like this:

ArticleNr ArticleName Price Amount
12345 tooth bruth 10 3
24939 stuff 2 1
19492 tooth paste 8 0
29491 water 1 4
So how can this be done?

Thanks,

Stefan

Hi Stefan,

That can be done in many ways in PHP.
I often use similar construct to store some databasevalues in a ASSOC array,
that needs to have some formatting so other functions can grok it.

Here is an example (postgresql style, but you can probably adapt it easily)

$myArticles = array();

$SQL = "SELECT ArticleID, ArticleNr, ArticleName, Price, Amount FROM
tblarticles";
$RS = pg_exec($connection,$SQL);
$numrows = pg_numrows($RS);
for ($i=0;$i<$numrows;$i++) {
$row = pg_fetch_array($RS,$i,PGSQL_ASSOC);
$thisrow = array();
$thisrow["ArticleID"] = $row["ArticleID"];
$thisrow["ArticleNr"] = $row["ArticleNr"];
$thisrow["ArticleName"] = $row["ArticleName"];
$thisrow["Price"] = $row["Price"];
$thisrow["Amount"] = $row["Amount"];

// Now add to $myArticles
$myArticles["ID".$thisrow["ArticleID"]] = $thisrow;
}

// From here on $myArticles contains many arrays
// stored under the name IDxxx where xxx is articleID
Of course the above example is plain stupid, but it shows how to populate an
array with arrays, using the ArticleID as key.

Hope this helps you.
Tip: Also have a look at foreach at php.net to easily loop over ASSOC arrays
containing arrays, retrieving the key.

foreach (array_expression as $key => $value) {
// $key now contains ID3423
// $value now is one of the arrays $thisrow
}

Good luck,

Regards,
Erwin Moller
Jul 17 '05 #3

P: n/a
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:42***********************@news.xs4all.nl...
: > So how can this be done?
: >
: > Thanks,
: >
: > Stefan
:
:
: Hi Stefan,
:
: That can be done in many ways in PHP.
: I often use similar construct to store some databasevalues in a ASSOC
array,
: that needs to have some formatting so other functions can grok it.
:
: Here is an example (postgresql style, but you can probably adapt it
easily)
:
: $myArticles = array();
:
: $SQL = "SELECT ArticleID, ArticleNr, ArticleName, Price, Amount FROM
: tblarticles";
: $RS = pg_exec($connection,$SQL);
: $numrows = pg_numrows($RS);
: for ($i=0;$i<$numrows;$i++) {
: $row = pg_fetch_array($RS,$i,PGSQL_ASSOC);
: $thisrow = array();
: $thisrow["ArticleID"] = $row["ArticleID"];
: $thisrow["ArticleNr"] = $row["ArticleNr"];
: $thisrow["ArticleName"] = $row["ArticleName"];
: $thisrow["Price"] = $row["Price"];
: $thisrow["Amount"] = $row["Amount"];
:
: // Now add to $myArticles
: $myArticles["ID".$thisrow["ArticleID"]] = $thisrow;
: }

Or even
while ($row = pg_fetch_array($RS, $i, PGSQL_ASSOC))
{
foreach ($row as $fld=>$val)
{
if ($fld == 'ArticleID') next;
$myArticles[$row['ArticleID']][$fld] = $val;
}
}

:
: // From here on $myArticles contains many arrays
: // stored under the name IDxxx where xxx is articleID
:
:
: Of course the above example is plain stupid, but it shows how to populate
an
: array with arrays, using the ArticleID as key.

Just a bit clunky!
:
: Hope this helps you.
: Tip: Also have a look at foreach at php.net to easily loop over ASSOC
arrays
: containing arrays, retrieving the key.
:
: foreach (array_expression as $key => $value) {
: // $key now contains ID3423
: // $value now is one of the arrays $thisrow
: }

Why not do the same when populating the array?

Matt
Jul 17 '05 #4

P: n/a
Matt Mitchell wrote:
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:42***********************@news.xs4all.nl...
: > So how can this be done?
: >
: > Thanks,
: >
: > Stefan
:
:
: Hi Stefan,
:
: That can be done in many ways in PHP.
: I often use similar construct to store some databasevalues in a ASSOC
array,
: that needs to have some formatting so other functions can grok it.
:
: Here is an example (postgresql style, but you can probably adapt it
easily)
:
: $myArticles = array();
:
: $SQL = "SELECT ArticleID, ArticleNr, ArticleName, Price, Amount FROM
: tblarticles";
: $RS = pg_exec($connection,$SQL);
: $numrows = pg_numrows($RS);
: for ($i=0;$i<$numrows;$i++) {
: $row = pg_fetch_array($RS,$i,PGSQL_ASSOC);
: $thisrow = array();
: $thisrow["ArticleID"] = $row["ArticleID"];
: $thisrow["ArticleNr"] = $row["ArticleNr"];
: $thisrow["ArticleName"] = $row["ArticleName"];
: $thisrow["Price"] = $row["Price"];
: $thisrow["Amount"] = $row["Amount"];
:
: // Now add to $myArticles
: $myArticles["ID".$thisrow["ArticleID"]] = $thisrow;
: }

Or even
while ($row = pg_fetch_array($RS, $i, PGSQL_ASSOC))
{
foreach ($row as $fld=>$val)
{
if ($fld == 'ArticleID') next;
$myArticles[$row['ArticleID']][$fld] = $val;
}
}
Yes yes, I know!
I told you it was a stupid example.

But in your example you make an exact copy of the row, while I was merely
showing the idea.
By doing it step by step you can also add other stuff to it.
(That is how I use it)


:
: // From here on $myArticles contains many arrays
: // stored under the name IDxxx where xxx is articleID
:
:
: Of course the above example is plain stupid, but it shows how to
: populate
an
: array with arrays, using the ArticleID as key.

Just a bit clunky!
But clear I hope. :P
:
: Hope this helps you.
: Tip: Also have a look at foreach at php.net to easily loop over ASSOC
arrays
: containing arrays, retrieving the key.
:
: foreach (array_expression as $key => $value) {
: // $key now contains ID3423
: // $value now is one of the arrays $thisrow
: }

Why not do the same when populating the array?
Erm...
We ARE populating arrays. :P

Matt

Regards,
Erwin Moller
Jul 17 '05 #5

P: n/a
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:42***********************@news.xs4all.nl...
: > : for ($i=0;$i<$numrows;$i++) {
: > : $row = pg_fetch_array($RS,$i,PGSQL_ASSOC);
: > : $thisrow = array();
: > : $thisrow["ArticleID"] = $row["ArticleID"];
: > : $thisrow["ArticleNr"] = $row["ArticleNr"];
: > : $thisrow["ArticleName"] = $row["ArticleName"];
: > : $thisrow["Price"] = $row["Price"];
: > : $thisrow["Amount"] = $row["Amount"];
: > :
: > : // Now add to $myArticles
: > : $myArticles["ID".$thisrow["ArticleID"]] = $thisrow;
: > : }
: >
: > : foreach (array_expression as $key => $value) {
: > : // $key now contains ID3423
: > : // $value now is one of the arrays $thisrow
: > : }
: >
: > Why not do the same when populating the array?
:
: Erm...
: We ARE populating arrays. :P

Erm, it was a reference to hard-coding the contents of the array, rather
than looping through it...

Matt "Erm" Mitchell
Jul 17 '05 #6

P: n/a
Matt Mitchell wrote:
"Erwin Moller"
: >
: > Why not do the same when populating the array?
:
: Erm...
: We ARE populating arrays. :P

Erm, it was a reference to hard-coding the contents of the array, rather
than looping through it...
Ok.
I think we confusing the original poster now. :-)

Matt "Erm" Mitchell


:-)

Regards,
Erwin "Erm too" Moller

Jul 17 '05 #7

P: n/a
JDS
On Wed, 23 Feb 2005 08:00:42 -0800, Stefan Richter wrote:
I want something like this:

$articleTable= array( $articleNr => array("name" => $name, "price " =>
$price ,"amount" => 0 );


Well that should work just fine (except for a syntax error). What is the
trouble?

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #8

P: n/a
JDS
On Wed, 23 Feb 2005 18:00:55 +0100, Erwin Moller wrote:
Ok.
I think we confusing the original poster now. :-)


<ha!>
You forgot to mention bi-spline combinatorial population of arrays as a
possible solution. By taking the octal root of the spline, one can
generate multi-layered, multi-dimensional sub-arrays that conform to
international spec IEEAC-091499, are manageable within tri-polar dihedral
clusters, can autonomously recombine into sub-sub-arrays, and also look
real nice.
</ha!>

Confused now?
--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #9

P: n/a
JDS wrote:
On Wed, 23 Feb 2005 18:00:55 +0100, Erwin Moller wrote:
Ok.
I think we confusing the original poster now. :-)


<ha!>
You forgot to mention bi-spline combinatorial population of arrays as a
possible solution. By taking the octal root of the spline, one can
generate multi-layered, multi-dimensional sub-arrays that conform to
international spec IEEAC-091499, are manageable within tri-polar dihedral
clusters, can autonomously recombine into sub-sub-arrays, and also look
real nice.
</ha!>

Confused now?


Wow!
Yes, I am confused now.

I have totally 100% NO-CLUE what you are talking about.
:P

But I must admit: It sounds REALLY SOPHISTICATED!

Maybe you can get a patent on that and sell it to M$?
(Please give me 1% idiotfee)

Thanks. :P

Regards,
Erwin Moller
Jul 17 '05 #10

P: n/a
"JDS" <je*****@example.invalid> wrote in message
news:pa****************************@example.invali d...
: > Ok.
: > I think we confusing the original poster now. :-)
:
: <ha!>
: You forgot to mention bi-spline combinatorial population of arrays as a
: possible solution. By taking the octal root of the spline, one can
: generate multi-layered, multi-dimensional sub-arrays that conform to
: international spec IEEAC-091499, are manageable within tri-polar dihedral
: clusters, can autonomously recombine into sub-sub-arrays, and also look
: real nice.
: </ha!>

Ah, but these are much more effective when coded in Intercal... And don't
forget, PHP *still* hasn't implemented the "COME FROM" statement.

:
: Confused now?

LMFAO!

Seriously though, it's just that so many people posting in here seem to be
unaware of constructs like foreach http://www.php.net/foreach and the perl
regex stuff http://www.php.net/pcre , that contain much of the power of PHP
to make clean, easily maintainable, elegant code.

These were some of the things I really liked about Perl and PHP when I
started. Otherwise, it really *would* make more sense to code things in
VBScript...

Matt
Jul 17 '05 #11

P: n/a
JDS
On Wed, 23 Feb 2005 18:46:54 +0000, Matt Mitchell wrote:
Seriously though, it's just that so many people posting in here seem to be
unaware of constructs like foreach http://www.php.net/foreach and the perl


Yes. Seriously. How often do we have to see "How do I print all the
elements in an array?" Read a friggin book, hoss!

Or please please please Google:
<http://www.google.com/search?hl=en&lr=&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=php+How+do+I+print+all+the+element s+in+an+array%3F&btnG=Search>

wheeeeeeeeeeeeeeeeee

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.