Saturday, May 30, 2009

Re: Modifying arrays in

This doesn't work?

foreach ($posts as $i => $post)
{
foreach($post['Comment'] as $j => $comment)
{
$posts[$i]['Comment'][$j]['test'] = 'test';
}
}

On Sat, May 30, 2009 at 4:59 AM, sleepycat
<blessedbyvirtuosity@gmail.com> wrote:
>
> I am just getting used to working with CakePHP and ran into a problem.
> I am hoping someone here might know how to solve it.
> I thought this was going to be an easy one but for some reason its
> eluding me...
>
> So I have an array, something like this:
>
> [0] => Array
> (
> [Post] => Array
> (
> [id] => 1
> [title] => First article
> [content] => aaa
> [created] => 2008-05-18 00:00:00
> )
> [Comment] => Array
> (
> [0] => Array
> (
> [id] => 1
> [post_id] => 1
> [author] => Daniel
> [email] => dan@example.com
> [website] => http://example.com
> [comment] => First comment
> [created] => 2008-05-18 00:00:00
>
> I want to be able to add (and later remove) an element from the array.
> For instance adding some extra field to the comment array.
>
> I thought that since it is an associative array it would be pretty
> easy, a foreach with something like:
> $post['Comment']['test'] = 'test';
> I have looked at Set::insert and it looks like it should do what I
> want but I can't figure out how to get it to iterate add an element
> for every comment in the array.
>
> The end result should look like this:
>
> [Comment] => Array
> (
> [0] => Array
> (
> [id] => 1
> [post_id] => 1
> [author] => Daniel
> [email] => dan@example.com
> [website] => http://example.com
> [comment] => First comment
> [created] => 2008-05-18 00:00:00
> [test] => test
>
>
> Anyone have any suggestions?
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: