Jump to content


Photo

multi-dimensional arrays


  • Please log in to reply
3 replies to this topic

#1 edg322

edg322
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 18 October 2006 - 02:17 PM

Hello, I have a multi-dim array, $a which produces the following var_export($a):

array (
  'some_ID' => 5,
  'Create_Date' => '2004-02-20 15:18:13',
  'some_Name' => 'foo',
  'some_Description' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
  'Priority' => 1,
  'some_URL' => 'http://www.fooname.com'
  'Featured' => 1,
  'Admin_ID' => 30,
  'Status' => 'A',
  'Last_Update' => '2004-08-31 11:29:27',
)

array (
  'some_ID' => 16,
  'Create_Date' => '2000-06-14 10:10:36',
  'some_Name' => 'bar',
  'some_Description' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
  'Priority' => 2,
  'some_URL' => 'http://www.bardomain.com'
  'Featured' => 0,
  'Admin_ID' => NULL,
  'Status' => 'A',
  'Last_Update' => '2003-08-18 15:43:22',
)

array (
  'some_ID' => 19,
  'Create_Date' => '2006-06-19 11:15:59',
  'some_Name' => '555',
  'some_Description' => '555',
  'Priority' => 555,
  'some_URL' => '555',
  'Featured' => 0,
  'Admin_ID' => NULL,
  'Status' => 'A',
  'Last_Update' => '2006-10-17 10:28:32',
)

array (
  'some_ID' => 20,
  'Create_Date' => '2006-06-20 11:36:01',
  'some_Name' => 'anotherfoo',
  'some_Description' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
  'Priority' => 7,
  'some_URL' => 'http://www.anotherfoo.com/'
  'Featured' => 0,
  'Admin_ID' => NULL,
  'Status' => 'A',
  'Last_Update' => '2005-02-18 15:43:22',
)

My question is:  How do I iterate through and only extract the value of 'some_ID' and place that into a table?

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 02:24 PM

You need a foreach loop...

<?php
foreach ($a as $key => $value){
   // Do something with some_ID
   echo "{$a[$key]['some_ID']}<br>\n";
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 edg322

edg322
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 18 October 2006 - 02:27 PM

rockin, thx huggie

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 02:34 PM

Try something like this for your table...

<?php
// Open the table
echo "<table width=\"300\" cellspacing=\"0\" cellpadding=\"3\" border=\"1\">";

// Loop through each instance in the array
foreach ($a as $key => $value){
   echo "<tr><td width=\"300\" align=\"center\">{$a[$key]['some_ID']}</td></tr>\n"; // Echo the table rows
}

// Close off the table
echo "</table>\n";
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users