Jump to content

how to get array indexes?


edg322

Recommended Posts

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

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?
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();//empty
foreach($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.
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.
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.