Merge with count for key value pair in associative array - php

I have this array and I want to combine the y_axis if the x_axis is the same, please see both examples so you will have an idea of what I need.
array:4 [
0 => array:2 [
"x_axis" => 8
"y_axis" => 1
]
1 => array:2 [
"x_axis" => 9
"y_axis" => 1
]
2 => array:2 [
"x_axis" => 11
"y_axis" => 2
]
3 => array:2 [
"x_axis" => 11
"y_axis" => 3
]
]
Like
array:3 [
0 => array:2 [
"x_axis" => 8
"y_axis" => 1
]
1 => array:2 [
"x_axis" => 9
"y_axis" => 1
]
2 => array:2 [
"x_axis" => 11
"y_axis" => 5
]
]

I wrote a function. Where i include two two loops. First loop create a new array where you calculate the values. The second create the final array where assign the values in the pattern where you need.
<?php
$arr = [
['x_axis' => 8, 'y_axis' => 1],
['x_axis' => 9, 'y_axis' => 1],
['x_axis' => 11, 'y_axis' => 2],
['x_axis' => 11, 'y_axis' => 3],
];
function make($arr) {
$xy = [];
foreach($arr as $k => $v) {
$xy[$v['x_axis']] = isset($xy[$v['x_axis']]) ? $xy[$v['x_axis']] + $v['y_axis'] : $v['y_axis'];
}
$arrNew = [];
foreach($xy as $k => $v) {
$arrNew[] = ['x_axis' => $k, 'y_axis' => $v];
}
return $arrNew;
}
print_r( make($arr) );
output
Array
(
[0] => Array
(
[x_axis] => 8
[y_axis] => 1
)
[1] => Array
(
[x_axis] => 9
[y_axis] => 1
)
[2] => Array
(
[x_axis] => 11
[y_axis] => 5
)
)

Related

Laravel - Can i add new item to an array based on the `id` value existing that array?

array (
0 =>
array (
'courseId' => 14,
'tutorName' => 'admin',
),
1 =>
array (
'courseId' => 15,
'tutorName' => 'merl',
),
)
var_export($response) is an array like above. For each courseId in the $response array, i wanted to find sum of points when the courseId in the $response array exists student_learning table also. After that, i wanted to add this sum of points($points) to the $response array as a new item for the corresponding courseId. Here, the issue is that, every values of $points is added as a new item in every datasets of the $response array,but i wanted it to get added to the $response array of it's respective courseId only. How can i do that?
foreach ($response as $key ) {
$points=DB::table('student_learning')->groupBy('courseId')->where('courseId',$key['courseId'])->sum('points');
$res = array_map(function($e) use($points,$percent) {
$e['points'] = $points;
return $e; }, $response);
dump($res);
}
dump($res) gives an output like below
array:2 [
0 => array:8 [
"courseId" => 14
"tutorName" => "admin"
"points" => 12
]
1 => array:8 [
"courseId" => 15
"tutorName" => "me"
"points" => 12
]
]
array:2 [
0 => array:8 [
"courseId" => 14
"tutorName" => "admin"
"points" => 3
]
1 => array:8 [
"courseId" => 15
"tutorName" => "me"
"points" => 3
]
]
dump($res) outside the foreach gives an output like below
array:2 [
0 => array:8 [
"courseId" => 14
"tutorName" => "admin"
"points" => 3
]
1 => array:8 [
"courseId" => 15
"tutorName" => "me"
"points" => 3
]
]
Expected/Required Output:
[
"courseId" => 14
"tutorName" => "admin"
"points" => 12
]
[
"courseId" => 15
"tutorName" => "me"
"points" => 3
]
You can add new array key while looping using foreach
$response = array (
0 =>
array (
'courseId' => 14,
'tutorName' => 'admin',
),
1 =>
array (
'courseId' => 15,
'tutorName' => 'merl',
),
);
$newResponse = [];
foreach($response as $eachResponse){
$points=DB::table('student_learning')->groupBy('courseId')->where('courseId',$eachResponse['courseId'])->sum('points');
$eachResponse['points'] = $points;
$newResponse[] = $eachResponse;
}
dd($newResponse);

Group multi array by key and value PHP

