Merge 2 Arrays on second level with different keys - php

I need to merge two arrays on the second level with different key names but the same amount of keys.
usera=102
userb=103
My desired output would be array 3.
I have tried a lots of variations to get this together.
My best attempt was something like the following but it destroyed my key names:
$results = array();
foreach($arr1 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name'] = $value;
}
}
foreach($arr2 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name2'] = $value;
}
}
Array 1 :
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
)
[1] => Array
(
[user] => usera
[duration_s] => 9
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
)
[3] => Array
(
[user] => userb
[duration_s] => 19
)
)
)
Array 2:
Array
(
[102] => Array
(
[0] => Array
(
[user] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => 102
[duration_s2] => 378
)
)
[103] => Array
(
[2] => Array
(
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[usernr] => 103
[duration_s2] => 110
)
)
)
Array 3:
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
[usernr] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => usera
[duration_s] => 9
[usernr] => 102
[duration_s2] => 378
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[user] => userb
[duration_s] => 19
[usernr => 103
[duration_s2] => 110
)
)
)

Try the following code using array_values() :
<?php
$array1 = [
'usera' => [
0=> ['user' => 'usera','duration_s' => 15],
1=> ['user' => 'usera','duration_s' => 9],
],
'userb' => [
2=> ['user' => 'usera','duration_s' => 15],
3=> ['user' => 'usera','duration_s' => 9],
],
];
$array2 = [
102 => [
0=> ['usernr' => 102,'duration_s2' => 54],
1=> ['usernr' => 102,'duration_s2' => 378]
],
103 => [
2=> ['usernr' => 103,'duration_s2' => 299],
3=> ['usernr' => 103,'duration_s2' => 110]
],
];
$array2 = array_values($array2);
$array1 = array_values($array1);
foreach($array1 as $index=>$ar1){
foreach ($ar1 as $index2=>$ar2){
$array1[$index][$index2] = array_merge($ar2,$array2[$index][$index2]);
}
}
print_r($array1);

To merge the array you can also do the following :
$index = 0;
foreach ($arr2 as $key => $value) { // changing the indexing of second array
$arr3[$index] = $value;
$index++;
}
$results = array();
$count = 0;
foreach ($arr1 as $index => $value) { // merging two array
$total = 0;
foreach ($value as $key => $val) {
$merge_value = array_merge($val,$arr3[$count][$total]);
$results[$index][$total] = $merge_value;
$total++;
}
$count++;
}
print_r($results);
In this result your indexing will not lost.

Related

empty value of a key if the array has duplicates

