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 11 2516
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
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
"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
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
"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
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
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/
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
"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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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";
|
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...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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);
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |