473,233 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

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

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
11 2509

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
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
"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
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
"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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
2
by: Zenobia | last post by:
I have a problem. I need to look up several values stored in arrays. Each value is stored as a pair. The first (number) represents the probability of this item occurring. For instance, in the...
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.