Welcome Guest, Not a member yet? Register   Sign In
Foreach Associative Array
#1

[eluser]egunay[/eluser]
Hello,

I have an associative array like:

Code:
Array ( [title] => Array ( [0] => Test Post [1] => Second Post ) [body] => Array ( [0] => Test Body! [1] => Second Test Body! ) [author] => Array ( [0] => 1 [1] => 1 ) )

But unfortunately I couldn't figure out how to do an foreach loop to show them seperately.

Hope I could have explained my problem.

Thanks in advance!
#2

[eluser]cahva[/eluser]
Define your array differently, it will be easier to handle. Like:
Code:
$posts = array();

$posts[] = array(
    'title' => 'Test post1',
    'body' => 'Test Body1',
    'author' => 1
);

$posts[] = array(
    'title' => 'Test post2',
    'body' => 'Test Body2',
    'author' => 1
);
Then you can easily handle posts in the view:
Code:
<?php foreach ($posts as $post): ?>

    <h3>&lt;?php echo $post['title'] ?&gt;  by &lt;?php echo $post['author'] ?&gt;</h3>
    <p>&lt;?php echo $post['body'] ?&gt;</p>

&lt;?php endforeach; ?&gt;
#3

[eluser]egunay[/eluser]
Yes, it became really easy this way. Thanks for the tip! Smile




Theme © iAndrew 2016 - Forum software by © MyBB