I have an array like the one below
array:4 [▼
0 => array:3 [▼
0 => array:2 [▼
"url" => "https://domain.com.vn"
"value" => "Keyword B3"
]
1 => array:2 [▼
"url" => "https://domain.com.vn"
"value" => "[IMAGES 1]"
]
]
1 => array:3 [▼
0 => array:2 [▼
"url" => "https://domain-4.com.vn"
"value" => "D1"
]
1 => array:2 [▼
"url" => "https://domain-4.com.vn"
"value" => "E3"
]
]
]
I want to combine the above 2 arrays into 1 array as follows (number of items in 2 arrays is always equal):
$result = [
[
"url" => [
"https://domain.com.vn",
"https://domain-4.com.vn"
]
"value" => [
"Keyword B3",
"D1"
]
],
[
"url" => [
"https://domain.com.vn",
"https://domain-4.com.vn"
],
"value" => [
"[IMAGES 1]",
"E3"
]
]
]
I have searched a lot but have not found a solution yet. Thanks for any help.
Because number of items in 2 sub-arrays is always equal, you can use a for loop like this:
$array = [
0 => [
0 => [
'url' => 'https://domain.com.vn',
'value' => 'Keyword B3'
],
1 => [
'url' => 'https://domain.com.vn',
'value' => '[IMAGES 1]'
]
],
1 => [
0 => [
'url' => 'https://domain-4.com.vn',
'value' => 'D1'
],
1 => [
'url' => 'https://domain-4.com.vn',
'value' => 'E3'
]
]
];
$result = [];
for ($i = 0 ; $i < count($array[0]); $i++) {
$result[] = [
'url' => [$array[0][$i]['url'], $array[1][$i]['url']],
'value' => [$array[0][$i]['value'], $array[1][$i]['value']],
];
}
print_r($result);
Result:
Array
(
[0] => Array
(
[url] => Array
(
[0] => https://domain.com.vn
[1] => https://domain-4.com.vn
)
[value] => Array
(
[0] => Keyword B3
[1] => D1
)
)
[1] => Array
(
[url] => Array
(
[0] => https://domain.com.vn
[1] => https://domain-4.com.vn
)
[value] => Array
(
[0] => [IMAGES 1]
[1] => E3
)
)
)
For a dynamic solution, you will need to iterate the first level of the array, then combine the first deep subarray's keys with the return value of array_map()'s special transposing behavior with null and the spread operator.
It sure is a bunch of voodoo, but it is dynamic.
Code: (Demo) (Demo showing different length first level data sets)
var_export(
array_map(
function ($subarray) {
return array_combine(
array_keys($subarray[0]),
array_map(null, ...array_values($subarray))
);
},
$array
)
);
See other other phptranspose tagged Stack Overflow pages to see other usages of the transposing technique.

Comparing two associative arrays values and replacing them

I'm facing issue while dealing with 2 associative arrays. I have two arrays, If id of Array 1 = id of Array 2 = id, then active_lession and active_learning_lession of Array 1 should be replaced by active_lession and active_learning_lession of Array 2 respectively.
Array 1 =>
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 10
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Array 2=>
array:3 [
0 => array:3 [
"id" => 3
"active_learning_lession" => 25
"active_lession" => 20
]
1 => array:3 [
"id" => 4
"active_learning_lession" => 20
"active_lession" => 15
]
]
Thus Expected Array will be
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 20
"active_learning_lession" => 25
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 15
"active_learning_lession" => 20
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Try this code..
$res = [];
foreach($x as $key => $xx)
{
foreach($y as $k => $yy)
{
if($xx['id'] == $yy['id'])
{
$res[$key] = $xx;
$res[$key]['active_lession'] = $yy['active_lession'];
$res[$key]['active_learning_lession'] = $yy['active_learning_lession'];
}
else
{
if(!array_key_exists($key,$res))
{
$res[$key] = $xx;
}
}
}
}
print_r($res);
OR
foreach($x as $key => $value)
{
foreach($y as $yy)
{
if($value['id'] == $yy['id'])
{
$x[$key]['active_learning_lession'] = $yy['active_learning_lession'];
$x[$key]['active_lession'] = $yy['active_lession'];
}
}
}
print_r($x);
Output will be
Array
(
[0] => Array
(
[id] => 3
[status] => 1
[active_lession] => 20
[active_learning_lession] => 25
[learninglessions] => Array
(
[0] => Array
(
[id] => 2
[language_id] => 1
[category_id] => 3
[sentence] => jhdbfhbs
[english_sentence] => I am student of …… School.
)
[1] => Array
(
[id] => 27
[language_id] => 1
[category_id] => 3
[sentence] => dbshbfjhf
[english_sentence] => He is my friend.
)
)
)
[1] => Array
(
[id] => 4
[name] => Module 2
[image] => public/icon/downloadxxx.jpeg
[status] => 1
[active_lession] => 15
[active_learning_lession] => 20
[learninglessions] => Array
(
[0] => Array
(
[id] => 1
[language_id] => 1
[category_id] => 4
[sentence] => jhbdsfhjferu
[english_sentence] => I am...
)
)
)
[2] => Array
(
[id] => 5
[status] => 1
[active_lession] => 0
[active_learning_lession] => 0
[learninglessions] => Array
(
[0] => Array
(
[id] => 29
[language_id] => 1
[category_id] => 5
[sentence] => jbfhgbdu
[english_sentence] => This is a Park.
)
[1] => Array
(
[id] => 34
[language_id] => 1
[category_id] => 5
[sentence] => jhsbdhjfbuyefr
[english_sentence] => How are things ?
)
)
)
)

how to sort array data in alphabetic key order in php

