473,226 Members | 1,247 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,226 software developers and data experts.

Can't display column of multi-dimensional array

Using php.net I got this far, and so far so good!
But I can't fenagle it quite the way I need to.

I'm creating an array (which I didn't know how to do before today, so a
success for me *g*):

$a = array();
$a[0][0] = "Name";
$a[0][1] = $name;
$a[1][0] = "Message";
$a[1][1] = $msg;
$a[2][0] = "Subject";
$a[2][2] = $subject;

Then I'm feeding it through a pre-defined function (also which I'm
learning how to do, so in general I feel pretty good *g*):

foreach ($a as $v1) {
$fieldname = $v1;
foreach ($v1 as $v2) {
$returned = valid_field($fieldname,$v2);
if ($returned != "true") {
$error .= $returned;
$errflag = "1";
}
}
}

That's the last way I tried. I also tried without the "$fieldname =
$v1;" and using eith $a and $v1 in the first of the two valid_field()
variables.

In all cases, what gets echoed back are: "array" and the correct value
of the second column:

"array", $name
"array", $msg
"array", $subject

If someone could point me to a clue, don't give me the answer, just a
push in the right direction for me to figure it out...because I can't
think of what to try.

Thanks!
Liam

Sep 17 '05 #1
5 1970
I noticed that Message-ID:
<11**********************@f14g2000cwb.googlegroups .com> from
ne**@celticbear.com contained the following:
foreach ($a as $v1) {
$fieldname = $v1; $v1 is an array and so $fieldname is also an array foreach ($v1 as $v2) {
$returned = valid_field($fieldname,$v2); we don't know what your function does if ($returned != "true") {
$error .= $returned;
$errflag = "1";
}
}
}


Try setting up a simple multidimensional array and echoing it to screen.
Then add your checking function when you understand what's going on.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Sep 18 '05 #2

Geoff Berrow wrote:
Try setting up a simple multidimensional array and echoing it to screen.
Then add your checking function when you understand what's going on.


Is this not a "simple multidimensional array"? (Wouldn't surprise me if
it's not.

OK, here's all of it, plus a bit of code to show what's in the array.
Using that' I've tried to manipulate it to match the a with the b, but
it's all coming out as individual items.
That made no sense.

$a = array();
$a[0][0] = "Name";
$a[0][1] = $name;
$a[1][0] = "Message";
$a[1][1] = $msg;
$a[2][0] = "Subject";
$a[2][2] = $subject;

function valid_field($field,$value)
{
// return FALSE if it contains characters which
// which AREN'T on the specified list
if(ereg("[^[:space:]a-zA-Z0-9_.-\!\\'\,]{1,}", $value))
{
$funcerror = $field." value of: <span
class='bolding'>\"".$value."\"</span> contains invalid characters.<br
/>";
return $funcerror;
}
else
{
return true;
}
}

while (list ($x, $tmp) = each ($a)) {
while (list ($y, $val) = each ($tmp)) {
echo "$x, $y, $val<br />";
}
}
foreach ($a as $v1) {
$fieldname = $v1;
foreach ($v1 as $v2) {
$returned = valid_field($fieldname,$v2);
if ($returned != "true") {
$error .= $returned;
$errflag = "1";
}
}
}
The while loop puts out the following:
0, 0, Name
0, 1, Liam
1, 0, Message
1, 1, Hello, this is a test msg.
2, 0, Subject
2, 2, It's a test subject.

Sep 18 '05 #3
ne**@celticbear.com wrote:
$a = array();
$a[0][0] = "Name";
$a[0][1] = $name;
$a[1][0] = "Message";
$a[1][1] = $msg;
$a[2][0] = "Subject";
$a[2][2] = $subject;
I guess you mean [2][1] on the last line. A more instructive array
definition with the same result:

$a = array(
array('Name', $name),
array('Message', $msg),
array('Subject', $subj)
);
foreach ($a as $v1) {
$fieldname = $v1;


As you can see above, $a[0] is an array containing two items: 'Name' and
$name. So $v1 (an alias for $a[0], $a[1], etc.) is not the fieldname, but
$v1[0] is. A better way would be:

foreach ($a as $field) {
list($fieldname, $fieldvalue) = $field;

Or maybe even:

$a = array(
array('name' => 'Name', 'value' => $name),
array('name' => 'Message', 'value' => $msg),
array('name' => 'Subject', 'value' => $subj)
);
foreach ($a as $field)
echo "{$field['name']} = {$field['value']}<br />\n";

--
E. Dronkert
Sep 18 '05 #4
Thanks for the help!
I went this way:

$a = array(
array('name' => 'Name', 'value' => $name),
array('name' => 'Message', 'value' => $msg),
array('name' => 'Subject', 'value' => $subj)
);

foreach ($a as $v1) {
$a1 = $v1['name'];
$a2 = $v1['value'];
$returned = valid_field($a1,$a2);
if ($returned != "true") {
$error .= $returned;
$errflag = "1";
}
}

I get that; that makes sense to me.
I was just about the write how I still don't "get" the LIST version,
and when I went to copy-n-paste it into here...I got it.
The relationship of the $field, which is the next iteration of the
array, and the LIST parses the twho columns, right?

Well, in any case, that works AND I get it!. =)
Much appreciated!!
Liam

Sep 18 '05 #5
ne**@celticbear.com wrote:
Thanks for the help!
I went this way:

$a = array(
array('name' => 'Name', 'value' => $name),
array('name' => 'Message', 'value' => $msg),
array('name' => 'Subject', 'value' => $subj)
); [snip]

In this case there is no need to use a multidimensional array, as
PHP's arrays are maps, which has many purposes.

For the above, you might as well use:
$a = array(
'Name' => $name),
'Message' => $msg),
'Subject' => $subj)
);

