PHP Sort Multidimensional Array by a sub sub-key - php

I have looked at numerous threads relating to this and none of them have been of any help to me. I have an array which follows the basic structure of $array[location][store][person] = funds. What is the most efficient way of sorting the array so that the [person] key is in ASC order?
This is what it looks like now:
Array
(
[Florida] => Array
(
[AppleSauce] => Array
(
[Rabbit, Hunting] => 5
[Brown, Bubba] => 20
[Chicken, Cantina] => 10
[Gum, Bubble] => 10
[Pool, Swimming] => 4
[Bath, Taka] => 2
)
)
[Texas] => Array
(
[BeatleJuice] => Array
(
[Chicken, Cantina] => 10
[Pool, Swimming] => 4
[House, Road] => 5
)
[CaramelApple] => Array
(
[Chicken, Cantina] => 10
[Pool, Swimming] => 4
[House, Road] => 5
)
)
This is what I am looking for:
Array
(
[Florida] => Array
(
[AppleSauce] => Array
(
[Bath, Taka] => 2
[Brown, Bubba] => 20
[Chicken, Cantina] => 10
[Gum, Bubble] => 10
[Pool, Swimming] => 4
[Rabbit, Hunting] => 5
)
)
[Texas] => Array
(
[BeatleJuice] => Array
(
[Chicken, Cantina] => 10
[House, Road] => 5
[Pool, Swimming] => 4
)
[CaramelApple] => Array
(
[Chicken, Cantina] => 10
[House, Road] => 5
[Pool, Swimming] => 4
)
)

You can use ksort to sort the array keys of people in alphabetical order
foreach($array as $state => $locations) {
foreach($locations as $location => $people) {
ksort($array[$state][$location]);
}
}

The php function array_multisort can do this:
Sorting multi-dimensional array
<?php
$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order).
array(2) {
[0]=> array(5) {
[0]=> string(2) "10"
[1]=> int(100)
[2]=> int(100)
[3]=> int(11)
[4]=> string(1) "a"
}
[1]=> array(5) {
[0]=> int(1)
[1]=> int(3)
[2]=> string(1) "2"
[3]=> int(2)
[4]=> int(1)
}
}
This is based from the official documentation:
http://php.net/manual/en/function.array-multisort.php

Related

Make two index array to be one index array PHP [duplicate]

This question already has answers here:
Implode two-dimensional array in PHP
(3 answers)
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 4 years ago.
i just want to make two index array to be one array.
this is my code:
$intdate=array();
$arr=0;
foreach ($cek_cutay as $key => $value) {
$intdate[] =intervalDate($value->tgl_cuti_awal,$value->tgl_cuti_akhir);
$intdate[$arr++];
}
the result like :
array(2) {
[0]=> array(4) {
[0]=> string(10) "2018-11-12"
[1]=> string(10) "2018-11-13"
[2]=> string(10) "2018-11-14"
[3]=> string(10) "2018-11-15"
}
[1]=> array(2) {
[0]=> string(10) "2018-10-31"
[1]=> string(10) "2018-11-01"
}
}
i hope to be like :
array(5){
[0]=> string(10) "2018-11-12"
[1]=> string(10) "2018-11-13"
[2]=> string(10) "2018-11-14"
[3]=> string(10) "2018-11-15"
[4]=> string(10) "2018-10-31"
[5]=> string(10) "2018-11-01"
}
Thank you..!!
If the return from the intervalDate function is an array, you can do this
$intdate=array();
$arr=0;
foreach ($cek_cutay as $key => $value) {
foreach (intervalDate($value->tgl_cuti_awal,$value->tgl_cuti_akhir) as $date) {
$intdate[] = $date;
}
}
I think you expected to merge two array into a single array. If so then array_reduce might help you simply.
$arr = [
[
"2018-11-12",
"2018-11-13",
"2018-11-14",
"2018-11-15" ,
],
[
"2018-10-31",
"2018-11-01",
]
];
$intdate = array_reduce($arr, function($old, $new) {
return array_merge($old, $new);
}, []);
echo '<pre>', print_r($intdate), '</pre>';
You could use this function
function flatten_array( array $array, array $flattened = array() ) {
foreach ( $array as $item ) {
if ( is_array($item) ) {
$flattened = flatten_array( $item, $flattened );
continue;
}
$flattened[] = $item;
}
return $flattened;
}
$arr = array(
array(
"2018-11-12",
"2018-11-13",
"2018-11-14",
"2018-11-15" ,
),
array(
"2018-10-31",
"2018-11-01",
),
);
flatten_array($arr);
$arr1 = array(1,2,3,4,5,11);
$arr2 = array(6,7,8,9,10,11);
echo'<pre>';print_r(array_merge($arr1,$arr2));
echo'<pre>';print_r(array_unique(array_merge($arr1,$arr2)));die;
respective output :
array_merge :
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 11
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
)
array_unique + array_merge :
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 11
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
)

