Sort Array by court and hour ascending - php

How can i sort this array in php so that it stays sorted by court and hour in ascending order.
The array is the following:
array(15) {
[0]=>
array(1) {
["Court 3"]=>
string(5) "19h00"
}
[1]=>
array(1) {
["Court 1"]=>
string(5) "21h00"
}
[2]=>
array(1) {
["Court 2"]=>
string(5) "21h00"
}
[3]=>
array(1) {
["Court 1"]=>
string(5) "20h00"
}
[4]=>
array(1) {
["Court 3"]=>
string(5) "18h00"
}
[5]=>
array(1) {
["Court 2"]=>
string(5) "18h00"
}
[6]=>
array(1) {
["Court 1"]=>
string(5) "16h00"
}
[7]=>
array(1) {
["Court 1"]=>
string(5) "18h00"
}
[8]=>
array(1) {
["Court 4"]=>
string(5) "18h00"
}
[9]=>
array(1) {
["Court 2"]=>
string(5) "19h00"
}
[10]=>
array(1) {
["Court 1"]=>
string(5) "19h00"
}
[11]=>
array(1) {
["Court 2"]=>
string(5) "17h00"
}
[12]=>
array(1) {
["Court 3"]=>
string(5) "17h00"
}
[13]=>
array(1) {
["Court 4"]=>
string(5) "19h00"
}
[14]=>
array(1) {
["Court 3"]=>
string(5) "20h00"
}
}
My attempt was this one:
asort($courtsHoras);
foreach($courtsHoras as $key => $value){
foreach($value as $key1 => $value1){
echo $value1;
}
}
var_export() of the array
array ( 0 => array ( 'Court 3' => '19h00', ), 1 => array ( 'Court 1' => '21h00', ), 2 => array ( 'Court 2' => '21h00', ), 3 => array ( 'Court 1' => '20h00', ), 4 => array ( 'Court 3' => '18h00', ), 5 => array ( 'Court 2' => '18h00', ), 6 => array ( 'Court 1' => '16h00', ), 7 => array ( 'Court 1' => '18h00', ), 8 => array ( 'Court 4' => '18h00', ), 9 => array ( 'Court 2' => '19h00', ), 10 => array ( 'Court 1' => '19h00', ), 11 => array ( 'Court 2' => '17h00', ), 12 => array ( 'Court 3' => '17h00', ), 13 => array ( 'Court 4' => '19h00', ), 14 => array ( 'Court 3' => '20h00', ), )

You can use strnatcmp for this purpose along with usort. We first compare the court {some_num} key and if they are not equal, we return the result accordingly.
If they are same, we return the comparison of their timestamps.
usort($data,function($a,$b){
$court_name_1 = array_keys($a)[0];
$court_name_2 = array_keys($b)[0];
$court_ordering = strnatcmp($court_name_1,$court_name_2);
if($court_ordering === 0){
return strnatcmp($a[$court_name_1],$b[$court_name_2]);
}
return $court_ordering;
});
Demo: http://sandbox.onlinephpfunctions.com/code/fe3330ceb671bac3984e062a6f784947a6f4e81e

To simplify the sorting process, you need to normalise the array keys before sorting.
For example:
$a = [
["Court 3" => "19h00"],
["Court 1" => "21h00"],
["Court 2" => "21h00"],
["Court 1" => "20h00"],
["Court 3" => "18h00"],
["Court 2" => "18h00"],
["Court 1" => "16h00"],
["Court 1" => "18h00"],
["Court 4" => "18h00"],
["Court 2" => "19h00"],
["Court 1" => "19h00"],
["Court 2" => "17h00"],
["Court 3" => "17h00"],
["Court 4" => "19h00"],
["Court 3" => "20h00"]
];
$b = [];
foreach($a as $ka => $va) {
foreach($va as $kb => $vb) {$b[$kb][] = $vb;}
}
ksort($b); // sort courts
foreach ($b as $k => $v) {sort($b[$k]);} // sort hours
var_dump($b);

Related

Managing two different array in one loop TWIG

