I am trying to divide an array based on values within the array, and to sort it based on keys that the array also has. My array currently looks like this:
array:6 [▼
"player_ids" => array:8 [▼
0 => "103"
1 => "221"
2 => "283"
3 => "321"
4 => "333"
5 => "406"
6 => "425"
7 => "428"
]
"game_id" => array:8 [▼
103 => "33058041"
221 => "33058041"
283 => "33058041"
321 => "33058041"
333 => "33058041"
406 => "33058041"
425 => "33058041"
428 => "33058041"
]
"goals" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"assists" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"yellows" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
"red" => array:8 [▼
103 => "0"
221 => "0"
283 => "0"
321 => "0"
333 => "0"
406 => "0"
425 => "0"
428 => "0"
]
]
with arrays with each of the players embedded in a particular field. How would I manipulate this array to make it so that player_ids would become the first key, like so:
"103" => array:6 [▼
player_ids => "103"
game_id => "33058041"
goals => "0"
assists => "0"
yellows => "0"
red => "0"
]
for each of the player id's listed?
you can loop through the player_ids key in your big array and create a new array with the structure you want. Something like this.
$newArray = array();
foreach ($array['player_ids'] as $p){
$newArray[$p] = array (
'player_ids' => $p,
'game_id' => $array['game_id'][$p],
'goals' => $array['goals'][$p],
'assists' => $array['assists'][$p],
'yellows' => $array['yellows'][$p],
'red' => $array['red'][$p]
)
}
EDIT: As I'm posting this, it's really just a slightly different way to do what #Long Kim already posted, although his is a bit more elegant for this scenario. I'd go with that... The method below is better for a larger array though.
You don't provide a name for your 'master array', so I'm calling it $masterArray here. $newArray is what I believe you're looking for.
$newArray = array();
foreach($masterArray['player_ids'] as $playerid){
foreach($masterArray as $key => $value){
if($key=="player_ids"){
$newArray[$playerid][$key] = $playerid;
}
else{
$newArray[$playerid][$key] = $value[$playerid];
}
}
}
Related
I have the following array:
"origem_fornecedor" => array:5 [▼
"A" => 50
"B" => 70
"C" => 50
"D" => 85
"E" => 50
]
And I need to check if the keys present in that array matches the values present in this constant:
const NIVEIS = ['A', 'B', 'C', 'D', 'E'];
Arrays that should fail this test:
"origem_fornecedor" => array:2 [▼
"A" => 50
"B" => 70
]
"origem_fornecedor" => array:7 [▼
"A" => 50
"B" => 70
"C" => 50
"D" => 85
"E" => 50
"F" => 95
"G" => 10
]
if ( array_keys($origem_fornecedor) == NIVEIS ) echo "Ok"; else echo "KO";
PS : What's this syntax : "origem_fornecedor" ?
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
Here is an php array representing years and months:
array:3 [
2017 => array:2 [
0 => "2"
1 => "3"
]
2018 => array:2 [
0 => "1"
1 => "5"
]
2019 => array:3 [
0 => "10"
1 => "12"
2 => "6"
]
]
I want to sort it on the basis of key (descending) on first level and values (descending) on the second level. by this records of latest month in latest year will appear. So the output must be:
array:3 [
2019 => array:3 [
0 => "12"
1 => "10"
2 => "6"
]
2018 => array:2 [
0 => "5"
1 => "1"
]
2017 => array:2 [
0 => "3"
1 => "2"
]
]
This is just a question of applying krsort to the top-level of the array and rsort to each sub-level:
krsort($array);
array_walk($array, function (&$v) { rsort($v); });
Output:
Array
(
[2019] => Array
(
[0] => 12
[1] => 10
[2] => 6
)
[2018] => Array
(
[0] => 5
[1] => 1
)
[2017] => Array
(
[0] => 3
[1] => 2
)
)
Demo on 3v4l.org
This question already has answers here:
Merge rows of data from two 2D arrays (each with a shared and a unique column) based on the shared column value
(3 answers)
Closed 5 months ago.
I wish you could help me ...
I have 2 arrays, which I would like to insert into a 1array, by new_date to which if there is no data is data = '0', if there is no data1 is data1 = '0'.
Sorry if I do not know how to explain it well, I gave a small example. I have already tried array_merge (but everything is separate) and array_combine (it gives an error of Both parameters should have an equal number of elements).
Thank you in advance for all the help you can give ...
array1 = array:5 [▼
0 => array:2 [▼
"data" => 118
"new_date" => "06-2017"
]
1 => array:2 [▼
"data" => 263
"new_date" => "07-2017"
]
2 => array:2 [▼
"data" => 264
"new_date" => "08-2017"
]
3 => array:2 [▼
"data" => 266
"new_date" => "09-2017"
]
4 => array:2 [▼
"data" => 306
"new_date" => "10-2017"
]
5 => array:2 [▼
"data" => 100
"new_date" => "11-2017"
]
array2 = array:6 [▼
0 => array:2 [▼
"data1" => 100
"new_date" => "02-2016"
]
1 => array:2 [▼
"data1" => 170
"new_date" => "06-2017"
]
2 => array:2 [▼
"data1" => 354
"new_date" => "07-2017"
]
3 => array:2 [▼
"data1" => 397
"new_date" => "08-2017"
]
4 => array:2 [▼
"data1" => 421
"new_date" => "09-2017"
]
5 => array:2 [▼
"data1" => 531
"new_date" => "10-2017"
]
Exemple:
array3 = array:7 [▼
0 => array:3 [▼
"data1" => 111
"data" => 0
"new_date" => "02-2016"
]
1 => array:3 [▼
"data1" => 170
"data" => 118
"new_date" => "06-2017"
]
2 => array:3 [▼
"data1" => 354
"data" => 263
"new_date" => "07-2017"
]
3 => array:3 [▼
"data1" => 397
"data" => 264
"new_date" => "08-2017"
]
4 => array:3 [▼
"data1" => 421
"data" => 266
"new_date" => "09-2017"
]
5 => array:3 [▼
"data1" => 531
"data" => 306
"new_date" => "10-2017"
]
6 => array:3 [▼
"data1" => 0
"data" => 100
"new_date" => "11-2017"
]
Use array_combine to set the new_date as keys. Then add the first array element the d1 (as "data1" in your example) with array_map. Finely, loop on second array and add d1 is exist and all the rest with d ("data" in your example) as 0 if not.
You can do it like this:
$a1 = array(["d"=>1, "new_date"=> "06-2017"],["d"=>2, "new_date"=> "02-2016"]);
$a2 = array(["d1"=>3, "new_date"=> "06-2017"],["d1"=>4, "new_date"=> "07-2017"]);
$a1 = array_combine(array_column($a1, "new_date"), $a1);
$a2 = array_combine(array_column($a2, "new_date"), $a2);
// adding default d1 as 0
$a1 = array_map(function ($e) {return array_merge($e, ["d1" => 0]);},$a1);
foreach($a2 as $k => $v) {
if (isset($a1[$k]))
$a1[$k]["d1"] = $v["d1"]; // if new_data exist set only d1
else
$a1[$k] = array_merge($v, ["d" => 0]); //add with d as 0
}
Your result will be in $a1
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