i have an php array of sports scores and i want to sort by the following
Wins Desc, Ties Desc, Points Scored Desc, Points Allowed Asc
anyone know how i can do this?
This array was generated by multiple mysql quires
[14] => Array
(
[title] => Crushers
[team_color] => #0000ff
[wins] => 0
[losses] => 1
[ties] => 1
[pointsscored] => 18
[pointsallowed] => 19
)
[15] => Array
(
[title] => Purple Stars
[team_color] => #ae25d9
[wins] => 1
[losses] => 1
[ties] => 0
[pointsscored] => 20
[pointsallowed] => 21
)
[16] => Array
(
[title] => Lighting Boltz
[team_color] => #d6c128
[wins] => 0
[losses] => 0
[ties] => 2
[pointsscored] => 12
[pointsallowed] => 12
)
[17] => Array
(
[title] => The Black Cheetahs
[team_color] => #000000
[wins] => 1
[losses] => 0
[ties] => 1
[pointsscored] => 15
[pointsallowed] => 13
)
I made a function to sort every multidimensional array by one of his columns ,look at array_multisort() and customize this function to your need:
function sortArrayBy($array , $column_name,$sort=SORT_DESC){
foreach ($array as $key => $row) {
$column[$key] = $row[$column_name];
}
array_multisort($column, $sort, $array);
return $array;
}
Call it like this :
<?php sortArrayBy($yourArray,'date') ; ?>
Related
I have an api with a list of arrays inside one array and each array inside this array it has one key [attributes] and inside this key one array with key [CITY_NAME]
this is my array coming from api
this array number is 0 but every day this number is changed and my figures come wrong, what is the good way to always have my city figures using the key [CITY_NAME]
I am fetching the data using this code
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$data = $json_data['features'];
$mycity = $data[0]['attributes'];
Array
(
[0] => Array
(
[attributes] => Array
(
[CITY_NAME] => city1
[Name] => city1
[ADMIN_NAME] => city1
[POP_CLASS] => 5,000,000 to10,000,000
[Population] => 7676654
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2018
[CityID] => 14
[Longitude] => 46.7614868685786
[Latitude] => 24.7388786516234
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city1
[Join_Count] => 61
[Confirmed_SUM] => 5152
[Deaths_SUM] => 9
[Recovered_SUM] => 1407
[Active_SUM] => 3736
[Tested_SUM] => 376607
[ObjectId] => 14
)
)
[1] => Array
(
[attributes] => Array
(
[CITY_NAME] => city2
[Name] => city2
[ADMIN_NAME] => city2
[POP_CLASS] => 1,000,000 to 5,000,000
[Population] => 1675368
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2010
[CityID] => 9
[Longitude] => 39.8148987363852
[Latitude] => 21.4273876500039
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city2
[Join_Count] => 59
[Confirmed_SUM] => 6848
[Deaths_SUM] => 85
[Recovered_SUM] => 1145
[Active_SUM] => 5618
[Tested_SUM] => 0
[ObjectId] => 9
)
)
Since I may have misunderstood and you may have many, just build a new array with CITY_NAME:
foreach($data as $values) {
$result[$values['attributes']['CITY_NAME']] = $values['attributes'];
}
Now you can access by CITY_NAME:
echo $result['city1']['Population'];
This might be easier. Extract all attributes from all arrays into an array, then create an array from that indexed by CITY_NAME:
$data = array_column(array_column($json_data['features'], 'attributes'),
null, 'CITY_NAME');
I have an array like this
Array
(
[0] => stdClass Object
(
[cat_id] => 3
[cat_name] => sample 3
[cat_description] =>
[cat_folder] => sample_3
[cat_path] => sample_3
[cat_parent] => 0
[cat_num_files] => 0
[cat_num_files_total] => 0
[cat_user_roles] =>
[cat_owner] => 1
[cat_icon] =>
[cat_exclude_browser] => 0
[cat_order] => 0
)
[1] => stdClass Object
(
[cat_id] => 2
[cat_name] => sample 2
[cat_description] =>
[cat_folder] => sample_2
[cat_path] => sample_3/sample_2
[cat_parent] => 3
[cat_num_files] => 0
[cat_num_files_total] => 0
[cat_user_roles] =>
[cat_owner] => 1
[cat_icon] =>
[cat_exclude_browser] => 0
[cat_order] => 0
)
)
I need to list these like
-sample 3
--sample 2
---sample 4
There is no limit for the link depth, a category may have 5 deep, it may be 10. I have tried this in a foreach loop but failed to retrieve a list like a tree.
I recommend recursion, becouse you don't know how nested it will be. Bellow is example of recursion function.
function genCat($parentCat){
echo $parentCat->name;
if ($parentCat->haveChildren){
foreach($parentCat->children as $child){
genCat($child);
}
}
}
This is just example, hope it helps
I am trying to write a recursive function that drills back to the root category of a nest of unknown depth.
[TABLE]
cat_id | cat_name | cat_parent | cat_slug
//each category with a cat_parent of 0 is a root category
[/TABLE]
Example SQL result:
Array
(
[0] => Array
(
[cat_id] => 17
[cat_name] => another-test-category
[cat_parent] => 16
[cat_slug] => Another test category
)
)
Function:
function breadcrumb($cat_id){
$cat_nest =
SELECT *
FROM table
WHERE cat_id = '$cat_id'
//returns 1 row;
$cat_array[$cat_id] = $cat_nest[0];
if($cat_nest[0]['cat_parent'] != 0){
$cat_array[] = breadcrumb($cat_nest[0]['cat_parent']);
}
return $cat_array;
}
It is outputting:
Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)
[18] => Array
(
[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)
[17] => Array
(
[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)
[16] => Array
(
[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)
[7] => Array
(
[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)
)
)
)
)
)
Desired output:
Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)
[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)
[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)
[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)
[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)
)
I ran some tests and I think the solution can be implemented this way:
function breadcrumb($cat_id){
$cat_nest =
SELECT *
FROM table
WHERE cat_id = '$cat_id'
//returns 1 row;
$cat_array[$cat_id] = $cat_nest[0];
if($cat_nest[0]['cat_parent'] != 0){
$cat_array = array_merge( $cat_array, breadcrumb($cat_nest[0]['cat_parent']) );
}
return $cat_array;
}
This would maintain the IDs exactly as you need them, since this would only be merging arrays and not creating new indexes with $cat_array[]
Consider using a do-while-loop:
function breadcrumb($cat_id) {
$cat_array = array();
do {
$cat_nest =
SELECT *
FROM table
WHERE cat_id = '$cat_id';
$cat_array[$cat_id] = $cat_nest[0];
$cat_id = $cat_nest[0]['catparent'];
} while ($cat_id != 0)
return $cat_array;
}
This way you'll also be able to easily identify the 'first' breadcrumb, and add extra code for it, like declaring the array.
I have a data structure like this
Array
(
[0] => Array
(
[actionResult] => Array
(
[show_page] => Array
(
[15] => Array
(
[section_page_id] => 15
[metadata_id] => 62
[section_id] => 7
[display_order] => 0
[current_layout] => 15
[behaviors] => a:1:{i:0;a:1:{i:0;a:0:{}}}
[options] => a:1:{s:13:"defaultLayout";i:63;}
[section_title] => Ask Study
)
[16] => Array
(
[section_page_id] => 16
[metadata_id] => 66
[section_id] => 7
[display_order] => 1
[current_layout] => 16
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:67;}
[section_title] => Ask Study
)
[17] => Array
(
[section_page_id] => 17
[metadata_id] => 69
[section_id] => 7
[display_order] => 2
[current_layout] => 17
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:70;}
[section_title] => Ask Study
)
[18] => Array
(
[section_page_id] => 18
[metadata_id] => 72
[section_id] => 7
[display_order] => 3
[current_layout] => 18
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:73;}
[section_title] => Ask Study
)
)
)
)
[1] => Array
(
[actionResult] => Array
(
[view_page] => 18
)
)
)
What i need is the ability to flatten this to an array structure to a point where it stops at "actionResult" where all the actionResult will become ONE array rather than nested like this...
How can I go about by doing this in PHP???
if i have understood what you want correctly this should work:
$arr2=array();
foreach($arr as $tmp){
foreach($tmp as $actionRequest){
foreach($actionRequest as $key=>$val){
$arr2[$key]=$val;
}
}
}
where $arr is what you already have and $arr2 will be an array including 2 values , Show_Page and view_page
Best solution is:
$new_arr = array_map(function ($a) {
return $a['actionResult'];
}, $old_arr);
In the multidimensional array below, I would like to merge arrays that have the same merge_id. I'm not sure "merge" is the right word: in the example below, array['0'] should become array['0'] with in it array['0']['0'] and array['0']['1'], the latter being equal to array['1']. I hope this makes sense ...
The array comes out of the db sorted on merge_id so arrays with matching merge_id are always "next to" each other, and there will only ever be 2 with the same merge_id
As I loop through the array I know I need to keep a variable that is always equal to the previous merge_id and if there is a match between previous and current, then merge.
Array
(
[0] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 3
[fee] => 111
[year] => 2009
[quarter] => 3
[date_inserted] => 1264948583
[description] => 2009 - Q3
[fee_type] =>
[merge_id] => a87ff679a2f3e71d9181a67b7542122c
[total_paid] => 0
[total_remainder] => 0
)
[1] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 6
[fee] => 55.5
[year] => 2010
[quarter] => 2
[date_inserted] => 1264949470
[description] => 2010 - Q2
[fee_type] =>
[merge_id] => a87ff679a2f3e71d9181a67b7542122c
[total_paid] => 0
[total_remainder] => 0
)
[2] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 4
[fee] => 111
[year] => 2009
[quarter] => 4
[date_inserted] => 1264948583
[description] => 2009 - Q4
[fee_type] =>
[merge_id] =>
[total_paid] => 0
[total_remainder] => 0
)
[3] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 7
[fee] => 55.5
[year] => 2010
[quarter] => 3
[date_inserted] => 1264949470
[description] => 2010 - Q3
[fee_type] =>
[merge_id] =>
[total_paid] => 0
[total_remainder] => 0
)
)
Code
$merger = $data['search']['0']['merge_id'];
$i = 0;
foreach($data['search'] as $fee)
{
if($fee['merge_id'] == $merger)
{
//bump up & merge arrays
???
}
$merger = $fee['merge_id'];
$i++;
}
You can use the ID as key to put all items with the same ID in the same array:
$merged = array();
foreach ($data['search'] as $fee) {
if ($fee['merge_id'] == '') {
continue;
}
if (!isset($merged[$fee['merge_id']])) {
$merged[$fee['merge_id']] = array();
}
$merged[$fee['merge_id']][] = $fee;
}
$merged = array_values($merged);
Notice that this will skip the items with an empty merge ID. You could also use a default merge ID in that case by replacing continue; with $fee['merge_id'] = 0;.
foreach($array as $p)
$result[$p['merge_id']][] = $p;