Mohit
Mohit

Reputation: 1234

How to show the array content in php

I have an array which looks like:

Array
(
    [0] => Array
        (
            [total words] => 1476
        )

    [1] => Array
        (
            [keyword] => difference
            [count] => 82
            [percent] => 5.56
        )

    [2] => Array
        (
            [keyword] => 2010
            [count] => 37
            [percent] => 2.51
        )

    [3] => Array
        (
            [keyword] => very
            [count] => 22
            [percent] => 1.49
        )

)

I want to show the array content in a table of three column and three rows. Each row contains keyword, count and percent as a column and Total. Words will be shown in table caption.

Please help me ! I'm trying to run a for loop but don't know how to show the array content, because it looks like a multi dimensional array. Please help me.

Upvotes: 0

Views: 4493

Answers (6)

SawnOffLockie
SawnOffLockie

Reputation: 1

Further to Godea Andrei's answer above, if you are using print_r just use

    print_r($array)

Calling array_values within the print_r call will strip off all associative naming of the array elements. If just using numerical array indexes it will still show the index value. E.g.

    $a = array("red", "green", "blue");

print_r($a) will display

    Array ( [0] => red [1] => green [2] => blue )

whereas

    $b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

print_r($b) will display

    Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )

If using array_values the output of the second example would be

    Array ( [0] => 35 [1] => 37 [2] => 43 )

Upvotes: 0

Godea Andrei
Godea Andrei

Reputation: 55

array_values — Return all the values of an array

print_r(array_values($array));

Upvotes: 0

Samuel Herzog
Samuel Herzog

Reputation: 3611

you can iterate through your array using a foreach($array => $value) loop.
this code should do the trick:

<table>
<tr>
    <th>Keyword</th>
    <th>Count</th>
    <th>%</th>
</tr>
<?php foreach ( $data as $row ): ?>
<tr>
    <td><?php echo $row['keyword']; ?></td>
    <td><?php echo $row['count']; ?></td>
    <td><?php echo $row['percent']; ?></td>
</tr>
<?php endforeach; ?>
</table>

Upvotes: 1

Dirk
Dirk

Reputation: 2207

Lets say array is stored in variable $a.

foreach($item in $a) {
  echo $item['keyword'] . " " . $item['count'] . " " .  $item['percent'] . "<br>\n";
}

Upvotes: 0

Jason Benson
Jason Benson

Reputation: 3399

Implode is your friend in this case:

$arrayLength = count($myArray);

echo '<table>';

for($i=0;$i<$arrayLength;$i++){

   echo '<tr><td>'.
       .implode('</td><td>',$myArray[$i])
       .'</td></tr>';

}
echo '</table>';

https://www.php.net/manual/en/function.implode.php

Upvotes: 1

Matt Lowden
Matt Lowden

Reputation: 2616

This should be what you're after.

print '<table>';
$headers = array_keys(reset($array));

print '<tr>';
foreach($headers as $header){
    print '<th>'.$header.'</th>';
}
print '<tr>';

foreach($array as $row){
    print '<tr>';
    foreach($row as $col){
        print '<td>'.$col.'</td>';
    }
    print '</tr>';
}
print '</table>';

Upvotes: 2

Related Questions