Jump to content

question for experts


azashow

Recommended Posts

I have a result from a query in database like this :

Array

(

    [0] => Array

        (

            [feed_id] => 3

            [tag] => adidas

            [user_id] => 1

            [full_name] => admin

            [comment] => Axa

            [date_posted] => 2011-02-11 14:30:31

        )

 

    [1] => Array

        (

            [feed_id] => 3

            [tag] => Nike

            [user_id] => 1

            [full_name] => admin

            [comment] =>

            [date_posted] => 2011-02-11 14:30:31

        )

 

    [2] => Array

        (

            [feed_id] => 6

            [tag] => Puma

            [user_id] => 1

            [full_name] => admin

            [comment] =>

            [date_posted] => 2011-02-11 14:30:31

        )

 

 

Thank you in advance

what i want to do is merge the array that has the same feed_id  it repeated caus eof teh same feed has more than 1 comment  so this is the result i wanted:

 

 

 

Array

(

    [0] => Array

        (

            [feed_id] => 3

            [tags] => array(

                                [tag]=>adidas

                                [tag]=>Nike

                                  )

            [user_id] => 1

            [full_name] => admin

            [comments] => array(

                                [comment]=> what i comment..

                                [comment]=> second what i comment..

                              )

            [date_posted] => 2011-02-11 14:30:31

        )

 

    [1] => Array

        (

            [feed_id] => 6

            [tag] => Puma

            [user_id] => 1

            [full_name] => admin

            [comment] =>

            [date_posted] => 2011-02-11 14:30:31

        )

 

   

Link to comment
https://forums.phpfreaks.com/topic/228893-question-for-experts/
Share on other sites

<?php
$test = Array(
    '0' => Array(
            'feed_id' => 3,
            'tag' => 'adidas',
            'user_id' => 1,
            'full_name' => 'admin',
            'comment' => 'Axa',
            'date_posted' => '2011-02-11 14:30:31'
        ),
    '1' => Array(
            'feed_id' => 3,
            'tag' => 'Nike',
            'user_id' => 1,
            'full_name' => 'admin',
            'comment' => '',
            'date_posted' => '2011-02-11 14:30:31'
        ),
    '2' => Array(
            'feed_id' => 6,
            'tag' => 'Puma',
            'user_id' => 1,
            'full_name' => 'admin',
            'comment' => '',
            'date_posted' => '2011-02-11 14:30:31'
        )
);
$out = array();
foreach ($test as $data)
    $out[$data['feed_id']][] = $data;

foreach ($out as $k => $data){
    if(count($data)>1){
        $tmp = array();
        foreach ($data as $k1 => $data1){
            foreach ($data1 as $key => $v)$tmp[$key][]=$v;
        }
        foreach ($tmp as $key => $v){
            $v = array_unique($v);
            if(count($v)==1) $out1[$k][$key] = array_shift ($v); else $out1[$k][$key.'s']=array_values ($v);
        }
    } else $out1[]=$data;
}
$out1=array_values($out1);
print_r($out1);
?>

Thank you for the fast reply.

 

I still have  a problem with it

 

Array

(

    [0] => Array

        (

            [feed_id] => 3

            [tags] => Array

                (

                    [0] => adidas

                    [1] => Nike

                )

 

            [user_id] => 1

            [full_name] => admin

            [comments] => Array

                (

                    [0] => Axa

                    [1] => helloooo

                )

 

            [date_posted] => 2011-02-11 14:30:31

        )

 

    [1] => Array

        (

            [0] => Array

                (

                    [feed_id] => 6

                    [tag] => Puma

                    [user_id] => 1

                    [full_name] => admin

                    [comment] => cdcsdvfsvfvv

                    [date_posted] => 2011-02-11 14:30:31

                )

 

        )

 

)

 

 

 

Array

(

    [0] => Array

        (

            [feed_id] => 3

            [tags] => Array

                (

                    [0] => adidas

                    [1] => Nike

                )

 

            [user_id] => 1

            [full_name] => admin

            [comments] => Array

                (

                    [0] => Axa

                    [1] => helloooo

                )

 

            [date_posted] => 2011-02-11 14:30:31

        )

 

    [1] => Array

        (

            [0] =>

                (

                    [feed_id] => 6

                    [tag] => Puma

                    [user_id] => 1

                    [full_name] => admin

                    [comment] => cdcsdvfsvfvv

                    [date_posted] => 2011-02-11 14:30:31

                )

 

     

 

)

 

 

i want the second element to be an array instead of a multi demential array.

 

Thank you in advance

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.