tags:

views:

47

answers:

1

Quotes object:

Array
(
    [0] => Array
        (
            [sitecaptions] => Array
                (
                    [id] => 2
                    [caption] => Great camera deals!
                    [linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
                )

        )

)

The Posts Object:

Array
(
[0] => Array
        (
            [Post] => Array
                (
                    [id] => 2797
                    [post_title] => xx1
                    [item_desc] => xx desc
                    [dateadded] => 2009-12-22 11:10:15
                )
            [Category] => Array
                (
                    [0] => Array
                        (
                            [id] => 99
                            [name] => Others
                        )

                )
       )
[1] => Array
        (
            [Post] => Array
                (
                    [id] => 2798
                    [post_title] => xx2
                    [item_desc] => xx2 desc
                    [dateadded] => 2009-12-22 11:10:45
                )
            [Category] => Array
                (
                    [0] => Array
                        (
                            [id] => 99
                            [name] => Others
                        )

                )
       )
)

As you can see, the Posts Object contains two elements, [Post] and [Category] for each record [0],[1] etc. I want to insert the [sitecaptions] element into that Posts Object so that in effect it looks like:

Array
(
[0] => Array
        (
            [Post] => Array
                (
                    [id] => 2797
                    [post_title] => xx1
                    [item_desc] => xx desc
                    [dateadded] => 2009-12-22 11:10:15
                )
            [Category] => Array
                (
                    [0] => Array
                        (
                            [id] => 99
                            [name] => Others
                        )

                )
            [sitecaptions] => Array
                (
                    [id] => 2
                    [caption] => Great camera deals!
                    [linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
                )
       )
)

How do I combine two objects like that? Or how do I insert elements into an existing object? Hope I'm clear about what I'm asking. Thanks for your time...

+2  A: 

lets call these objects $Quotes and $Posts respectively.

Quotes object:

Array (
    [0] => Array ( 
        [sitecaptions] => Array (
            [id] => 2 
            [caption] => Great camera deals! 
            [linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders )
            )
        )
    )

The Posts Object:

Array ( 
    [0] => Array (
        [Post] => Array (
            [id] => 2797
            [post_title] => xx1
            [item_desc] => xx desc
            [dateadded] => 2009-12-22 11:10:15 
        )
        [Category] => Array (
            [0] => Array (
                [id] => 99
                [name] => Others
            )
        )
    )
    [1] => Array (
        [Post] => Array (
            [id] => 2798 
            [post_title] => xx2 
            [item_desc] => xx2 desc 
            [dateadded] => 2009-12-22 11:10:45
         )
         [Category] => Array (
            [0] => Array (
                [id] => 99 
                [name] => Others 
            )
        )
   )
)

do you want the [sitecaptions] from the $quotes to be in both of your $posts elements? or just the one with the same key?

as to say if you have just $quotes[0] only $posts[0] will be affected. OR both $posts[0] and $posts[1] will be affected.

if you want $quotes[0] to be in both $post elements you can do this:

foreach ($posts as $key=>$post) {
    $posts[$key]['sitecaptions'] = $quotes[0]['sitecaptions'];
}

if you want only the elements from $quotes that have the same index as the elements in $posts you can do this:

$posts = array_merge_recursive($posts,$quotes);

doing this second one would have $posts[1] being without a ['sitecaptions'] element. end result:

Array (
    [0] => Array ( 
        [Post] => Array ( 
            [id] => 2797 
            [post_title] => xx1 
            [item_desc] => xx desc 
            [dateadded] => 2009-12-22 11:10:15 
        ) 
        [Category] => Array ( 
            [0] => Array ( 
                [id] => 99 
                [name] => Others 
            )
        )
        [sitecaptions] => Array (
            [id] => 2
            [caption] => Great camera deals!
            [linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders 
        )
    )
)

Hope it helps!

Jaimz
Ashok