my collection of data in array which is shown below the index key is A,B,C but i want to store these key in "key" and its key letter's words in "dishes" key
array:3 [
"A" => array:4 [
0 => 37
1 => "Algerian"
2 => 6
3 => "American"
]
"B" => array:6 [
0 => 27
1 => "Belgian"
2 => 20
3 => "Brazilian"
]
and so on..
i wanna sort this array like aplhabetic order as shown below
array:10 [
0 => array:2 [
"key" => "A"
"dishes" => array:2 [
0=>array:2[
"id" => 37
"type" => "Algerian"
],
1=>array:2[
"id" => 6
"type" => "American"
]
]
]
1 => array:2 [
"key" => "B"
"dishes" => array:2 [
0=>array:2[
"id" => 27
"type" => "Belgian"
],
1=>array:2[
"id" => 20
"type" => "Brazilian"
]
]
]
and so on...
This would be a possible solution:
<?php
$input = [
'A' => [
0 => 37,
1 => "Algerian",
2 => 6,
3 => "American"
],
'B' => [
0 => 27,
1 => "Belgian",
2 => 20,
3 => "Brazilian"
]
];
$output = [];
array_walk($input, function($values, $key) use (&$output) {
$entry = [
'key' => $key,
'dishes' => []
];
foreach(array_chunk($values, 2) as $chunk) {
$entry['dishes'][] = [
'id' => $chunk[0],
'type' => $chunk[1]
];
}
$output[] = $entry;
});
print_r($output);
The output of above code obviously is:
Array
(
[0] => Array
(
[key] => A
[dishes] => Array
(
[0] => Array
(
[id] => 37
[type] => Algerian
)
[1] => Array
(
[id] => 6
[type] => American
)
)
)
[1] => Array
(
[key] => B
[dishes] => Array
(
[0] => Array
(
[id] => 27
[type] => Belgian
)
[1] => Array
(
[id] => 20
[type] => Brazilian
)
)
)
)
You have to loop through the original array to create your new structure. Then you can use the ksort function to sort them.
$newArr = new array();
for ($arr as $elem) {
$dishArr = new array();
for($elem['dishes'] as $dish) {
$dishArr[] = $dish['id'];
$dishArr[] = $dish['type'];
}
$newArr[$elem['key']] = $dishArr;
}
ksort($newArr);

mapping value to one key in php array

I have a array with many key and value pairs. I want to get all value with one key in php array.
I Have result like
$seat=[
(int) 0 => [
(int) 0 => 'A_1',
(int) 1 => 'A_2',
(int) 2 => 'A_3',
(int) 3 => 'A_4'
],
(int) 1 => [
(int) 0 => 'B_1',
(int) 1 => 'B_2',
(int) 2 => 'B_3'
]
]
i want to change like following syntax.
$seat=[
(int) 0 => [
(int) 0 => 'A_1',
(int) 1 => 'A_2',
(int) 2 => 'A_3',
(int) 3 => 'A_4'
(int) 4 => 'B_1',
(int) 5 => 'B_2',
(int) 6 => 'B_3'
]
]
thank all:
try this,
<?php
$result = array();
$seat=array(
"0" => array("0" => 'A_1',"1" => 'A_2',"2" => 'A_3',"3" => 'A_4'),
"1" => array("0" => 'B_1',"1" => 'B_2',"2" => 'B_3'),
"2" => array("0" => 'C_1',"1" => 'C_2',"2" => 'C_3'));
for($i=0;$i<count($seat);$i++){
$result = array_merge($result,$seat[$i]);
}
echo "<pre>";print_r($result);
?>
OUTPUT
Array
(
[0] => A_1
[1] => A_2
[2] => A_3
[3] => A_4
[4] => B_1
[5] => B_2
[6] => B_3
[7] => C_1
[8] => C_2
[9] => C_3
)
Simply using array_merge function.
$result = call_user_func_array('array_merge', $seat);
WORKING DEMO: http://phpio.net/s/1tx5
You can simply do it like this:
function flattenArray($arr) {
$toflat = array($arr);
$res = array();
while (($r = array_shift($toflat)) !== NULL) {
foreach ($r as $v) {
if (is_array($v)) {
$toflat[] = $v;
} else {
$res[] = $v;
}
}
}
return $res;
}
$newArray = array(flattenArray($array1)); // This will be your desired result
OUTPUT:
// So for example, if you pass array to flatternArray($array) method:
$array = array:2 [
0 => array:3 [
0 => 1
1 => 2
2 => 3
]
1 => array:3 [
0 => 4
1 => 5
2 => 6
]
]
// then $newArray will results in:
array:1 [
0 => array:6 [
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
]
]
Hope this helps!
Since your input is an indexed array, you can just flatten the structure (with a classic splat&merge technique) and push the merged structure into an empty array to achieve the desired result (no loop required).
Code: (Demo)
$seat = [
['A_1', 'A_2', 'A_3', 'A_4'],
['B_1', 'B_2', 'B_3'],
['C_1']
];
var_export(
[array_merge(...$seat)]
);
Output:
array (
0 =>
array (
0 => 'A_1',
1 => 'A_2',
2 => 'A_3',
3 => 'A_4',
4 => 'B_1',
5 => 'B_2',
6 => 'B_3',
7 => 'C_1',
),
)

Categories