i have this array
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] => 3
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)
what i want to do is if the key post_id is repeated i just want to empty it and keep one so my final array will look like this
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] =>
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] =>
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] =>
[post_user_added_id] => 15
)
i have tried this code but it doesn't seem to work it deletes the whole array
foreach($arry as $k => $v)
{
foreach($arry as $key => $value)
{
if($k != $key && $v['post_id'] == $value['post_id'])
{
unset($arry [$k]);
}
}
}
print_r($arry);
You can perform foreach with ternary operator
$last = null;//this will keep the previous post_id
foreach($arr as &$v){
($last && $last == $v['post_id']) ? ($v['post_id'] = '') : ($last = $v['post_id']);
}
print_r($arr);
Working example :- https://3v4l.org/RiU9J
here try this.
$tempArr = [];
$resArr = [];
foreach($orignalArr as $key=>$obj){
if(in_array($obj['post_id'], $tempArr)){
$obj['post_id'] = '';
$resArr[] = $obj;
}else{
$tempArr[] = $obj['post_id'];
$resArr[] = $obj;
}
}
print_r($resArr);
try this :
// example code
$arrayResult = array();
$arry = [
1 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 2
],
2=>
[
'user_id' => 76,
'post_id' => 3,
'post_user_added_id' => 16
],
3 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 12
],
4 =>
[
'user_id' => 76,
'post_id' => 9,
'post_user_added_id' => 15
],
5 =>
[
'user_id' => 77,
'post_id' => 9,
'post_user_added_id' => 15
]];
$final = array();
foreach($arry as $a)
{
if (in_array($a['post_id'], $arrayResult)) {
$a['post_id'] = 0;
}else{
$arrayResult[] = $a['post_id'];
}
$final[] = $a;
}
var_dump($final);
Maybe if you use a stack for comparison?
edit: rewritten the code...
$stack=[];
foreach ($array as $index => $subarray){
if(in_array($subarray['post_id'], $stack)) {
$array[$index]['post_id'] = null;
}
else $stack[]=$subarray['post_id'];
}
print_r($array);
example: https://3v4l.org/FsPUG
Hope this helps!
use of your array value in instead of $arr and try to filter array like the following code :
$arr = array(
array(
'user_id'=>15,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
);
$postids = array();
foreach($arr as $key=>$val){
if(in_array($val['post_id'], $postids)){
$arr[$key]['post_id'] = '';
}else{
$postids[] = $val['post_id'];
}
}
echo "<pre>";print_r($arr);exit;
Output :
Array
(
[0] => Array
(
[user_id] => 15
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
[2] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[3] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[4] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
)

Converting Array type in PHP

Please Help in conversion of an array from one form to another I Have This Array
Array (
[mpr_last_month] => 376431
[mpr_month] => 03
[total_boys_all_6m_36m] => 5550225
[total_girls_all_6m_36m] => 5215529
[total_boys_all_36m_72m] => 4209639
[total_girls_all_36m_72m] => 4149613
[total_pse_boys_36m_72m] => 4442301
[total_pse_all_girls_36m_72m] => 4413446
[total_pregnanting] => 2209158 )
Array (
[mpr_last_month] => 448216
[mpr_month] => 04
[total_boys_all_6m_36m] => 7153209
[total_girls_all_6m_36m] => 6798913
[total_boys_all_36m_72m] => 5175846
[total_girls_all_36m_72m] => 5105460
[total_pse_boys_36m_72m] => 5290617
[total_pse_all_girls_36m_72m] => 5263340
[total_pregnanting] => 2944612
)
Array (
[mpr_last_month] => 448253
[mpr_month] => 05
[total_boys_all_6m_36m] => 11742417
[total_girls_all_6m_36m] => 6362815
[total_boys_all_36m_72m] => 4879252
[total_girls_all_36m_72m] => 4756805
[total_pse_boys_36m_72m] => 5344042
[total_pse_all_girls_36m_72m] => 5095155
[total_pregnanting] => 2852864
)
Array (
[mpr_last_month] => 470848
[mpr_month] => 06
[total_boys_all_6m_36m] => 6552523
[total_girls_all_6m_36m] => 6217771
[total_boys_all_36m_72m] => 4613019
[total_girls_all_36m_72m] => 4551685
[total_pse_boys_36m_72m] => 5182666
[total_pse_all_girls_36m_72m] => 5165730
[total_pregnanting] => 2746293
)
Array (
[mpr_last_month] => 465489
[mpr_month] => 07
[total_boys_all_6m_36m] => 6638749
[total_girls_all_6m_36m] => 6310676
[total_boys_all_36m_72m] => 4801665
[total_girls_all_36m_72m] => 4657764
[total_pse_boys_36m_72m] => 5020964
[total_pse_all_girls_36m_72m] => 5051785
[total_pregnanting] => 2815773
)
I Want This
name: 'mpr_last_month',
data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175,123,123,123,123]
.
.
.
.
.
.
.
.
Simplest approach would be foreach loop.
$newData = [];
foreach ($yourArray as $innerArray) {
foreach ($innerArray as $key => $value) {
$newData[$key][] = $value;
}
}
It loops over the first array (the big one, containg all others), the runs over each inner array and store the value in the correct place.
You can use array_column function. But be aware that this function works only for PHP >= 5.5.0. Try this:
//example data
$array = [
['a' => 2, 'b'=>3, 'c'=>6],
['a' => 12, 'b'=>13, 'c'=>16],
['a' => 22, 'b'=>23, 'c'=>26],
];
$result = [];
//get indexes from first element of array
$indexes = array_keys($array[0]);
foreach($indexes as $index) {
$result[$index] = array_column($array, $index);
}
And result is:
Array
(
[a] => Array
(
[0] => 2
[1] => 12
[2] => 22
)
[b] => Array
(
[0] => 3
[1] => 13
[2] => 23
)
[c] => Array
(
[0] => 6
[1] => 16
[2] => 26
)
)

How to extract values from multidimensional array grouped by some value inside it using PHP?

I have an array that looks like this:-
Array (
[0] => Array ( [id] => 10 [group] => 11 )
[1] => Array ( [id] => 11 [group] => 13 )
[2] => Array ( [id] => 12 [group] => 13 )
[3] => Array ( [id] => 13 [group] => 13 )
[4] => Array ( [id] => 14 [group] => 16 )
[5] => Array ( [id] => 15 [group] => 16 )
[6] => Array ( [id] => 16 [group] => 16 )
)
For each different group in this array i want to create array that stores id's.
In my example i have 3 groups, so i want to get 3 arrays like this:
Array ( [0] => 10)
Array ( [0] => 11
[1] => 12
[2] => 13)
Array ( [0] => 14
[1] => 15
[2] => 16)
Is it possible if it is how can i do it?
This should help you get started.
https://iconoun.com/demo/temp_icold.php
<?php // demo/temp_icold.php
/**
* Manipulate multi-dimensional arrays
*
* https://stackoverflow.com/questions/45422046/how-to-extract-values-from-multidimensional-array-grouped-by-some-value-inside-i
*/
error_reporting(E_ALL);
echo '<pre>';
$original = Array (
'0' => Array ( 'id' => 10, 'group' => 11 ),
'1' => Array ( 'id' => 11, 'group' => 13 ),
'2' => Array ( 'id' => 12, 'group' => 13 ),
'3' => Array ( 'id' => 13, 'group' => 13 ),
'4' => Array ( 'id' => 14, 'group' => 16 ),
'5' => Array ( 'id' => 15, 'group' => 16 ),
'6' => Array ( 'id' => 16, 'group' => 16 ),
);
print_r($original);
foreach ($original as $arr)
{
$ndx = 'group' . $arr['group'];
$out[$ndx][] = $arr['id'];
}
print_r($out);
You can achieve this through many different methods, but the simplest is probably a foreach() loop. In the example below I am looping through $a (your sample array) and building up a new array called $grouped with the index as the group attributes value.
In my example I am not duplicating the ID's inside of $group. If you wanted duplicates you could remove the second if statement.
$a = [
['id' => 10, 'group' => 11],
['id' => 11, 'group' => 13],
['id' => 12, 'group' => 13],
['id' => 13, 'group' => 13],
['id' => 14, 'group' => 16],
['id' => 15, 'group' => 16],
['id' => 16, 'group' => 16],
];
$grouped = [];
foreach ($a as $entry) {
if (! array_key_exists($entry['group'], $grouped)) {
$grouped[$entry['group']] = [];
}
if (! in_array($entry['id'], $grouped[$entry['group']])) {
$grouped[$entry['group']][] = $entry['id'];
}
}
var_dump($grouped);
The example outputs the following:
array(3) {
[11]=>
array(1) {
[0]=>
int(10)
}
[13]=>
array(3) {
[0]=>
int(11)
[1]=>
int(12)
[2]=>
int(13)
}
[16]=>
array(3) {
[0]=>
int(14)
[1]=>
int(15)
[2]=>
int(16)
}
}
$result_array = array();
foreach($array_name as $sub_array){
$result_array[$sub_array['group']][] = $sub_array['id'];
}
This will loop through your input array and create a result two dimensional array indexed by your group values. To extract a group result just index for the group as such: $result_array['GROUP_NUMBER'];
Here is my take on this:
<?php
$arr = array(
array("id"=>10,"group"=>11),
array("id"=>11,"group"=>13),
array("id"=>12,"group"=>13),
array("id"=>13,"group"=>13),
array("id"=>14,"group"=>16),
array("id"=>15,"group"=>16),
array("id"=>16,"group"=>16)
);
echo "<pre>".print_r(groupsToArrays($arr),true)."</pre>";
function groupsToArrays($arr, $groupkey = "group") {
$main = array();
$group = 0;
$arr[] = array("id"=>"end", $groupkey => "0");
foreach($arr as $key => $value) {
if($group != $value[$groupkey]) {
if($key != 0) $main[$group] = $tempArray;
if($value['id'] == "end") continue;
$group = $value[$groupkey];
$tempArray = array();
}
$tempArray[] = $value;
}
return $main;
}
This function will loop through your array and check the $groupkey key and will add every match to it's own array.
This is the return:
Array
(
[11] => Array
(
[0] => Array
(
[id] => 10
[group] => 11
)
)
[13] => Array
(
[0] => Array
(
[id] => 11
[group] => 13
)
[1] => Array
(
[id] => 12
[group] => 13
)
[2] => Array
(
[id] => 13
[group] => 13
)
)
[16] => Array
(
[0] => Array
(
[id] => 14
[group] => 16
)
[1] => Array
(
[id] => 15
[group] => 16
)
[2] => Array
(
[id] => 16
[group] => 16
)
)
)
So now you can access that group array by doing:
$groups = groupsToArrays($arr);
print_r($groups[$groupID]);

Remove the top level array and merge sub array into one

I have a multidimensional array and I want to remove the top level array and merge all its sub array into one array.
Below is my array:
$arr = [KEY1] => Array
(
[0] => Array
(
[Feb] => 120
)
[1] => Array
(
[Jan] => 230
)
[3] => Array
(
[Mar] => 340
)
)
[KEY2] => Array
(
[0] => Array
(
[Feb] => 12
)
[1] => Array
(
[Jan] => 23
)
[3] => Array
(
[Mar] => 34
)
)
I need to arrange and sort this array like below:
Output:
[KEY1] => Array
(
[Jan] => 230,
[Feb] => 120,
[Mar] => 340
)
[KEY2] => Array
(
[Jan] => 23,
[Feb] => 12,
[Mar] => 34
)
I have used
call_user_func_array('array_merge', $arr ); but not working.
Please suggest any wise way to do this.
Thanks
You can try like this as simple way,
$arr = [
'key1' =>[
0 => ["feb" => 123],
1 => ["dev" => 213],
2 => ["jan" => 111],
],
'key2' =>[
0 => ["feb" => 132],
1 => ["dev" => 321],
2 => ["jan" => 555],
],
];
$result = [];
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2)
$result[$k][$k2] = $v2;
}
}
print_r($result);
Hope this will solve your problem.
EDIT
Here is your sorting function.
function sortNestedArray(&$a)
{
sort($a);
for ($i = 0; $i < count($a); $i++) {
if (is_array($a[$i])) {
sortNestedArray($a[$i]);
}
}
return $a;
}
$a = sortNestedArray($result);
print_r($a);