How to create a new key name and combine values in array with PHP?

I have 2 PHP arrays that I need to combine values together.
First Array
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(2) "40"
}
}
Second Array
array(2) {
[0]=>
string(4) "1008"
[1]=>
string(1) "4"
}
Output desired
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1",
["count"]=>
string(1) "1008"
}
[1]=>
array(1) {
["id"]=>
string(2) "40",
["count"]=>
string(1) "4"
}
}
As you can see I need to add a new key name (count) to my second array and combine values to my first array.
What can I do to output this array combined?
Try something like the following. The idea is to iterate on the first array and for each array index add a new key "count" that holds the value contained on the same index of the second array.
$array1 = [];
$array2 = [];
for ($i = 0; $i < count($array1); $i++) {
$array1[$i]['count'] = $array2[$i];
}
you can do it like this
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
for($i=0;$i<count($arr2);$i++){
$arr1[$i]["count"] = $arr2[$i];
}
Live demo : https://eval.in/904266
output is
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Another functional approach (this won't mutate/change the initial arrays):
$arr1 = [['id'=> "1"], ['id'=> "40"]];
$arr2 = ["1008", "4"];
$result = array_map(function($a){
return array_combine(['id', 'count'], $a);
}, array_map(null, array_column($arr1, 'id'), $arr2));
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Or another approach with recursion:
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
foreach ($arr1 as $key=>$value) {
$result[] = array_merge_recursive($arr1[$key], array ("count" => $arr2[$key]));
}
print_r($result);
And output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)

how to insert a array data in multiple columns based on a value pair which exists in array in php

array(1) {
[0]=> array(7) {
["ranking_course_id"]=> string(1) "2"
["ranking_college_id"]=> string(2) "20"
["ranking_year"]=> string(4) "2017"
["ranking_agency"]=> array(3) {
[0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(1) "3"
}
["ranking_value"]=> array(3) {
[0]=> string(2) "10"
[1]=> string(2) "20"
[2]=> string(2) "30"
}
["ranking_salary"]=> string(9) "9.20 Lacs"
["ranking_fees"]=> string(6) "354500"
}
}
What I want to insert is the following.
course_id | college_id | ranking_agency | ranking | value | salary fees
2 20 1 10 9.20 354500
2 20 2 20 9.20 354500
2 20 3 30 9.20 354500
Please help me out.
Try this workout
$oldarray = array(array("ranking_course_id" => "2",
"ranking_college_id"=> "20",
"ranking_year"=> "2017",
"ranking_agency"=> array("1","2","3"),
"ranking_value"=> array("10","20","30"),
"ranking_salary"=> "9.20 Lacs",
"ranking_fees"=> "354500"));
for($i=0; $i<count($oldarray[0]['ranking_agency']); $i++)
{
$newarray[] = $oldarray[0];
$newarray[$i]['ranking_agency'] = $oldarray[0]['ranking_agency'][$i];
$newarray[$i]['ranking_value'] = $oldarray[0]['ranking_value'][$i];
}
echo "<pre>"; print_r($newarray);
echo "<pre>"; print_r($oldarray);
output:
newarray
Array
(
[0] => Array
(
[ranking_course_id] => 2
[ranking_college_id] => 20
[ranking_year] => 2017
[ranking_agency] => 1
[ranking_value] => 10
[ranking_salary] => 9.20 Lacs
[ranking_fees] => 354500
)
[1] => Array
(
[ranking_course_id] => 2
[ranking_college_id] => 20
[ranking_year] => 2017
[ranking_agency] => 2
[ranking_value] => 20
[ranking_salary] => 9.20 Lacs
[ranking_fees] => 354500
)
[2] => Array
(
[ranking_course_id] => 2
[ranking_college_id] => 20
[ranking_year] => 2017
[ranking_agency] => 3
[ranking_value] => 30
[ranking_salary] => 9.20 Lacs
[ranking_fees] => 354500
)
)
oldarray
Array
(
[0] => Array
(
[ranking_course_id] => 2
[ranking_college_id] => 20
[ranking_year] => 2017
[ranking_agency] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[ranking_value] => Array
(
[0] => 10
[1] => 20
[2] => 30
)
[ranking_salary] => 9.20 Lacs
[ranking_fees] => 354500
)
)
and now insert the records :)
This is altered version of #Nobita, for multidimensional array.
$oldarray = array(array("ranking_course_id" => "2",
"ranking_college_id"=> "20",
"ranking_year"=> "2017",
"ranking_agency"=> array("1","2","3"),
"ranking_value"=> array("10","20","30"),
"ranking_salary"=> "9.20 Lacs",
"ranking_fees"=> "354500"),
array("ranking_course_id" => "2",
"ranking_college_id"=> "20",
"ranking_year"=> "2017",
"ranking_agency"=> array("1","2","3"),
"ranking_value"=> array("10","20","30"),
"ranking_salary"=> "9.20 Lacs",
"ranking_fees"=> "354500"));
$new_array = [];
$j=0;//For row
foreach($oldarray as $array)
{
$i=0;//For ranking agency and ranking value
foreach ($array['ranking_agency'] as $key => $value) {
$new_array[$j] = $array;
$new_array[$j]['ranking_agency'] = $array['ranking_agency'][$i];
$new_array[$j]['ranking_value'] = $array['ranking_value'][$i];
$i++; $j++;
}
}
echo "<pre>";print_r($new_array);
exit;
The array generation based on the 'ranking_value' & 'ranking_agency'.
Happy coding...!
For inserting multiple records use the following code.
$this->db->insert_batch("table_name", $array_name);
For a single dimension array, use the following code.
$this->db->insert("table_name", $array_name);
For more details see Active Record Class.
array key should be same as them name of the column in that table you inserting

