473,387 Members | 1,590 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,387 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 1975
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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 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.