i'm new in twig. I had read the twig documentation but can nt find solution, as it is much easier in PHP. ((Trying two array in one loop.))
: i have two arrays one is {{ dump(render_categories) }}--
array(3) {
[0] => array(3) {
[0] => array(2) {
["category_id"] => string(2)
"65" ["name"] => string(11)
"Annivarsary"
} [1] => array(2) {
["category_id"] => string(2)
"67" ["name"] => string(9)
"Christmas"
} [2] => array(2) {
["category_id"] => string(2)
"66" ["name"] => string(8)
"Birthday"
}
} [1] => array(4) {
[0] => array(2) {
["category_id"] => string(2)
"61" ["name"] => string(6)
"Mother"
} [1] => array(2) {
["category_id"] => string(2)
"62" ["name"] => string(6)
"Sister"
} [2] => array(2) {
["category_id"] => string(2)
"60" ["name"] => string(8)
"Daughter"
} [3] => array(2) {
["category_id"] => string(2)
"63" ["name"] => string(10)
"Girlfriend"
}
} [2] => array(5) {
[0] => array(2) {
["category_id"] => string(2)
"73" ["name"] => string(8)
"Earrings"
} [1] => array(2) {
["category_id"] => string(2)
"72" ["name"] => string(22)
"Necklaces&Pendants"
} [2] => array(2) {
["category_id"] => string(2)
"71" ["name"] => string(5)
"Rings"
} [3] => array(2) {
["category_id"] => string(2)
"70" ["name"] => string(9)
"Bracelets"
} [4] => array(2) {
["category_id"] => string(2)
"69" ["name"] => string(6)
"Charms"
}
}
}
Second is {{ dump(load_steps) }} : (of different length)
array(3) {
[0] => array(4) {
["id"] => string(1)
"1" ["category_id"] => string(2)
"64" ["heading"] => string(21)
"What is the occasion?" ["status"] => string(1)
"1"
} [1] => array(4) {
["id"] => string(1)
"2" ["category_id"] => string(2)
"59" ["heading"] => string(5)
"For which Person" ["status"] => string(1)
"1"
} [2] => array(4) {
["id"] => string(1)
"3" ["category_id"] => string(2)
"68" ["heading"] => string(24)
"Type text heading 878787" ["status"] => string(1)
"1"
}
}
I'm trying to get Output like:
What is the occasion? --(get from second array)--
Annivarsary, Christmas, Birthday --(get from First array)--
For which Person? --(get from second array)--
Mother, Sister, Daughter, Girlfriend --(get from First array)--
------So, on-----
Any help is appreciated, ThankYou.
Wouldn't this do the trick ?
{% for key, step in load_steps %}
{{ step['heading'] }}
{% for category in render_categories[key] %}
{{ category['name'] }}
{% endfor %}
{% endfor %}

Why my array does not sort correctly

