0

I have obtained an array output from a curl command. Now, I need to place that output in a table. I have written the entire code in php. The .phtml part is:

echo "<table border='1'>


<tr><td>NAME</td></tr>
                <tr><td>COUNTRY</td></tr>
                </table>";

The loop part is:

foreach($arr['fruits'] as $key=>$fruit) {
                ?>
             <tr><td><?php echo $fruit->NAME["Name"];}?></td></tr>
             <tr><td><?php echo $fruit->COUNTRY["Country"];}?></td></tr>

I'm very very new to all this. So, I don't really know what I'm exactly doing. Thanks in advance...

6
  • where is the question? any errors? Commented Jul 17, 2013 at 11:33
  • 1
    So $arr is an array of objects, with properties that are associative arrays? Commented Jul 17, 2013 at 11:34
  • yea, I get parse errors Commented Jul 17, 2013 at 11:34
  • yes, its an array of objects Commented Jul 17, 2013 at 11:34
  • 1
    Why you used } in each td tag ? Commented Jul 17, 2013 at 11:34

3 Answers 3

1

Use it like this :

Considering your array structure is like this :

$your_array   = array(array("name"=>"prasanth","country"=>"India"),
                      array("name"=>"bendra","country"=>"India"),
                      array("name"=>"User","country"=>"US")
                     );

<?php foreach($your_array as $key=>$fruit) { ?>
    <tr><td><?php echo $fruit['name'];?></td></tr>
    <tr><td><?php echo $fruit['country'];?></td></tr>
<?php } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Well, I get error that "Cannot use object of type stdClass as array"
0

Here's all code.

<?php
    echo "<table border='1'>
    <tr><td>NAME</td></tr>
                    <tr><td>COUNTRY</td></tr>";
        foreach($arr['fruits'] as $key=>$fruit) {
                        ?>
                     <tr><td><?php echo $fruit->NAME["Name"];?></td></tr>
                     <tr><td><?php echo $fruit->COUNTRY["Country"];?></td></tr>
        <?php ;}
    echo "</table>";
 ?>

2 Comments

Well, I get error that "Cannot use object of type stdClass as array"
what function do you use for fetching result? _fetch_assoc() or _fetch_method ?
0

Your problem is with this sort of thing:

$fruit->NAME["Name"]

This means in the $fruit object look for a property called NAME that is an array containing the key "Name".

Also, your table isn't very tabular with only one cell per row. I would try something like this (if $fruit is really an array not an object):

<table border="1">
   <tr><th>Name</th><th>Country</th></tr>
<?php
foreach( $arr['fruits'] as $fruit ) {
   echo "<tr><td>$fruit['NAME']</td><td>$fruit['COUNTRY']</td></tr>";
}
?>
</table>

We can't help you without knowing the actual structure of $arr['fruits']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.