uniq multiple dimensional array - php

I need to merge following two arrays into a single array.
It looks like array_merge not support multiple dimensional array.
Courses
Array (
[0] => Array ( [Name] => Course1 [CourseId] => 1 [Selected] => )
[1] => Array ( [Name] => Course2 [CourseId] => 2 [Selected] => )
[2] => Array ( [Name] => Course3 [CourseId] => 3 [Selected] => )
[3] => Array ( [Name] => Course4 [CourseId] => 4 [Selected] => )
[4] => Array ( [Name] => Course5 [CourseId] => 5 [Selected] => )
)
TeacherCourses
Array (
[0] => Array ( [CourseId] => 1 [Selected] => selected)
[1] => Array ( [CourseId] => 2 [Selected] => selected)
[2] => Array ( [CourseId] => 3 [Selected] => selected)
)
I need this result
Array (
[0] => Array ( [Name] => Course1 [CourseId] => 1 [Selected] => selected)
[1] => Array ( [Name] => Course2 [CourseId] => 2 [Selected] => selected)
[2] => Array ( [Name] => Course3 [CourseId] => 3 [Selected] => selected)
[3] => Array ( [Name] => Course4 [CourseId] => 4 [Selected] => )
[4] => Array ( [Name] => Course5 [CourseId] => 5 [Selected] => )
)

Have you tried foreach loop?
Here we go,:
foreach($arr1 as $key => $value){
foreach($arr2 as $value2){
if($value['CourseId'] === $value2['CourseId']){
$arr1[$key]['Selected'] = $value2['Selected'];
}
}
}
hope this would help you.
keep coding :)

Related

Need to Group php array with same keys

Hii I need help in grouping array where i have this sort of array :
Array
(
[0] => Array
(
[name] => rose
[price] => 1
)
[1] => Array
(
[name] => daisy
[price] => 3
)
[2] => Array
(
[name] => orchid
[price] => 1
)
[3] => Array
(
[name] => rose
[price] => 2
)
[4] => Array
(
[name] => daisy
[price] => 3
)
[5] => Array
(
[name] => orchid
[price] => 1
)
[6] => Array
(
[name] => rose
[price] => 2
)
[7] => Array
(
[name] => daisy
[price] => 3
)
[8] => Array
(
[name] => orchid
[price] => 2
)
)
and i want it to be like :
Array
(
[0] => Array
(
[0] => Array
(
[name] => rose
[price] => 1
)
[1] => Array
(
[name] => daisy
[price] => 1
)
[2] => Array
(
[name] => orchid
[price] => 1
)
)
[1] => Array
(
[0] => Array
(
[name] => rose
[price] => 2
)
[1] => Array
(
[name] => daisy
[price] => 2
)
[2] => Array
(
[name] => orchid
[price] => 2
)
)
[2] => Array
(
[0] => Array
(
[name] => rose
[price] => 3
)
[1] => Array
(
[name] => daisy
[price] => 3
)
[2] => Array
(
[name] => orchid
[price] => 3
)
)
)
I mean want to group them where same "price" value occurs . You can better understand them from given arrays .
$group = array();
foreach ( $array as $value ) {
$group[$value['price']][] = $value;
}
var_dump($group);

Create associative array with value as key

I have this array with information, which I want to create a new associative array with. Each key in the associative array should be the "name" from the old one. And in each new key, I want the corresponding information to be collected.
Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
[2] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
[3] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[4] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
This is the array I'm trying to create:
Array
(
["Ben"] => Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[2] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
["Evan"] => Array
(
[0] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
)
["Steve"] => Array
(
[0] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
)
)
$newArr = array();
foreach($myArr as $value) {
$name = $value['name'];
if (isset($newArr[$name])) {
$newArr[$name][] = $value;
}
else {
$newArr[$name] = array($value);
}
}
Use a foreach loop to create a new array:
$newArr = [];
foreach($myArr as $key => $value){
$newArr[$myArr[$key][$value['name']]][] = $myArr[$key];
}

Compare values of two multidimentional array and insert if not exits

I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){

Sort Multi-dimensional Array by Value and save

How can I sort this array by the value of the "seasons" field first, then of the "series" field? And save the keys
Array
(
[2] => Array
(
[0] => Array
(
[season] => 2
[series] => 3
[title] => Boom
)
[1] => Array
(
[season] => 2
[series] => 1
[title] => Boom
)
[2] => Array
(
[season] => 2
[series] => 2
[title] => Boom
)
)
[1] => Array
(
[0] => Array
(
[season] => 1
[series] => 1
[title] => Boom
)
[1] => Array
(
[season] => 1
[series] => 3
[title] => Boom
)
[2] => Array
(
[season] => 1
[series] => 2
[title] => Boom
)
)
)
)
usort, ksort functions does not save the keys, but sorting by seasons and series values really good
After usort function:
Array
(
[0] => Array
(
[0] => Array
(
[season] => 1
[series] => 1
[title] => Boom
)
[1] => Array
(
[season] => 1
[series] => 2
[title] => Boom
)
[2] => Array
(
[season] => 1
[series] => 3
[title] => Boom
)
)
[1] => Array
(
[0] => Array
(
[season] => 2
[series] => 1
[title] => Boom
)
[1] => Array
(
[season] => 2
[series] => 2
[title] => Boom
)
[2] => Array
(
[season] => 2
[series] => 3
[title] => Boom
)
)
)
Who can help me solve this problem?
Solved by array_walk
array_walk($arr, function($a, $b){
return $a['season'] - $b['season'];
});

Ranking or Position on value of multidimensional array in PHP

I want to rank this following array according to points and duplicate points will be same ranked. Any idea how I will be complete this task.
Array
(
[6] => Array
(
[points] => 0
[details] => Array
(
[name] => name 1
[rank] =>
)
)
[220] => Array
(
[points] => 794
[details] => Array
(
[name] => name 2
[rank] =>
)
)
[369] => Array
(
[points] => 1002
[details] => Array
(
[name] => name 3
[rank] =>
)
)
[138] => Array
(
[points] => 1239
[details] => Array
(
[name] => name 4
[rank] =>
)
)
[778] => Array
(
[points] => 1302
[details] => Array
(
[name] => name 5
[rank] =>
)
)
)
something like this:
$sort=array();
foreach($array as $key=>$v){$sort[$key]=$v['points'];}
sort($sort);
$newArray=array();
$i=0; $points=0;
foreach($sort as $key=>$v){
$i++;
if($v['points']>$points){$i++; $points=$v['points'];
$newArray[$key]=$array['key'];
$newArray[$key]['rank']=$i;
}
$array=$newArray;

Categories