I have two arrays $men, $sub , I want want to sort them with this condition: if($men["titre"] == $sub["menuParent"]) I put the content of the second array that contains $sub["menuParent"] in the first one.
Case:
//Array1 ($menu)
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(3) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
//Array2 ($submenu)
array(3) {
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
["submenu2"] => array(3) {
["titre"] => string(9) "SubTitle3"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "3"
}
["submenu3"] => array(3) {
["titre"] => string(9) "SubTitle1"
["menuParent"] => string(6) "Title1"
["order"] => string(1) "1"
}
}
Here for exemple in:
array1 ["menu1"]["titre"] => string(6) "Title2"
array2 ["submenu1"]["menuParent"] => string(6) "Title2"
Now while ["menu1"]["titre"] & ["submenu1"]["menuParent"] values are the same "Title2", So I push :
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
in:
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
as child like this:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
["child"] => array(3) {
[0] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
}
So I do all this for whole elements`
Here is my try, but It push all the elements in ["menu2"] instead of ["submenu1"], ["submenu2"] IN ["menu1"] and ["submenu3"] IN ["menu3"]
My Try:
/*Injection submenu to Menu*/
foreach($menu as $men) {
foreach($submenu as $sub) {
if($men["titre"] == $sub["menuParent"]){
$key = key($menu);
$menu[$key]['child'][] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
//My array_push doesn't work also
array_push($menu, array($sub["titre"], $sub["menuParent"], $sub["order"]))
}
}
}
Result:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(4) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
["child"] => array(3) {
[0] => array(3) {
[0] => string(9) "SubTitle2"
[1] => string(6) "Title2"
[2] => string(1) "2"
}
[1] => array(3) {
[0] => string(9) "SubTitle3"
[1] => string(6) "Title2"
[2] => string(1) "3"
}
[2] => array(3) {
[0] => string(9) "SubTitle1"
[1] => string(6) "Title1"
[2] => string(1) "1"
}
}
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
Where I'm at fault, If anyone can explain to me why and if I took the good way.
The problem is where your picking the key up, you can use the foreach ( as $key->$value) method to get the key of each item (foreach works on it's own copy of the array and so picking the key from the original array isn't the same as the current one). Also you could use &$men, the & allows you to alter the original array element rather than create a new entry and trying to push the new element into the array...
foreach($menu as $key => $men) {
foreach($submenu as $key1=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$menu[$key]['child'][$key1] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
gives...
Array
(
[menu1] => Array
(
[titre] => Title2
[lien] =>
[order] => 2
[child] => Array
(
[submenu1] => Array
(
[0] => SubTitle2
[1] => Title2
[2] => 2
)
[submenu2] => Array
(
[0] => SubTitle3
[1] => Title2
[2] => 3
)
)
)
[menu2] => Array
(
[titre] => Title3
[lien] => google.com
[order] => 3
)
[menu3] => Array
(
[titre] => Title1
[lien] =>
[order] => 1
[child] => Array
(
[submenu3] => Array
(
[0] => SubTitle1
[1] => Title1
[2] => 1
)
)
)
)
Update:
Code using &$men, note that in setting the data it no longer uses the key, but uses the $men variable.
foreach($menu as &$men) {
foreach($submenu as $key=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$men['child'][$key] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
unset($men); // Good practice to ensure reference is reset

How to extract from multi dimensional array using codeigniter

How to extract this multi dimensional array from php. I am using codeigniter framework.
array(2) {
[0] => array(14)
{
["Key"] =>
string(48) "32;FaXmBQJ7/0MATwBVAFAAMAAxADIAOA==9;1261414580;"
["Type"] => string(12) "Store_Coupon"
["Code"] => string(8) "COUP0128"
["Coupon_Issuer"] => string(2) "11"
["Coupon_Reference_No"] => string(1) "1"
["Description"] => string(5) "test2"
["Price_Group"] => string(3) "ALL"
["Discount_Type"] => string(15) "Discount_Amount"
["Discount_Value"] => string(1) "0"
["Validation_Period_ID"] => string(2) "14"
["Validation_Description"] => string(15) "2016 membership"
["Starting_Date"] => string(10) "2016-01-01"
["Ending_Date"] => string(10) "2016-12-31"
["Status"] => string(8) "Disabled"
}
[1] => array(14)
{
["Key"] => string(48) "32;FaXmBQJ7/0MATwBVAFAATwBOADAAMg==9;1261413680;"
["Type"] => string(12) "Store_Coupon"
["Code"] => string(8) "COUPON02"
["Coupon_Issuer"] => string(2) "11"
["Coupon_Reference_No"] => string(1) "2"
["Description"] => string(6) "test 3"
["Price_Group"] => string(3) "ALL"
["Discount_Type"] => string(16) "Discount_Percent"
["Discount_Value"] => string(1) "0"
["Validation_Period_ID"] => string(2) "14"
["Validation_Description"] => string(15) "2016 membership"
["Starting_Date"] => string(10) "2016-01-01"
["Ending_Date"] => string(10) "2016-12-31"["Status"] => string(8) "Disabled"
}
}
I want to extract Coupon_Reference_No element. Thanks in advance
Just do a foreach and store that variable in an array. Supposing your aray is $array
$Coupon_Reference_No = array();
foreach($array as $key=>$value){
if($key == 'Coupon_Reference_No'){
$Coupon_Reference_No[] = $value;
}
}
You will get an array $Coupon_Reference_No as array("1","2")

PHP - merge/combine array values in multidimensional array

How could I convert this array:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41
)
);
to this one:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41
),
1 => array(
0 => 39,
1 => 41
)
),
1 => array(
0 => array(
0 => 41,
1 => 31
),
1 => array(
0 => 41,
1 => 39
)
)
);
I tried several ways, but find no proper solution. At the moment my brain is overheated. So maybe someone has a solution for me.
Thanks #Manos.
Unfortunatly there are dynamic arrays. So these static function wont work for me.
So the array could also look like this:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41,
1 => 49,
2 => 51
),
2 => array(
0 => 73
)
);
Output should look like this:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41,
2 => 73
),
1 => array(
0 => 31,
1 => 49,
2 => 73
),
2 => array(
0 => 31,
1 => 51,
2 => 73
),
3 => array(
0 => 39,
1 => 41,
2 => 73
),
4 => array(
0 => 39,
1 => 49,
2 => 73
),
5 => array(
0 => 39,
1 => 51,
2 => 73
),
1 => array(
0 => array(
0 => 41,
1 => 31,
2 => 73
),
1 => array(
0 => 41,
1 => 39,
2 => 73
),
2 => array(
0 => 49,
1 => 31,
2 => 73
),
3 => array(
0 => 49,
1 => 39,
2 => 73
),
etc ......
)
);
Manos function Output:
array(3) {
[0]=>
array(8) {
[0]=>
array(2) {
[0]=>
int(31)
[1]=>
int(41)
}
[1]=>
array(2) {
[0]=>
int(39)
[1]=>
int(41)
}
[2]=>
array(2) {
[0]=>
int(31)
[1]=>
int(49)
}
[3]=>
array(2) {
[0]=>
int(39)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(31)
[1]=>
int(51)
}
[5]=>
array(2) {
[0]=>
int(39)
[1]=>
int(51)
}
[6]=>
array(2) {
[0]=>
int(31)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(39)
[1]=>
int(73)
}
}
[1]=>
array(9) {
[0]=>
array(2) {
[0]=>
int(41)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(49)
[1]=>
int(31)
}
[2]=>
array(2) {
[0]=>
int(51)
[1]=>
int(31)
}
[3]=>
array(2) {
[0]=>
int(41)
[1]=>
int(39)
}
[4]=>
array(2) {
[0]=>
int(49)
[1]=>
int(39)
}
[5]=>
array(2) {
[0]=>
int(51)
[1]=>
int(39)
}
[6]=>
array(2) {
[0]=>
int(41)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(49)
[1]=>
int(73)
}
[8]=>
array(2) {
[0]=>
int(51)
[1]=>
int(73)
}
}
[2]=>
array(5) {
[0]=>
array(2) {
[0]=>
int(73)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(73)
[1]=>
int(39)
}
[2]=>
array(2) {
[0]=>
int(73)
[1]=>
int(41)
}
[3]=>
array(2) {
[0]=>
int(73)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(73)
[1]=>
int(51)
}
}
}
foreach ($a as $first_group_key => $first_group) {
foreach ($a as $second_group_key => $second_group) {
if ($second_group_key == $first_group_key) {
continue;
}
$i = count($b[$first_group_key]);
foreach ($second_group as $second_value) {
foreach ($first_group as $first_key => $first_value) {
$b[$first_group_key][$i][0] = $first_value;
$b[$first_group_key][$i][1] = $second_value;
$i++;
}
}
}
}

Remove stdClass objects from array

I have a array like below (array 1) and I need to remove stdClass from it like in below array no. 2. Currently i'm doing it using a foreach loop, is there are better way to do that wthout looping?
Array no.1
array(3) {
[0] => object(stdClass)#169 (4) {
["id"] => string(2) "59"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(20) "898998989"
}
[1] => object(stdClass)#190 (4) {
["id"] => string(2) "58"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(8) "71877858"
}
[2] => object(stdClass)#193 (4) {
["id"] => string(2) "34"
["name"] => string(9) "test"
["email"] => string(22) "abc#abc.com"
["telephone"] => string(13) "3189028092139"
}
}
Array no.2
array(3) {
[0] => array(4) {
["id"] => string(2) "62"
["name"] => string(5) "test"
["email"] => string(22) "abc#abc.com"
["telephone"] => string(10) "898998989"
}
[1] => array(4) {
["id"] => string(2) "59"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(20) "71877858"
}
[2] => array(4) {
["id"] => string(2) "58"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(8) "3189028092139"
}
}
This is what I do (casting)
foreach($moderationContacts as $contact)
{
$contacts[] = (array)$contact;
}
try
$array = json_decode( json_encode($array), true);
EDIT:
I've tested this case, and it works:
$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
"a" => Array("b","c"),
"d" => $stdClass
);
$array = json_decode( json_encode($array), true);
var_dump($array);
OUTPUT
array
'a' =>
array
0 => string 'b' (length=1)
1 => string 'c' (length=1)
'd' =>
array
'test' => string 'foo' (length=3)
You can try
$array = array_map(function ($v) {
return (array) $v ; // convert to array
}, $array);
Or if this data is from json use
$array = json_decode($data,true);

Categories