Two array combine or flipped - php

I have this two arrays:
array:4 [▼
0 => 518
1 => 519
2 => 520
3 => 521
]
and this one:
array:4 [▼
0 => "1"
1 => "2"
2 => "3"
3 => "3"
]
Can someone please help me how to achieve like this..
array:4 [▼
518=>1
519=> 2
520 => 3
521 => 3
]
I do not know if it is possible or not like

You can use built in function array_combine to achieve this:
$arr1 =array(
0 => 518,
1 => 519,
2 => 520,
3 => 521,
);
$arr2 =array(
0 => "1",
1 => "2",
2 => "3",
3 => "3",
);
$new_array = array_combine($arr1,$arr2);
print_r($new_array);
DEMO

You can loop through and use the key to associate between your arrays. You have a little type juggling to do.
<?php
$one =
[
0 => 518,
1 => 519,
2 => 520,
3 => 521
];
$two =
[
0 => "1",
1 => "2",
2 => "3",
3 => "3"
];
$desired =
[
518 => 1,
519 => 2,
520 => 3,
521 => 3
];
foreach($one as $k=>$v)
{
$out[$v] = (int) $two[$k];
}
if($desired === $out) {
echo 'All good.';
}
Output:
All good.

Related

Merge with count for key value pair in associative array

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

Merge 2 arrays having same number of items - php [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
The number of items in both arrays $newResponse & $key will always be same.For each item of the $newResponse, i wanted to add items of $key to $newResponse in the same order they exists in $key array as mentioned in the Expected result.How can i achieve that?
dump($newResponse); is an array like below,which am getting as the result of a foreach loop.
array:4 [
0 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
1 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
2 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
3 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
]
dump($key); is an array like below, which is the result of another foreach loop.
array:4[
0=>[
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
1=> [
"totalPoints" => 10
"percent" => 2
"id" => 3
]
2=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
3=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
Expected result:
[
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 10
"percent" => 2
"id" => 3
]
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
Here's one way to do it:
Assuming your data is:
$newResponse = [
[
"courseId" => 18,
"courseDisplayName" => "qqq"
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
],
[
"courseId" => 18,
"courseDisplayName" => "qqq",
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
]
];
$key = [
[
"totalPoints" => 2,
"percent" => 1.0,
"id" => 2
],
[
"totalPoints" => 10,
"percent" => 2,
"id" => 3
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 6
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 5
]
];
Then I'd probably reach for array_map, having used it for this kind of purpose recently:
$out = [];
array_map(function($a,$b) use (&$out) {
$out[] = $a + $b;
},$newResponse,$key);
print_r($out);
Note in the above the + is very similar to array merge and I believe interchangeable in your use case. You can read about the difference here: Array_merge versus +

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

Merging Multi-dimensional Array (Custom) PHP - Laravel

Merge a multi-dimensional array to the custom array as required.
Need the solution in PHP (using in Laravel-8).
Custom array is needed to make rows for creating an excel file using Spreadsheet.
This is the Original array I have =
array:3 [
0 => array:4 [
0 => array:3 [
0 => "Name"
1 => "English"
2 => "Math"
]
1 => array:3 [
0 => "John"
1 => 10
2 => 20
]
2 => array:3 [
0 => "Doe"
1 => 20
2 => 30
]
3 => array:3 [
0 => "Smith"
1 => 30
2 => 50
]
]
1 => array:4 [
0 => array:3 [
0 => "Name"
1 => "Science"
2 => "Hindi"
]
1 => array:3 [
0 => "John"
1 => 10
2 => 20
]
2 => array:3 [
0 => "Doe"
1 => 20
2 => 57
]
3 => array:3 [
0 => "Smith"
1 => 30
2 => 89
]
]
2 => array:4 [
0 => array:3 [
0 => "Name"
1 => "ABC"
2 => "XYZ"
]
1 => array:3 [
0 => "John"
1 => 10
2 => 20
]
2 => array:3 [
0 => "Doe"
1 => 20
2 => 23
]
3 => array:3 [
0 => "Smith"
1 => 30
2 => 89
]
]
]
From the above array need to make the array like below (array size may very, so need dynamic solutions) -
array:1 [
0 => array:4 [
0 => array:7 [
0 => "Name"
1 => "English"
2 => "Math"
3 => "Science"
4 => "Hindi"
5 => "ABC"
6 => "XYZ"
]
1 => array:7 [
0 => "John"
1 => 10
2 => 20
3 => 10
4 => 20
5 => 10
6 => 20
]
2 => array:7 [
0 => "Doe"
1 => 20
2 => 30
3 => 20
4 => 57
5 => 20
6 => 23
]
3 => array:7 [
0 => "Smith"
1 => 30
2 => 50
3 => 30
4 => 89
5 => 30
6 => 89
]
]
]

Merging one array into another

I have 2 arrays
Array 1:
array:3 [▼
0 => 1
1 => 2.3
2 => 4.5
]
Array 2:
array:3 [▼
0 => array:2 [▼
"name" => "john"
"age" => 34
]
1 => array:2 [▼
"name" => "doe"
"age" => 12
]
2 => array:2 [▼
"name" => "kelvin"
"age" => 14
]
]
How do I merge array 1 into array 2 so that I have something like this-
array:3 [▼
0 => array:3 [▼
"name" => "john"
"age" => 34,
"score" => 1
]
1 => array:3 [▼
"name" => "doe"
"age" => 12,
"score" => 2.3
]
2 => array:3 [▼
"name" => "kelvin"
"age" => 14,
"score" => 4.5
]
]
Notice that the values of array 1 now have keys called 'score'.
You can use foreach loop with reference &:
$ar = [1,2,3.4];
$ar2 = [['name'=>'Joe','age' => 33],['name'=>'Joe2','age' => 33],['name'=>'Joe3','age' => 33]];
foreach($ar2 as $ind=>&$person){
$person['score'] = $ar[$ind];
}
print_r($ar2);
Demo
Output:
Array
(
[0] => Array
(
[name] => Joe
[age] => 33
[score] => 1
)
[1] => Array
(
[name] => Joe2
[age] => 33
[score] => 2
)
[2] => Array
(
[name] => Joe3
[age] => 33
[score] => 3.4
)
)
You can also use array_walk to walk through the array.
<?php
$a = [1,2.3,4.5];
$b = [
["name" => "john", "age" => 34],
["name" => "doe","age" => 12],
["name" => "kelvin", "age" => 14]
];
array_walk($a,function($val,$key) use (&$b){
$b[$key]['score'] = $val;
});
print_r($b);
Demo: https://3v4l.org/58rXG

Categories