select min (lowest) or max (bigest) from array float number [duplicate] - php

This question already has answers here:
Get the minimum and maximum values in an array column
(7 answers)
Closed 4 months ago.
Halo people.
There is possible to select select min (lowest) or max (bigest) from array float number?
Im try min(array) and max(array) but not working?
I can not found on manual.
Can you help me?
The array comes from sql
Array ( [0] => 1.11954 ) Array ( [0] => 1.11983 ) Array ( [0] => 1.11854 ) Array ( [0] => 1.11978 ) Array ( [0] => 1.1198 ) Array ( [0] => 1.12024 ) Array ( [0] => 1.11994 ) Array ( [0] => 1.12055 ) Array ( [0] => 1.12106 ) Array ( [0] => 1.12186 ) Array ( [0] => 1.12191 ) Array ( [0] => 1.1214 ) Array ( [0] => 1.12432 ) Array ( [0] => 1.12398 )
for ($list = 1; $list <= $rezult; $list++)
{
$_array=array($rekord['xxx'])
}
print_r($_array);
$_min=min($_array);
$_max=max($_array);

Your problem is that your array is an array of arrays, not an array of floating point numbers, so to find the min/max values you effectively need to flatten the array, which you can do with array_column:
$flat = array_column($array, 0);
echo min($flat), " ", max($flat);
Output:
1.11854 1.12432
Demo on 3v4l.org
Alternatively you can recode your loop to push values, rather than arrays into it:
for ($list = 1; $list <= $rezult; $list++) {
$_array[] = $rekord['xxx']
}
echo min($_array) . " " . max($_array);

Related

Array values to single array using foreach loop in PHP

I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)

Create a multidimensional array based on number of values [duplicate]

This question already has answers here:
Transpose a PHP multidimensional array with predefined keys
(3 answers)
How to transpose a multidimensional multi-file upload submission and maintain associative keys?
(4 answers)
Closed 1 year ago.
i have an array that i get from a form with alot of fields, i need to order those fields under an array and that array under the ''main'' array. Ill post what i have, and what i need to get.
WHAT I HAVE:
Array
(
[perfil_area1] => Array
(
[0] => a2
[1] => a3
)
[perfil_years] => Array
(
[0] => 2
[1] => 4
)
[perfil_function] => Array
(
[0] => f1
[1] => f4
)
[perfil_obs] => Array
(
[0] => teste1
[1] => teste2
)
[perfil_company] => Array
(
[0] => emp1
[1] => emp2
)
)
This is what i need to be so i can turn it into a query:
What i need
Array
(
[0] =>
(
[perfil_area1] => a2
[perfil_years] => 2
[perfil_function] => f1
[perfil_obs] => teste1
[perfil_company] => emp1
)
[1] =>
(
[perfil_area1] => emp2
[perfil_years] => 2
[perfil_function] => f4
[perfil_obs] => 4
[perfil_company] => a3
)
)
i have tried with 2 foreach but i didnt manage to get it done. I have read Create a multidimensional array in a loop , how to create multidimensional array using a foreach loop in php? , create multidimensional array using a foreach loop , Converting an array from one to multi-dimensional based on parent ID values and some more but i still cant do it. Any tips on how to create it?
You just need to loop over the input array and build the new array
$new = [];
foreach ($in as $key=>$arr) {
$new[0][$key] = $arr[0];
$new[1][$key] = $arr[1];
}
<?php
$result = [];
for ($i=0; $i<count(reset($array)); $i++) {
$result[] = array_combine(array_keys($array), array_column($array, $i));
}
Please read the manual for more details about array_combine(), array_keys() and array_column().

PHP - Dealing with duplicated array keys (internal pointers)

I have the following lines of code that fetches some data from my database. I need to reference some specific contents of the data but I am having an issue with duplicated array keys. i do not want to sort through the data using SQL statements due to the complexity of the project.
Upon checking the pointers of the array, the following was generated.
( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 )
A sample of the printed array :
( [0] => 3371450.18 ) Array ( [0] => 54459051.95 ) Array ( [0] => 210382.52 ) Array ( [0] => 6860440.01 ) Array ( [0] => 13131358.12 )
Since all the array keys are duplicated some PHP array functions like(array_sum, array_max) etc do work on the array and its getting really frustrating.
I wonder if this is because of the while loop. Are array pointers within a while loop always duplicated.
`
if(isset($_SESSION['sess']) && !empty(isset($_SESSION['sess']))){
echo "Session id is ".$_SESSION['sess']." exist";
$currentSession = $_SESSION['sess'];
$sql = "SELECT * FROM `loanbook` WHERE LoanBookSessionId='$currentSession'";
$result = mysqli_query($dbs,$sql);
$teamselection = "SELECT * FROM `Teams`";
$teamresult = mysqli_query($dbs,$teamselection);
while($row = mysqli_fetch_array($result)){
?>
<?php
$Sn[] = $row['Id'];
$Team = $row['Team'];
$CounterClass = $row['CounterPartyClassification'];
$GrossL = $row['GrossLoan'];
$CollType = $row['CollateralType'];
print_r(array_keys(array($GrossL)));
print_r((array($GrossL)));
}
?>`
It looks as if you have a 2d array, where each of the inner arrays contains a string that represents a float.
Mapping each one of the inner arrays to a parsed float value should provide the data structure you need.
<?php
$x = [
["1.43434"],
["1.43434"],
];
echo json_encode($x) . "\n";
echo json_encode(array_map(function($t) {
return floatval($t[0]);
}, $x));
output
▶ php test.php
[["1.43434"],["1.43434"]]
[1.43434,1.43434]

How to merge two index of array in php? [duplicate]

This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 7 years ago.
How to merge only index of array itself, I have only one and I want to combine its index, I want to make 3d to 2d array. I just want to combine index of one array with in one index.This is one array,I am not asking for merge two different array.
Array
(
[0] => Array
(
[East] => 2
)
[1] => Array
(
[North] => 2
)
)
Now I want to format this array like below format.there can be nth no of indexes with in same array.
Array
(
[0] => Array
(
[East] => 2
[North] => 2
)
)
<?php
$a = array(0 => array('East' => 2), 1 => array('North' => 2));
$b[0] = $a[0];
for($i = 1; $i < count($a); $i++) {
$key = array_keys($a[$i])[0];
$b[0][$key] = $a[$i][$key];
}
print_r($b);
?>
Output:
Array
(
[0] => Array
(
[East] => 2
[North] => 2
)
)

PHP multi dimensional arrays [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I have two questions.
How can i create an array which i can add two values per index like:
$sample[0] = ("abc",10);
Second is that once i have created this array i would like to sort this array according to the 2nd value at the index.
So if i have an array like:
$sample[0] = ("abc",32);
$sample[1] = ("def",11);
The sorted result should be:
$sample[0] = ("def",11);
$sample[1] = ("abc",32);
Answer to part one:
$sample[0] = array("abc", 10);
Answer to part two:
array_multisort($sample, SORT_NUMERIC);
Testing Environment:
<?php
$sample[0] = array("abc", 32);
$sample[1] = array("def", 11);
print_r($sample);
array_multisort($sample, SORT_NUMERIC);
echo '<br />';
print_r($sample);
?>
Output:
Array ( [0] => Array ( [0] => abc [1] => 32 ) [1] => Array ( [0] => def [1] => 11 ) )
Array ( [0] => Array ( [0] => def [1] => 11 ) [1] => Array ( [0] => abc [1] => 32 ) )
Warning from #Deceze:
Above functionality is coincidental; correct code is:
usort($sample, function ($a, $b) { return $a[1] - $b[1]; })

Categories