edg322 Posted November 7, 2006 Share Posted November 7, 2006 I have an array like this:[code]Array( [0] => Array ( [name] => Caroline [description] => Description of Caroline [url] => http://ww.caroline.com [name] => Caroline.jpg [path] => /path/to/ ) [1] => Array ( [name] => Eric [description] => description of Eric [url] => http://ww.eric.com [name] => Eric.gif [path] => /path/to/ ) [2] => Array ( [name] => Tony [description] => This is a description [url] => http://ww.Tony.com [name] => Tony.jpg [path] => /path/to/ ) [3] => Array ( [name] => Jodi [description] => description of Jodi [url] => http://ww.jodi.com/ [name] => Jodi.jpg [path] => /path/to/ ))[/code]How can I get extract those 0, 1, 2, and 3 values? I want to ultimately use these for placement of each Array item into a 2-column table. Thanks! Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/ Share on other sites More sharing options...
edg322 Posted November 7, 2006 Author Share Posted November 7, 2006 The array comes out of a database. What I've done is counted the number of results returned. What I want to do is run a foreach to create a table that prints out the contents of each record into a row. however, the trick is that I want to have 2 columns. So the 1st column, on the left, shows half the records, and the 2nd column shows the other half. If the results are odd, then the first column should be just 1 longer than the 2nd. Any suggestions on how to specify how the table gets printed out in a foreach? Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-120978 Share on other sites More sharing options...
DrDre Posted November 7, 2006 Share Posted November 7, 2006 So your asking how to split the array into 2 columns? You can easily do this by toggling a boolean or something in a for statement like this[code=php:0]<?$array=$thearraydata;$newarray=array();//emptyforeach($array as $id => $data) { $switch=($switch != 0 ? 0 : 1);//If $switch != 0,$switch=0, else $switch=1 $newarray[$switch][$id]=$data;}$leftcol=$newarray[0];$rightcol=$newarray[1];?>Now your arrays will look like$leftcol = Array( [0] => Array ( [name] => Caroline [description] => Description of Caroline [url] => http://ww.caroline.com [name] => Caroline.jpg [path] => /path/to/ ) [2] => Array ( [name] => Tony [description] => This is a description [url] => http://ww.Tony.com [name] => Tony.jpg [path] => /path/to/ ))$rightcol=Array( [1] => Array ( [name] => Eric [description] => description of Eric [url] => http://ww.eric.com [name] => Eric.gif [path] => /path/to/ ) [3] => Array ( [name] => Jodi [description] => description of Jodi [url] => http://ww.jodi.com/ [name] => Jodi.jpg [path] => /path/to/ ))And you can use the elements as $leftcol[0]['name'], $leftcol[2]['name']or like [code=php:0]<table><tr><td><?foreach($leftcol as $data) { echo '<a href="'.$data['url'] .'" alt="'. $data['description'] . '">' . $data['name'] . "</a><br>\n";}?></td><td><?foreach($rightcol as $data) { echo '<a href="'.$data['url'] .'" alt="'. $data['description'] . '">' . $data['name'] . "</a><br>\n";}?></td></tr></table>[/code]Or however you plan to use it.Yes it could be done simpler, I layed it out like this so it would be easier to understand what exactly it is doing. Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-120998 Share on other sites More sharing options...
edg322 Posted November 7, 2006 Author Share Posted November 7, 2006 Thanks, that seems to do the trick.Where can I learn more about what you did in your line[code] $switch=($switch != 0 ? 0 : 1);//If $switch != 0,$switch=0, else $switch=1[/code]That's really powerful!Thanks DrDre. Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-121151 Share on other sites More sharing options...
edg322 Posted November 7, 2006 Author Share Posted November 7, 2006 nvm I found some info on it. Called a ternary operator if anyone is interested ...Thanks again Dre Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-121179 Share on other sites More sharing options...
doni49 Posted November 8, 2006 Share Posted November 8, 2006 Here's another way to do pretty much the same thing. I only offer it as a short example of thinking "outside the box".[code]$switch = abs($switch - 1);[/code]If $switch is already 1 then it's the same as saying [b]$switch = 1 - 1//returns zero[/b] and if $switch is already 0 then it's the same as saying [b]$switch = 0 - 1//returns -1 (negative 1)[/b] and then abs changes any negative number to positive. It's something that I've used for years in pretty much every language I've ever used--in one form or another. Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-121326 Share on other sites More sharing options...
doni49 Posted November 8, 2006 Share Posted November 8, 2006 Here's something to boild it down to the basics.[code]echo "<table>";foreach ($arr as $id => $data){ if(is_int($id/2)){echo "<tr>"; $endRow = FALSE;} //if is_int($id/2) is TRUE, then you're on an odd field so start the row. echo '<td><a href="'.$data['url'] .'" alt="'. $data['description'] . '">' . $data['name'] . "</a></td>"; if(!is_int($id/2)){echo "</tr>"; $endRow = TRUE;} //if is_int($id/2) is FALSE, then you're on an even field so end the row. }if(!$endRow){echo "</tr>";}echo "</table>";[/code]is_int($id/2) // another "think outside the box" example. If you divde something by 2 and it returns an integer, then it's NOT a decimal value and therefore the original number was even. Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-121330 Share on other sites More sharing options...
edg322 Posted November 9, 2006 Author Share Posted November 9, 2006 Thanks a lot doni! I like your abs switch and will try out the way you've built the table. Link to comment https://forums.phpfreaks.com/topic/26454-how-to-get-array-indexes/#findComment-121926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.