I have 2 arrays that I need to combine or merge together. I am at a bit of a loss as to how to achieve this.
So the first array looks like this:
$arr1 =
Array (
[0] => Array
(
[id] => 7
[round] => 1
)
[1] => Array
(
[id] => 11
[round] => 2
)
....
And the second array looks like this:
$arr2 =
Array (
[round_1] => 21
[round_2] => 32
....
And I need the result to end up like this:
$result =
Array (
[0] => Array
(
[id] => 7
[round] => 1
[disp] => 21
)
[1] => Array
(
[id] => 11
[round] => 2
[disp] => 32
)
...... etc etc
Any ideas on where to get started for this??
Thanks
$i = 1; // Counter
$result = $arr1; // Copy $arr1 to preserve it. (if necessary)
foreach ($result as $x){
$x['disp'] = $arr2['round_' . $i];
$i ++;
}
This will iterate through Array1 setting a value for 'disp' in each sub array. This value will be pulled from Array2 and the round number would be incremented by a basic counter.
You could equally use a standard for loop for this too.
$result = $arr1; // Copy $arr1 to preserve it. (if necessary)
for($i = 0; $i < count($result); $i++){
$result[$i]['disp'] = $arr2['round_' . ($i + 1)];
}
Just make sure you do "$i + 1" when calling the value from Array2 because that doesn't seem to be 0 indexed.
Related
I have an array of 83 arrays (an array that I have a chunk in 83). I'm trying to keep only the three highest values of each array. All the numbers in each array are included between -1 and 1. There is necessarily a 1 in each array that I don't want to count in my three highest values.
Array
(
[0] => Array
(
[1] => 0.5278533158407
[2] => 0.4080014506744
[3] => 0.5086879008467
[5] => 0.3950042642736
[6] => 1
[1] => Array
(
[1] => 1
[2] => 0.52873390443395
[3] => 0.52518076782133
[4] => 0.52983621494599
[5] => 0.54392829322042
[6] => 0.53636363636364
Etc...
I'm trying the below code but it doesn't work.
for ($i = 0; $i < sizeof($list_chunk); $i++) {
arsort($list_chunk[$i]);
}
for ($i = 0; $i < sizeof($list_chunk); $i++) {
array_slice($list_chunk[$i],1,3,true);
}
print("<pre>");
print_r($list_chunk);
print("</pre>");
Someone could help me? Thanks a lot
This solution uses a foreach loop with a reference to the subarray. The subarray is sorted in descending order of size. The first to third elements are extracted. If the first element is 1, then 3 elements are extracted from the 2 element onwards.
foreach($array as &$arr){
rsort($arr);
$start = $arr[0] == 1 ? 1 : 0;
$arr = array_slice($arr,$start,3);
}
Result:
array (
0 =>
array (
0 => 0.5278533158407,
1 => 0.5086879008467,
2 => 0.4080014506744,
),
1 =>
array (
0 => 0.54392829322042,
1 => 0.53636363636364,
2 => 0.52983621494599,
),
)
Full sample to try: https://3v4l.org/pUhic
I have a multidimensional array. I want to get array element which value is greater than 2 and less than 17. My Array is given below:
Array
(
[4] => Array
(
[0] => 17
[1] => 15
[2] => 12
)
[5] => Array
(
[0] => 16
)
[2] => Array
(
[0] => 3
[1] => 1
)
[1] => Array
(
[0] => 2
)
)
I want output like below:
Array
(
[4] => Array
(
[0] => 17
[1] => 15
[2] => 12
)
[5] => Array
(
[0] => 16
)
[2] => Array
(
[0] => 3
[1] => 1
)
)
Please help me how can I do it easy & fast method.
You can use a nested array filter.
$result = array_filter($outer_array, function($inner_array) {
return array_filter($inner_array, function($number) {
return $number > 2 && $number < 17;
});
});
Then inner array filter will result in an empty array being passed to the outer array filter if no values are found in the specified range. The empty array will evaluate to false in the outer filter callback, eliminating that array from the result.
Demo at 3v4l.org.
Generally, we want you to show what you have tried before we'll write code for you, but this one's on the house. For future reference, include some code snippets along with your question to avoid getting downvoted.
$output = array();
for($i = 0; $i < count($arr); $i++)
{
$use = false;
for($j = 0; $j < count($arr[$i]); $j++)
{
if($arr[$i][$j] > 2 and $arr[$i][$j] < 17)
{
$use = true;
break;
}
}
if($use)
$output[] = $arr[$i];
}
return $output;
This question already has answers here:
Finding cartesian product with PHP associative arrays
(10 answers)
Closed 7 years ago.
Initially, I am afraid, that I do not find a better title for this question.
I have a two dimensional array that looks like this, for example:
[0] => Array
(
[0] => 10,00
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => true
[1] => false
)
i'd like now to convert/parse this into a two dimensional array that looks like this:
[0] => Array
(
[0] => 10,00
[1] => 3
[2] => true
)
[1] => Array
(
[0] => 10,00
[1] => 4
[2] => true
)
[2] => Array
(
[0] => 10,00
[1] => 3
[2] => false
)
[3] => Array
(
[0] => 10,00
[1] => 4
[2] => false
)
i hope you see, that the result should provide all sort of possible combinations. indeed, the length of the first array can differ.
i'd be interested in how to solve this algorithmically, but at the moment i have no idea.
i am not sure, if this is as easy as it looks like. thank you in advance.
I imagine this could be refined, but it should do the trick:
<?php
$arrStart = array(
array('10,00'),
array(3, 4),
array('true', 'false')
);
$arrPositions = array();
$arrResult = array();
//get a starting position set for each sub array
for ($i = 0; $i < count($arrStart); $i++)
$arrPositions[] = 0;
//repeat until we've run out of items in $arrStart[0]
while (array_key_exists($arrPositions[0], $arrStart[0])) {
$arrTemp = array();
$blSuccess = true;
//go through each of the first array levels
for ($i = 0; $i < count($arrStart); $i++) {
//is there a item in the position we want in the current array?
if (array_key_exists($arrPositions[$i], $arrStart[$i])) {
//add that item to our temp array
$arrTemp[] = $arrStart[$i][$arrPositions[$i]];
} else {
//reset this position, and raise the one to the left
$arrPositions[$i] = 0;
$arrPositions[$i - 1]++;
$blSuccess = false;
}
}
//this one failed due to there not being an item where we wanted, skip to next go
if (!$blSuccess) continue;
//successfully adding nex line, increase the right hand count for the next one
$arrPositions[count($arrStart) - 1]++;
//add our latest temp array to the result
$arrResult[] = $arrTemp;
}
print_r($arrResult);
?>
This question already has answers here:
Cartesian Product of N arrays
(10 answers)
Closed 8 years ago.
Let's assume I have an two arrays and I want to merge every value with the other value of the array.
Array 1
array (size=2)
0 => 1
1 => 2
Array 2
array (size=2)
0 => 3
1 => 4
Wanted result array / string:
array (size=4)
0 => '1,3'
1 => '1,4'
2 => '2,3'
3 => '2,4'
I can't get my head around it. Obviously I would need to merge every one array key/value with the other ones. Is there a more elegant way then doing this in a while/foreach loop?
You need a foreach loop inside a foreach loop. (Actualy, you will have to loop through both arrays to get a concatenated product of both arrays, you don't actually need two foreach loops). You could mix: whiles, foreach, for, or php filter/intersect array functions
Example
$array1 = array(1,2);
$array2 = array(3,4);
$result = array();
foreach ($array1 as $item1){
foreach($array2 as $item2){
$result[] = $item1.','.$item2;
}
}
https://eval.in/215001
your result array Length will be array1.Length * array2.Length
2d arrays
You could also put an array inside an array like this:
$array1 = array(1,2);
$array2 = array(3,4);
$result = array();
foreach ($array1 as $item1){
foreach($array2 as $item2){
$result[] = array($item1, $item2);
}
}
//$result[0][0] = 1 -- $result[0][1] = 3
//$result[1][0] = 1 -- $result[1][1] = 4
//$result[2][0] = 2 -- $result[2][1] = 3
//$result[3][0] = 2 -- $result[3][1] = 4
We call this a 2d (2 dimensional) array, because you could grapicly display this as a grid, like displayed here above. If you would put an Array, inside an Array inside an Array, you would call this a 3 dimensional array, etc.
print_r($result); in php:
Array
(
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 1
[1] => 4
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 2
[1] => 4
)
)
try
$a= array ('0' => 1,'1' => 2);
$b= array ('0' => 3,'1' => 4);
for($i=0; $i<count($a); $i++) {
for($j=0; $j<count($b); $j++) {
$newarr[]= $a[$i].','.$b[$j];
}
}
print_r($newarr);//Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )
$a=array('1','2');
$b=array('3','4');
$res=array();
for($i=0;$i<count($a);$i++)
{
foreach($b as $bb)
{
$res[]=strval($a[$i].','.$bb);
}
}
print_r($res);//output=Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 )
There is an array containing K elements. What's the best way to get chunks of N < K items from this array?
Example input:
$x = [1,2,3,4,5,6,7,8,9,10]; // K = 10
Desired result, when N = 3;
$x1 = [1,2,3];
$x2 = [4,5,6];
$x3 = [7,8,9];
$x4 = [10];
Obviously, there is no need to store the result in variables. As long as it's possible to process it by foreach (or any other iteration logic), it should be fine.
The problem with array_slice is that it does not remove the N-slice from the beginning of the array. The problem with array_shift is that it does not support shifting more than 1 item at once. Is there anything more elegant than iterating over array_shift?
array_chunk is what you need.
<?php
$x = [1,2,3,4,5,6,7,8,9,10];
print_r(array_chunk($x,3));
OUTPUT :
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
[3] => Array
(
[0] => 10
)
)
Look into array_chunk -> http://www.w3schools.com/php/func_array_chunk.asp
$x = [1,2,3,4,5,6,7,8,9,10];
print_r(array_chunk($x,3,true));
or you could do it this way -
$x = [1,2,3,4,5,6,7,8,9,10];
$chunks = array();
while(count($x)){
$chunks[] = array_splice($x, 0,3,array());
$i++;
}
Could someone tell me which method would be more efficient?
array_chunk function is used to create sub arrays of equal size.
E.g.
$a=array_chunk($array,3);