Searching for a value inside an array and getting the array

I am trying to use an array to search for a value that is inside of an array and then take the full array that the value is in and add it to an array. Below is the array to get the value from:
Array (
[0] => Array ( [ID] => 138 [dimmer] => 5 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[1] => Array ( [ID] => 139 [dimmer] => 6 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[2] => Array ( [ID] => 140 [dimmer] => 7 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[3] => Array ( [ID] => 141 [dimmer] => 8 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
)
I am trying to get the value of dimmer with the search function below:
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
Below it uses the $chan value which is an integer to use the function above to search an array. The foreach is then supposed to go through the array that is $patch and select the arrays out of the array above (only returns an empty array), although it doesn't do that unless you change $patch_single['dimmer'] with a string such as "7".
$patch = search($patch, 'Channel', $chan);
foreach ($patch as $patch_single) {
print_r($patch_single);
$dim_single = intval($patch_single['dimmer']);
echo $dim_single;
$dimmers = search($dimmers, 'dimmer', $dim_single);
}
The array that is being used to get $patch_single['dimmer'] is, when inside the foreach:
Array ( [ID] => 241 [Channel] => 100 [dimmer] => 7 )
Array ( [ID] => 242 [Channel] => 100 [dimmer] => 25 )
Thank you for your advice.
Hm, i see that you have 2 dimensional array. Why you just don't use this?
foreach($array as $row) {
if ($row['dimmer'] == $myValue) { $newArray[] = $row; }
}
Try this :
$arr = Array (Array ( "ID" => 138, "dimmer" => 5, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 139, "dimmer" => 6, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 140, "dimmer" => 7, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 141, "dimmer" => 8, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ));
$arr = array_filter($arr, function($ar) {
return ($ar['dimmer'] == '7' );
});
echo "<pre>";
print_r($arr);
Output :
Array
(
[2] => Array
(
[ID] => 140
[dimmer] => 7
[order] => 2
[double] => 0
[location1] => DSR
[location2] => Stage Pockets
)
)
ref: http://php.net/manual/en/function.array-filter.php

Categories