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 ?
)
)
)
)
Related
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
]
]
]
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
I Would Like To Get The First Element Of This Array And Put In New Same Array Output
One Requirement: It Cannot Be Done With Passing By reference Index eg 0
This Input Array
[ 'id','ID','dt-text' ] ,
[ 'name','Name','dt-text' ] ,
[ 'artistList'=>['list','mm','defalut'] ,'Artist List','dt-select'] ,
[ 'nationality'=>['nationality','mm','defalut'] ,'Nationality','dt-select'] ,
[ 'view','View',''],
[ 'status','Status' ,'']
array:6 [▼
0 => array:3 [▼
0 => "id"
1 => "ID"
2 => "dt-text"
]
1 => array:3 [▼
0 => "name"
1 => "Name"
2 => "dt-text"
]
2 => array:3 [▼
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
0 => "Artist List"
1 => "dt-select"
]
3 => array:3 [▼
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
0 => "Nationality"
1 => "dt-select"
]
4 => array:3 [▼
0 => "view"
1 => "View"
2 => ""
]
5 => array:3 [▼
0 => "status"
1 => "Status"
2 => ""
]
]
The New Array I Needed
This IS OutPUT Array
['id','name','artistList'=>['list','mm','defalut'] ,'nationality'=>['nationality','mm','defalut'] ,'view','status']
array:6 [▼
0 => "id"
1 => "name"
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
2 => "view"
3 => "status"
]
Note
I Can Controll in Input Array Same , I Try with foreach in php And Tray In Laravel Helper Function head Put I get S
array:6 [▼
0 => "id"
1 => "name"
2 => array:1 [▼
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
]
3 => array:1 [▼
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
]
4 => "view"
5 => "status"
]
Put I Cant Get Resslut So , How Can I Do this?
Since you are changing the keys (structure) of the array, there is no way to do that without either generating a new array or passing the array by reference. One way to do it by generating a new array is with array_reduce:
$array = [
[ 'id','ID','dt-text' ] ,
[ 'name','Name','dt-text' ] ,
[ 'artistList'=>['list','mm','defalut'] ,'Artist List','dt-select'] ,
[ 'nationality'=>['nationality','mm','defalut'] ,'Nationality','dt-select'] ,
[ 'view','View',''],
[ 'status','Status' ,'']
];
$array = array_reduce($array, function ($c, $v) {
$first_key = array_keys($v)[0];
return array_merge($c, array($first_key => $v[$first_key])); }, []);
print_r($array);
Output:
Array (
[0] => id
[1] => name
[artistList] => Array (
[0] => list
[1] => mm
[2] => defalut
)
[nationality] => Array (
[0] => nationality
[1] => mm
[2] => defalut
)
[2] => view
[3] => status
)
Demo on 3v4l.org
I have an array of data like this that is passed to my controller via a form. It's being collected in a javascript function. It will always be email the name but there could be 2 sets or 100.
array:10 [▼
0 => "ken#email.com"
1 => "Ken"
2 => "robert#email.com"
3 => "Robert"
4 => "robert#email.com"
5 => "Robert"
6 => "mike#email.com"
7 => "Mike"
]
I'm currently doing this
$recipients = array_chunk($recipients, 2);
array:5 [▼
0 => array:2 [▼
0 => "ken#email.com"
1 => "Ken"
1 => array:2 [▼
0 => "robert#email.com"
1 => "Robert"
2 => array:2 [▼
0 => "robert#email.com"
1 => "Robert"
3 => array:2 [▼
0 => "mike#email.com"
1 => "Mike"
]
What I need though is this...
array:5 [▼
0 => array:2 [▼
email => "ken#email.com"
name => "Ken"
1 => array:2 [▼
email => "robert#email.com"
name => "Robert"
2 => array:2 [▼
email => "robert#email.com"
name => "Robert"
3 => array:2 [▼
email => "mike#email.com"
name => "Mike"
]
How? Thanks!
You can re-add the value back to the array with a key and remove its duplicate by the index.
$recipients = array(
0 => "ken#email.com",
1 => "Ken",
2 => "robert#email.com",
3 => "Robert",
4 => "robert#email.com",
5 => "Robert",
6 => "mike#email.com",
7 => "Mike"
);
$recipients = array_chunk($recipients, 2);
for ($i=0; $i < count($recipients); $i++)
{
$recipients[$i]['email'] = $recipients[$i][0];
$recipients[$i]['name'] = $recipients[$i][1];
unset($recipients[$i][0]);
unset($recipients[$i][1]);
}
It would result in the following output:
Array
(
[0] => Array
(
[email] => ken#email.com
[name] => Ken
)
[1] => Array
(
[email] => robert#email.com
[name] => Robert
)
[2] => Array
(
[email] => robert#email.com
[name] => Robert
)
[3] => Array
(
[email] => mike#email.com
[name] => Mike
)
)
I hope it helped you.
Yes you can do that.
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);
?>
add to an array with custom key
<?php
$array= array();
$array[]='name=>bob';
var_dump($array);
Array docs
I've two arrays
118 => array:7 [
"date" => "19.10.2016"
"time1" => "dfg"
"purpose1" => "dfg"
"chair1" => "dfg"
"time2" => "dfg"
"purpose2" => "dfg5"
"chair2" => "5345"
]
123 => array:7 [
"date" => "20.10.2016"
"time1" => "gdf"
"purpose1" => "gdfg"
"chair1" => "gdf"
"time2" => "gdfg"
"purpose2" => "gdf"
"chair2" => "534534"
]
124 => array:7 [
"date" => "20.10.2016"
"time1" => "gdf"
"purpose1" => "gdfg"
"chair1" => "gdf"
"time2" => "gdfg"
"purpose2" => "gdf"
"chair2" => "534534"
]
and
0 => {#231
+"label_id": "D101102E"
+"id": 118
}
1 => {#232
+"label_id": "D1011100"
+"id": 123
}
2 => {#233
+"label_id": "D1011100"
+"id": 124
}
Where id is key in first array, and value in second array. I want add label_id to first array as value in the same id as key. I already try use array_fill_keys and array_push but it's not the point. Thank you
Iterate through the first array, then in a nested loop, iterate through the second array comparing the outer key to the inner id. If you have a match, then append the value to the outer array and continue to the next item.
<?php
$a = [
118 => [
"date" => "19.10.2016",
"time1" => "dfg",
"purpose1" => "dfg",
"chair1" => "dfg",
"time2" => "dfg",
"purpose2" => "dfg5",
"chair2" => "5345",
],
123 => [
"date" => "20.10.2016",
"time1" => "gdf",
"purpose1" => "gdfg",
"chair1" => "gdf",
"time2" => "gdfg",
"purpose2" => "gdf",
"chair2" => "534534",
],
124 => [
"date" => "20.10.2016",
"time1" => "gdf",
"purpose1" => "gdfg",
"chair1" => "gdf",
"time2" => "gdfg",
"purpose2" => "gdf",
"chair2" => "534534",
],
];
$b = [
0 => [
"label_id" => "D101102E",
"id" => 118,
],
1 => [
"label_id" => "D1011100",
"id" => 123,
],
2 => [
"label_id" => "D1011100",
"id" => 124,
],
];
foreach($a as $key => $value){
foreach($b as $k => $v){
if($key === $v['id']){
$a[$key]['label_id'] = $k;
continue;
}
}
}
print_r($a);
Array
(
[118] => Array
(
[date] => 19.10.2016
[time1] => dfg
[purpose1] => dfg
[chair1] => dfg
[time2] => dfg
[purpose2] => dfg5
[chair2] => 5345
[label_id] => 0
)
[123] => Array
(
[date] => 20.10.2016
[time1] => gdf
[purpose1] => gdfg
[chair1] => gdf
[time2] => gdfg
[purpose2] => gdf
[chair2] => 534534
[label_id] => 1
)
[124] => Array
(
[date] => 20.10.2016
[time1] => gdf
[purpose1] => gdfg
[chair1] => gdf
[time2] => gdfg
[purpose2] => gdf
[chair2] => 534534
[label_id] => 2
)
)