how can remove duplicate values multi-dimensional array in php?
example array:
array ( [choice] => array ( [0] => array ( [day] => monday [value] => array ( [0] => array ( [name] => bi [time] => 10:00 [location] => b123 ) [1] => array ( [name] => bi [time] => 11:00 [location] => a123 ) ) ) [1] => array ( [day] => tuesday [value] => array ( [0] => array ( [name] => bi [time] => 10:00 [location] => b123 ) [1] => array ( [name] => bi [time] => 11:00 [location] => a123 ) ) ) ) )
i'd remove duplicate name
. want keep 1 subject each day.
my code far:
$taken = array(); foreach($subject_list['choice'][0]["value"] $key =>$item ) { if(!in_array($item['name'], $taken)) { $taken[] = $item['name']; }else { unset($flight_list['choice'][0]["value"][$key]); } }
output of code above (which wrong):
array ( [choice] => array ( [0] => array ( [day] => monday [value] => array ( [0] => array ( [name] => bi [time] => 10:00 [location] => b123 ) ) ) [1] => array ( [day] => tuesday [value] => array ( [0] => array ( [name] => bi [time] => 10:00 [location] => b123 ) [1] => array ( [name] => bi [time] => 11:00 [location] => a123 ) ) ) ) )
anyone can me how can remove same class name
@ tuesday
.
if want keep first set of unique values in each value
batch in terms of name
, create temporary container that. if have pushed it, don't process anything, after gathering, overwrite batch using foreach
&
reference:
foreach($subject_list['choice'] &$items) { $temp = array(); // temporary container current iteration foreach($items['value'] $value) { if(!isset($temp[$value['name']])) { // if new $temp[$value['name']] = $value; // push batch using key name } } $items['value'] = $temp; // apply unique value in end of batch }
Comments
Post a Comment