[snip] The relationship of the $field, which is the next iteration of the
array, and the LIST parses the twho columns, right?


You can say that. An important note is that "list" should be used only
when the array is sequently indexed like an ordinary array or vector.
Each "argument"(variable) passed to "list" will given the value of
respectively the 1st(indexed zero), 2nd, 3rd and so forth, value in
the array.
I.e. given the array:
$arr = array('a', 'b', 'c', 7 => 'd');

This:
list($a, $b, $c) = $arr;

is equivalent to:
$a = $arr[0];
$b = $arr[1];
$c = $arr[2];

This:
list($a, $b, $c, $d) = $arr;

will give a warning for undefined index, because there is no $arr[3].
/Bent
Sep 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Matthias Braun | last post by:
Help! Probably it is too easy, but I am not so experienced in MySQL up to now. I have two tables table1&table2 with the following columns: col1 double, col2 int(11), col3 date I want to...
5
by: Jim | last post by:
Now this is just weird. And yes, I validated the html and the stylesheet. I created a transparent gif image in Paint Shop Pro 7. I made the mistake of making a transparent png and went along...
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
1
by: Greg Heilers | last post by:
Once again, I am unsure about the solution to multi-column layouts; and compensating for browser quirks. Here is an example: http://geocities.com/greg_heilers/animals.html I basically use a...
3
by: Bob | last post by:
Thinking two things, 1- Creating a userControl -yeah you guessed it, a multi column drop down combobox - I've looked at several articles and did not find what I need, one that's bindable and that...
2
by: Mike TI | last post by:
March 26, 2006 Hi All I am new to VB.Net, using VB.Net 2005 Is there a way to have more than one column in a combo box. Thanks
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
4
by: rszebras | last post by:
I inherited a database (as a novice at Access) and need to modify it to make it more efficient, i.e., the assignment form needs to autopopulate with the client's name, address, phone number, etc.,...
0
by: mahesh.nimbalkar | last post by:
I want to use Multi column sorting in DataGridView. The scenario is like this: 1) DataGridView has two columns; Id and name 2) User clicks on Id column and DataGridView is sorted...
1
by: Sonny | last post by:
Hi, Would like to know the performance differenece between Multi-column Index vs Single Column Indexes. Let's say I have a table with col1, col2, col3 along with a primary key column and...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
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, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
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...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.