I have array, for example:
$array = (5, 2, 17, 9, 12, 6);
and always two values - $first and $last. I would like get all values from $array between $first and $last.
For example:
$first = 2;
$last = 6;
I would like receive:
$receive = array(17, 9, 12);
For example:
$first = 5;
$last = 9;
I would like receive:
$receive = array(2, 17);
How is the best way for that?
Search and slice:
$receive = array_slice($array,
$s=array_search($first, $array)+1,
array_search($last, $array)-$s);
Related
$array1 = [null, 6, null];
$array2 = [1,2,3,4];
How do I find all possible unique combinations of these two lists (Should be the same lenght of array1 and keep the 6 in the same position);
Need to return
[1, 6, 2],[1, 6, 3],[1, 6, 4],[2, 6, 3],[2, 6, 4],[3, 6, 4]
You can use the below logic
$array1 = [null, 6, null];
$array2 = [1,2,3,4];
$final = [];
for($i = 0;$i < count($array2);$i++){
for($j = $i + 1;$j < count($array2);$j++){
for($k = 0;$k < count($array1);$k++){
if(!is_null($array1[$k])){
$final[] = [$array2[$i], $array1[$k], $array2[$j]];
}
}
}
}
print_r($final);
I am working on a multi dimensional array in PHP. How is it possible to select unique values from 0 index of a nested array which has lowest value on index 1?
Below is my sample data array
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
I am trying to get following array as a result
$result = array (
array(1, 3),
array(9, 2),
);
One way to do this could be to loop the array, use the indexes of the arrays and compare the second value. If the stored second value is greater than the new value, then overwrite it.
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
$result = [];
foreach ($data as $d) {
if (!isset($result[$d[0]])) {
$result[$d[0]] = $d;
continue;
}
if ($result[$d[0]][1] > $d[1]) {
$result[$d[0]] = $d;
}
}
print_r($result);
Demo
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
$result = [];
$lowest0 = null;
$lowest1 = null;
foreach($data as $row){
if($lowest0 === null || $row[0] < $lowest0[0] || ($row[0] === $lowest0[0] && $row[1] < $lowest0[1]))
{
$lowest0 = $row;
}
if($lowest1 === null || $row[1] < $lowest1[1] || ($row[1] === $lowest1[1] && $row[0] < $lowest0[0]))
{
$lowest1 = $row;
}
}
$result = [$lowest0,$lowest1];
var_dump($result);
Short and dirty:
$data = array (
array(1, 9),
array(1, 3),
array(1, 5),
array(9, 2),
array(9, 7),
);
foreach ($data as $row) {
($row[1] < ($uniq[$row[0]][1] ?? INF)) and $uniq[$row[0]] = $row;
}
var_dump($uniq);
Pass resulting array through array_values if re-indexing from 0 is important to you
This is as clear as mud, but it I made it as an example that it works without looping.
In short I use min and array_intersect(_key) to find where the minimum values are in the columns (with array_column).
//split out the two columns
$col0 = array_column($data, 0);
$col1 = array_column($data, 1);
// find minimum value in each
$min0 = min($col0);
$min1 = min($col1);
// find what keys this minimum value is in of each array
$mincol0 = array_intersect($col0, [$min0]);
$mincol1 = array_intersect($col1, [$min1]);
// use the previous and compare with minimum value of the other column
$result[] = $data[array_keys(array_intersect_key($col1, $mincol0),min(array_intersect_key($col1, $mincol0)))[0]];
$result[] = $data[array_keys(array_intersect_key($col0, $mincol1),min(array_intersect_key($col0, $mincol1)))[0]];
Var_dump($result);
https://3v4l.org/dgbDK
Since I apperantly missunderstood the question.
Here is another answer, still no need to loop the full array. That is just a waste of time.
Loop only unique column 0 values and find the minmum value in column 1 using array_intersect and array_intersect_key.
$col0 = array_column($data, 0);
foreach(array_unique($col0) as $item){
$intersect = array_intersect($col0, [$item]);
$res[] = min(array_intersect_key($data, $intersect));
}
var_dump($res);
https://3v4l.org/3UPQh
for example i have the following codes in my controller:
foreach($-request->input('text') as $var){
$sum[] = $var;
}
$last = sizeof($request->input('text'));
$i = 0;
while($i < $last)
{
//insert code here
$i++;
}
$request->input('text') has the following values:
['1,2,3,4,5']
how do you add the values in the variable $var[] in eloquent? i tried the sum but it didn't work. someone also said ill use += sign on my increment
explode to convert comma-separated numbers into an array.
intval on all array elements using array_map to cast them into integer.
array_sum to add them all.
Try this.
$array = $request->input('text'); // ['1, 2, 3, 4, 5'];
$string_numbers = explode(', ', $array[0]); // ['1', '2', '3', '4', '5']
$numbers = array_map('intval', $string_numbers); // [1, 2, 3, 4, 5]
echo array_sum($numbers); // 15
$num = ['1,2,3,4,5']; // when $n = $request->input('text');
$numbers = explode(',', $num[0]);
$sum = array_sum($numbers);
dd($sum);
I think this will be helpful to you
Let's say I have two arrays...
$radicand_array = array(3, 5, 5, 2);
$coeff_array = array(-10, 14, 3, -6);
I'd like to be able to determine which (if any) values in $radicand_array match, AND also know what the keys are. So, in this case I would want to know that there's a match on keys 1 and 2 of $radicand_array.
I need to know this because I want to then add the corresponding key values in $coeff_array. So, in this case I would then add 14 and 3 based on the matching 5's in $radicand_array.
I've tried array_count_values(), but it doesn't seem to give the key values like I want. Is there a PHP function ready made for this?
Using the link:
Fixed. Good man!
<?php
// $radicand_array = array(3, 5, 5, 2);
$radicand_array = array(3, 5, 5, 2, 3, 3);
$coeff_array = array(-10, 14, 3, -6);
$unique = array_unique($radicand_array);
$duplicates = array_diff_assoc($radicand_array, $unique);
// Duplicate keys
$aDupes = array_keys(array_intersect($radicand_array, $duplicates));
$iSum = 0;
$iCountDupes = count( $aDupes );
for( $i = 0; $i < $iCountDupes; ++$i )
{
if( !empty( $coeff_array[ $aDupes[ $i ] ] ) )
{
$iSum = $iSum + $coeff_array[ $aDupes[ $i ] ];
}
}
var_dump( $iSum );
?>
This is an edited version of Vladimir Ramik's post to handle multiple sets of matches...
$radicand_array = array(3, 5, 5, 2, 3);
$coeff_array = array(-10, 14, 3, -6, 4);
$unique = array_unique($radicand_array);
$duplicates = array_diff_assoc($radicand_array, $unique);
$new_array = array_chunk($duplicates, 1);
// Duplicate keys
$aDupes = array_keys(array_intersect($radicand_array, $new_array[0]));
$iCountDupes = count( $aDupes );
for($i = 0; $i < $iCountDupes; ++$i){
$ans_coeff_1 += $coeff_array[$aDupes[ $i ]];
}
echo $ans_coeff_1;
// Duplicate keys
$bDupes = array_keys(array_intersect($radicand_array, $new_array[1]));
$iCountDupes = count( $bDupes );
for($i = 0; $i < $iCountDupes; ++$i){
$ans_coeff_2 += $coeff_array[$bDupes[ $i ]];
}
echo '<br>' . $ans_coeff_2;
I want to make an array with a random amount of values between 2 and 5.
Each value would be 50.
What's the easiest way to do this?
eg:
$array = array(50,50,50,50)
$array = array(50,50)
$array = array_fill(0,rand(2,5), 50) is the simplest I can think of.
Try with:
$array = array_fill(0, rand(2, 5), 50);
$array = array();
for($i = 0, $max = rand(2,5); $i < $max; $i++) $array[] = 50;
This will create an array with 2 to 5 items with the value 50.
Edit: An easier solution is to use array_fill():
$array = array_fill(0, rand(2, 5), 50);
Take a look at mt_rand(). To generate a random number between 2 and 5 (inclusive):
$random = mt_rand(2, 5);