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" ?
Related
I have 3 arrays that I need to merge together but can't figure out how?
array 1
array:4 [▼
0 => "admin98#wassiah.test"
1 => "admin69#wassiah.test"
2 => "admin25#wassiah.test"
3 => null
]
array 2
array:4 [▼
0 => "one"
1 => "three"
2 => "two"
3 => null
]
array 3
array:4 [▼
0 => "10"
1 => "11"
2 => null
3 => null
]
And I need to make new array like this:
array:4 [▼
0 => array:2 [▼
"email" => "admin98#wassiah.test"
"name" => "one"
"id" => "10"
]
1 => array:2 [▼
"email" => "admin69#wassiah.test
"name" => "three"
"id" => "11"
]
2 => array:2 [▼
"email" => "admin25#wassiah.test"
"name" => "two"
"id" => null
]
3 => array:2 [▼
"email" => null
"name" => null
"id" => null
]
]
Code
$mails = $request->input('mails'); // array 1
$names = $request->input('names'); // array 2
$heirIds = $request->input('ids'); // array 3
Any idea?
You can try the below code
<?php
$arr1 = array(
"admin98#wassiah.test",
"admin97#wassiah.test",
"admin96#wassiah.test",
"",
);
$arr2 = array(
"one",
"three",
"two",
"",
);
$arr3 = array(
"10",
"11",
"",
"",
);
$result = array();
$count = count($arr1);
for($i=0;$i<$count;$i++){
$result[$i]['email'] = $arr1[$i];
$result[$i]['name'] = $arr2[$i];
$result[$i]['id'] = $arr3[$i];
}
echo '<pre>'; print_r($result);
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 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];
}
}
}
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.
Assuming I don't want to loop through and build a new array, is there a built in way in PHP to add two arrays together and push all keys from the second array after the keys from the first? I Googled around and couldn't find anything that does exactly this, but wondering if anyone might know
For example to combine these..
array( 0 => "a", 1 => "b" );
array ( 0 => "c", 1 => "d" );
and get this..
array( 0 => "a", 1 => "b", 2 => "c", 3 => "d" );
This:
array_merge(array( 0 => "a", 1 => "b" ),array ( 0 => "c", 1 => "d" ));
Or
array( 0 => "a", 1 => "b" ) + array ( 0 => "c", 1 => "d" )
This first one will overwrite duplicate keys, the second will not. And you may have to sort the array afterwords.
Or, you could do:
array_merge(array_values(array( 0 => "a", 1 => "b" )), array_values(array ( 0 => "c", 1 => "d" )))
That will definitely work
Take a look at array_merge.
<?php
$ab = array('a', 'b');
$cd = array('c', 'd');
var_dump(
array_merge($ab, $cd)
);
/*
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
*/
You could also try:
<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = array_merge($array1, $array2);
print_r($result);
/*
Array
(
[0] => zero_a
[1] => two_a
[2] => three_a
[3] => one_b
[4] => three_b
[5] => four_b
)
*/
?>
Reference