Reply to Re: Can't display column of multi-dimensional array

Your name:

Reply:


Posted by Ewoud Dronkert on 09/18/05 04:02

news@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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация