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
)
)
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:
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 Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I have an multidimensional array and want to sort in according to date and also want to get only 5 data from an array.
Array :
Array
(
[0] => Array
(
[ID] => 1
[TITLE] => example1
[DATE] => 2016-05-17
[PST_BY] => 0
[IMG_NM] =>
[SLUG] =>
[NAME] => Web Design & Development
)
[1] => Array
(
[ID] => 2
[TITLE] => example2
[DATE] => 2016-05-20
[PST_BY] => 0
[IMG_NM] =>
[SLUG] =>
[NAME] => Mobile OS
)
)
I am doing this but not working :
$list = array_sort($blog, 'DATE', SORT_ASC);
print_r($list);
Example to sort on a specific key (in this case name):
// Function to compare two items in the array
function CompareName($left, $right) {
return $left['name'] > $right['name'];
}
// Example array/data
$myarray = [
["id"=>1, "name"=>"foo"],
["id"=>2, "name"=>"bar"],
["id"=>3, "name"=>"ah"],
["id"=>4, "name"=>"zoo"]
];
echo 'Unsorted:';
var_dump($myarray);
usort($myarray , 'CompareName');
echo 'Sorted:';
var_dump($myarray);
want to get only 5 data from an array
$top5 = array_slice($myarray, 0, 5);
or:
$top5 = array_splice($myarray, 0, 5);
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]; })
This question already has answers here:
Sort multidimensional array by multiple columns
(8 answers)
Closed 10 months ago.
I went through all the similiar questions but couldn't find an answer....so here goes.
My current array, simplified:
[order] => Array
(
[0] => Array
(
[strSupplier] => XYZ
(varying other fields)
)
[1] => Array
(
[strSupplier] => XYZ
(varying other fields)
)
[2] => Array
(
[strSupplier] => YYZ
(varying other fields)
)
)
Code:
function custom_sort2($a,$b) {
return $a['strSupplier']>$b['strSupplier'];
}
// Sort the multidimensional array
usort($tempOrderArray, "custom_sort2");
Currently, I am sorting on only the supplier, however, I need to ensure that the key is the second sort criteria, and I am not sure that it is.
Is there a way I can guarantee that it is sorted by strSupplier first, then key? If this is built into either the uasort or usort function, I apologize - I did not see it.
Your array would like this this:
[order] => Array
(
[0] => Array
(
[key] => 0,
[strSupplier] => 'XYZ',
//(varying other fields)
)
[1] => Array
(
[key] = 1,
[strSupplier] => 'XYZ',
//(varying other fields)
)
[2] => Array
(
[key] = 2,
[strSupplier] => 'YYZ',
//(varying other fields)
)
)
Then, when you sort:
function custom_sort2($a, $b) {
$cmp = $cmpstr = strcmp($a['strSupplier'], $b['strSupplier']); //Compare the string
$cmpkey = ($a['key'] == $b['key'] ? 0 : ($a['key'] > $b['key'] ? 1 : -1)); //Compare the key
if ($cmpkey == 1)
$cmp = ($cmpstr >= 0) ? 1 : 0;
return $cmp; //If we are moving the element forward, then we need to check the key.
}
// Sort the multidimensional array
uasort($array, "custom_sort2");
You could try using ksort first, and then sort by supplier.