Recreate Multidimensional Array in PHP

This is pretty basic, but my question is:
Given an array:
$a = array(
0 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_1', 'type'=>'day','value'=>10)),
1 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_2', 'type'=>'night','value'=>8)),
2 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_3', 'type'=>'day','value'=>7)),
3 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_4', 'type'=>'nigh','value'=>16)),
4 => array('Rate'=> array('type_id'=>3, 'name' => 'Rate_5', 'type'=>'day','value'=>10))
);
What is the most efficient way to change it so we have something like:
$new_array = array(
[type_id] => array(
[type] => array(
[value]
)
)
)
);
In other words, I would like to strip some data (the name, which I don't need) and reorganise the dimensions of the array. In the end I would have an array which I would be able to access the values by $new_array['type_id']['type']['value'].
Not entirely sure if this is exactly what you want, but with this you can access the values by saying
echo $new[TYPE_ID][DAY_OR_NIGHT];
$new = array();
foreach($a AS $b){
$c = $b['Rate'];
$new[$c['type_id']][$c['type']] = $c['value'];
}
Using print_r on $new would give you:
Array
(
[1] => Array
(
[day] => 10
[night] => 8
)
[2] => Array
(
[day] => 7
[night] => 16
)
[3] => Array
(
[day] => 10
)
)
Since php 5.3.0, array_reduce() allows using an array as the initial value, given your initial array $a, you can use the following code
function my_reducer ($result, $item) {
$result[$item['Rate']['type_id']][$item['Rate']['type']] = $item['Rate']['value'];
return $result;
}
$assoc_arr = array_reduce($a, 'my_reducer', array());
var_dump($assoc_arr);
This returns
array(3) { [1]=> array(2) {
["day"]=>
int(10)
["night"]=>
int(8) } [2]=> array(2) {
["day"]=>
int(7)
["nigh"]=>
int(16) } [3]=> array(1) {
["day"]=>
int(10) } }

multi-dimensional array, move into a simple array

I have an array that looks like this,
[0] => Array
(
[youtube_showreel_url_1] => youtube1.com
[youtube_showreel_description] => youtube1.com - desc
)
[1] => Array
(
[youtube_showreel_url_2] => youtube2.com
[youtube_showreel_description] => youtub2.com - desc
)
[2] => Array
(
[youtube_showreel_url_3] => youtube3.com
[youtube_showreel_description] => youtube3.com - desc
)
[3] => Array
(
[youtube_showreel_url_4] => youtube4.com
[youtube_showreel_description] => youtube4.com - desc
)
[4] => Array
(
[youtube_showreel_url_5] => youtube5.com
[youtube_showreel_description] => youtube5.com - desc
)
Is it possible with PHP to turn it into something that looks like this?
[0] => Array (
[youtube_showreel_url_1] => youtube1.com
[youtube_showreel_description] => youtube1.com - desc
[youtube_showreel_url_2] => youtube2.com
[youtube_showreel_description] => youtub2.com - desc
[youtube_showreel_url_3] => youtube3.com
[youtube_showreel_description] => youtube3.com - desc
[youtube_showreel_url_4] => youtube4.com
[youtube_showreel_description] => youtube4.com - desc
[youtube_showreel_url_5] => youtube5.com
[youtube_showreel_description] => youtube5.com - desc
)
Can explode it or run it through a loop or something?
Assuming your original data is held in a variable called $input:
// This will hold the result
$result = array();
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key] = $val; // Create entry in $result
}
}
// Show the result
print_r($result);
However, your input has the same key appearing in it more than once, and the later values will overwrite the first one. So you might want to do something like this:
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key.$index] = $val; // Create entry in $result
}
}
<?php
$aNonFlat = array(
1,
2,
array(
3,
4,
5,
array(
6,
7
),
8,
9,
),
10,
11
);
$objTmp = (object) array('aFlat' => array());
array_walk_recursive($aNonFlat, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);
var_dump($objTmp->aFlat);
/*
array(11) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
[6]=>
int(7)
[7]=>
int(8)
[8]=>
int(9)
[9]=>
int(10)
[10]=>
int(11)
}
*/
?>
source : http://ca.php.net/manual/fr/function.array-values.php#86784
array keys have to have unique names meaning that you wouldn't be able to have multiple youtube_showreel_description keys or else you just overwrite the value. You could rename the key to something like youtube_showreel_description_NN where NN is the number of the description similar to how you have the url.
there would be a collision on "youtube_showreel_description"
you should think of a better data structure for that
I would suggest you retain its multidimensional array structure
Array
(
[0] => Array
(
[url] => youtube1.com
[description] => youtube1.com - desc
)
[1] => Array
(
[url] => youtube2.com
[description] => youtube2.com - desc
)
[2] => Array
(
[url] => youtube3.com
[description] => youtube3.com - desc
)
[3] => Array
(
[url] => youtube1.com
[description] => youtube3.com - desc
)
[4] => Array
(
[url] => youtube1.com
[description] => youtube4.com - desc
)
)
If you like it that way, I think its cleaner, and easier to parse
Not exactly, the [youtube_showreel_description] would overwrite itself after each declaration, you would need to use a unique identifier for each key. If the keys are not important you can just loop through your existing array with a foreach loop and set a regular iterative key and "know" that the array comes in sets where the first element is the url and the second is the description.
Edit: If the keys aren't important, you could use the url as your key and description as your value, this will make it easier to iterate over as each element pertains to only one item.
As everyone has already mentioned, you're going to get a collision on 'youtube_showreel_description'. How about something like this?:
// values (stays the same - no need to reformat)
$values = array(
array(
'youtube_showreel_url_1' => 'youtube1.com',
'youtube_showreel_description' => 'youtube1.com desc'
),
array(
'youtube_showreel_url_2' => 'youtube2.com',
'youtube_showreel_description' => 'youtube2.com desc'
),
array(
'youtube_showreel_url_3' => 'youtube3.com',
'youtube_showreel_description' => 'youtube3.com desc'
),
array(
'youtube_showreel_url_4' => 'youtube4.com',
'youtube_showreel_description' => 'youtube4.com desc'
)
);
// the good stuff
$result = array_map(function($v) {
return array(
'url' => array_shift($v),
'description' => $v['youtube_showreel_description']
);
}, $values);
// result
array(4) {
[0]=>
array(2) {
["url"] => string(12) "youtube1.com"
["description"] => string(17) "youtube1.com desc"
}
[1]=>
array(2) {
["url"] => string(12) "youtube2.com"
["description"] => string(17) "youtube2.com desc"
}
[2]=>
array(2) {
["url"] => string(12) "youtube3.com"
["description"] => string(17) "youtube3.com desc"
}
[3]=>
array(2) {
["url"] => string(12) "youtube4.com"
["description"] => string(17) "youtube4.com desc"
}
}

Categories