azashow Posted February 26, 2011 Share Posted February 26, 2011 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 More sharing options...
sasa Posted February 26, 2011 Share Posted February 26, 2011 <?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); ?> Link to comment https://forums.phpfreaks.com/topic/228893-question-for-experts/#findComment-1180019 Share on other sites More sharing options...
azashow Posted February 26, 2011 Author Share Posted February 26, 2011 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 Link to comment https://forums.phpfreaks.com/topic/228893-question-for-experts/#findComment-1180074 Share on other sites More sharing options...
sasa Posted February 26, 2011 Share Posted February 26, 2011 change } else $out1[]=$data; to[/code]} else $out1[]=array_shift ($data);[/code] Link to comment https://forums.phpfreaks.com/topic/228893-question-for-experts/#findComment-1180081 Share on other sites More sharing options...
azashow Posted February 26, 2011 Author Share Posted February 26, 2011 Thank you SASA You are the Best Hassan Link to comment https://forums.phpfreaks.com/topic/228893-question-for-experts/#findComment-1180083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.