Create associative array with value as key - php

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];
}

Related

Need to sort array in ASC or DESC order by their title value

Below is my array,
Array
(
[0] => Array
(
[topic_title] => gff
[title] => gff
)
[1] => Array
(
[topic_title] => new toipic
[title] => new toipic
)
[2] => Array
(
[topic_title] => Welcome!
[title] =>Welcome!
)
[3] => Array
(
[entry_title] => sdasdad
[title] => sdasdad
)
[4] => Array
(
[event_title] => red
[title] => red
)
[5] => Array
(
[event_title] => sa
[title] => sa
)
[6] => Array
(
[event_title] => sadasd
[title] => sadasd
)
[7] => Array
(
[event_title] => Test Event
[title] => Test Event
)
)
I want the output like below
Array(
[0] => Array
(
[event_title] => Test Event
[title] => Test Event
)
[1] => Array
(
[event_title] => Welcome!
[title] => Welcome!
)
[2] => Array
(
[event_title] => sa
[title] => sa
)
[3] => Array
(
[entry_title] => sdasdad
[title] => sdasdad
)
[4] => Array
(
[event_title] => sadasd
[title] => sadasd
)
[5] => Array
(
[event_title] => red
[title] => red
)
[6] => Array
(
[topic_title] => new toipic
[title] => new toipic
)
[7] => Array
(
[topic_title] => gff
[title] => gff
)
)
This is the print code from PHP 5.6.
I uses usort() function but didn't get required one. I am not able to sort array in assenting or descending order by their title key.
This array I am getting from different queries an d after that I merge it in one array but not able to sort it by title value
Try this :
$array = Array ( Array("topic_title" => "aatoipic","title" => "new toipic"),Array("event_title" => "Test Event","title" => "Test Event"),Array("topic_title" => "Welcome!", "title" => "Welcome!"),Array("entry_title" => "sdasdad","title" => "p",),Array("event_title" => "red","title" => "k",),Array("event_title" => "sa","title" => "sa"),Array("event_title" => "sadasd","title" => "d"),Array("topic_title" => "gff", "title" => "gff"),Array( "topic_title" => "new toipic","title" => "h"));
$arr = $array;
$sort = array();
foreach($arr as $k=>$v) {
$sort['title'][$k] = $v['title'];
}
array_multisort($sort['title'], SORT_ASC, $arr);
echo "<pre>";
print_r($arr);
Output will come :
Array ( [0] => Array ( [event_title] => Test Event [title] => Test Event ) [1] => Array ( [topic_title] => Welcome! [title] => Welcome! ) [2] => Array ( [event_title] => sadasd [title] => d ) [3] => Array ( [topic_title] => gff [title] => gff ) [4] => Array ( [topic_title] => new toipic [title] => h ) [5] => Array ( [event_title] => red [title] => k ) [6] => Array ( [topic_title] => aatoipic [title] => new toipic ) [7] => Array ( [entry_title] => sdasdad [title] => p ) [8] => Array ( [event_title] => sa [title] => sa ) )
You can use array_multisort() to achieve the desired result.
E.g
$title = array();
foreach ($post as $key => $row)
{
$title[$key] = $row['title'];
}
array_multisort($title, SORT_DESC, $post);
You need to use usort with a strcasecmp:
function querySort ($x, $y) {
return strcasecmp($x['title'], $y['title']);
}
usort($myArray, 'querySort');
usort will do the trick:
usort($array, function($x, $y) {
return strcasecmp($x['title'], $y['title']);
});

Iterating recursively through an array

Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}

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'))){

uniq multiple dimensional array

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 :)

array difference

I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;

Categories