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]; })
Related
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().
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);
This question already has answers here:
Move an array element to a new index in PHP
(9 answers)
Closed 4 years ago.
Given the following array:
$arr = array ([0] => "apple", [1] => "pineapple", [2] => "fruit");
How can I place "fruit", if exists in the array, to the first index, pushing the others forward?
Expected result:
$arr = array ([0] => "fruit", [1] => "apple", [2] => "pineapple");
Try with array_search() and array_unshift()
<?php
$arr = array ("apple","pineapple","fruit");
$fruit_key = array_search('fruit', $arr);
if($fruit_key){
$fruit_value = $arr[$fruit_key];
unset($arr[$fruit_key]);
array_unshift($arr, $fruit_value);
print_r($arr);
}
?>
Output:
Array (
[0] => fruit
[1] => apple
[2] => pineapple
)
DEMO: https://3v4l.org/bir6i
This question already has answers here:
PHP: merge two arrays while keeping keys instead of reindexing?
(6 answers)
Closed 4 years ago.
I have a multiple arrays which I'd like to put into a single array in order to sort it:
$weight = array($weight);
$dev = array_combine($missing, $weight);
echo "<pre>";
print_r($dev);
echo "</pre>";
Output:
Array (
[angular] => 2
)
Array (
[android sdk] => 3
) Array (
[application] => 1
)
Now how do I turn the array above into this?
Array (
[android sdk] => 3
[angular] => 2
[application] => 1 )
I've tried the below from a solution that I've found on this site, but it returns NULL:
$weight = array($weight);
$dev = array_combine($missing, $weight);
$result = call_user_func_array("array_merge", $dev);
echo "<pre>";
print_r($result);
echo "</pre>";
EDIT
Here is my $missing array, some arrays are empty because a match hasn't been found against some keywords:
Array
(
)
Array
(
[0] => angular
)
Array
(
[0] => android sdk
)
Array
(
[0] => application
)
Array
(
)
Here are the value from $weight:
3 2 3 1 3
How can I get this?
Array (
[android sdk] => 3
[angular] => 2
[application] => 1 )
use array_merge:
$array1 = [1,2,3];
$array2 = [4,5,6];
$result = array_merge($array1, $array2);
print_r($result);
results in:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
You can use array_merge function.
Therefore, the code will be
array_merge($array1, $array2);
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
)
)