I've seen similar questions, I simply do not understand the answers. Hopefully if I ask specifically about my code something will click. I've been stuck for some time and would really appreciate the help.
I have an array:
foreach ($showresult as $display)
{
$display_result[] = array ('parkid' =>$display['parkid'],
'trailsys' =>$display['trailsys'],
'trailset' =>$display['trailset'],
'name' =>$display['name'],
'description' =>$display['description'],
'url' =>$display['url'],
'ldes' =>$display['ldes'],
'ltxt' =>$display['ltxt'],
'address' =>$display['address'],
'city' =>$display['city'],
'zip' =>$display['zip'],
'phone' =>$display['phone'],
'pos' =>$display['pos'],
'T1' => $display['systemname'],
'T2'=> $display['name']);
{ 'state' =>$display['state'],
Every item in the array has a value in T1. T2 can have a value, or it can be 0. I want to alphabetize the list by T1. In some cases, there will be three or four entries where T1 is the same, then I want to alphabetize those by T2. I'm stumped.
Please help.
You should try with usort() function ( http://www.php.net/usort ). It allows you to sort array by custom function. So, in your case it would be something like:
<?
usort($array,function($a,$b) {
if ($a['T1'] < $b['T1']) return -1;
elseif ($a['T1'] > $b['T1']) return 1;
else {
if ($a['T2'] < $b['T2']) return -1;
elseif ($a['T2'] > $b['T2']) return 1;
else return 0;
}
});
?>
You could check it out and adjust to your needs.
Let me know if this works for you.
Related
I have an algorithm that users can manually enter and it will display the header and footer. Although in my function there is a priority field, and I want everything to be sorted with priority.
3 Parameters - Field, Priority, Type
Keep in mind I already have a array of head/foot with priority preset, this function is to add additional or overwrite the priority. It then gets stored in another array to simplify. I then want to sort it all with the priority.
Array looks like this:
'js-amcharts-export' => array('link' => 'assets/global/plugins/amcharts/amcharts/plugins/export/export.min.js',
'type' => 'text/javascript',
'default' => false,
'priority' => ''),
Example:
$script->require_meta('js-amcharts-export', '20')
This would make sure all the other ones with priority 10 lets say get loaded first then the 20, and everything after.
Basically I need to sort the array based on 'priority'
Would asort($header_array['priority'] work?
I think the PHP array sort you are looking for uasort.
http://php.net/manual/en/function.uasort.php
+1 to #user3720435 for the answer.
In your case, the code would look like this:
function cmp($a, $b) {
if ($a['priority'] == $b['priority']) {
return 0;
}
return ($a['priority'] < $b['priority']) ? -1 : 1;
}
uasort($header_array, 'cmp');
Hi,
I have a function like this:
function column_links() {
$column = scandir("content");
foreach ($column as $value) {
$stvalue = str_replace(".php", "", $value);
echo "{$GLOBALS['tags'][$stvalue][title]}";
}
}
and this is my variable:
define('title', 0);
define('desc', 1);
define('order', 2);
$tags = array(
'index' => array('My Page', 'this is a description', '1'),
'about' => array('About', 'this is a description', '2'),
'sitemap' => array('Site Map', 'this is a description', '3'));
I want to sort the produced links by "order" rather than by file name so my html would look like this:
My Page
About
Site Map
I thought that array_multisort would do the trick but I cant even figure out where to put it. Any ideas please?
Thank you.
You can use the array function usort to define your own sorts!
The way it works is that you write your own function to determine how the array should be sorted. The function should return an integer less than, equal to, or greater than zero to determine how the first argument compares to the second.
For example:
I write a custom function called order_sort, which compares the ['order'] keys.
function order_sort($a, $b) {
if ($a['order'] > $b['order'])
return 1;
if ($a['order'] < $b['order'])
return -1;
return 0;
}
Then I can call usort on my array to sort it using my custom function.
usort($tags, 'order_sort');
Its keys will be discarded and the arrays will be sorted by ['order'].
Your code is a little confusing, I hope I understood your example right. If not, I hope my example helped you write your own!
This is the first time i create my own webservice (someone always did it for me before), so please bear with me.
I post this array :
$data = array(
'user_id' => $this->post('user_id'),
'group_id' => $this->post('group_id'),
'child_id' => $this->post('child_id'), //will be nested array
'custom' => $this->post('custom'),
'time' => $this->post('time'),
'date' => $this->post('date')
);
I tried to create a nested array with this : $this->post('child_id'), because user can post multiple child_id at once.
Then i tried to iterate through the child_id, because i need to insert them to the mysql :
for($i = 0; $i < sizeof($data['child_id']); $i++)
{
$result2 = $this->schedule_m->add_trans('transaction_schedule', $data, $result_id[0]['id']);
}
What should i do, so i can have an array of child_id in my $data array? (nested array)
And how to iterate through it?
UPDATE :
I have updated the codes above.
I use advanced rest client for testing, and i tried to post something like this in the form content type :
child_id=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id=2
Notice that theres two child_id (left most and right most), but only the last one (right most) is inserted.
And this is the add_trans in the model :
function add_trans($table, $data, $schedule_id) {
$query = $this->db->insert($table, array('child_id' => $data['child_id'], 'schedule_id' => $schedule_id));
return $query;
}
Thanks a lot for your time.
Even thought you set the name attribute as child[] on the markup,
You still need to call it as:
'child_id' => $this->post('child_id')
It will still return an array.
for($i = 0; $i < sizeof($data['child_id']); $i++) {
$result2 = $this->schedule_m->add_trans('transaction_schedule', $data, $result_id[0]['id']);
}
EDIT:
Looking upon you query string, that seems to be the culprit:
child_id=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id=2
^ same index , same index, same index, it will overwrite and you will get only `2`
If you want to get them all into an array format, you need to set them like this
child_id[]=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id[]=2
^ it needs to be set like this
UPDATE:
And in your model, if you want each id per row, well you can also loop in this case:
function add_trans($table, $data, $schedule_id) {
foreach($data['child_id'] as $child_id) {
$query = $this->db->insert($table, array('child_id' => $child_id, 'schedule_id' => $schedule_id));
}
// return $this->db->insert_id();
return $query;
}
ofcourse that won't work, it has to be
for($i = 0; $i < sizeof($data['child_id']); $i++)
{
$result2 = $this->schedule_m->add_trans('transaction_schedule', $data['child_id'][$i], $result_id[0]['id']);
}
because you've not set $data['child_id[]'] so it doesn't exist, the key is just a string or number, it does not validate or parse anything
you don't need to give child[] in post method. just give only child, it will get complete array what are you sending from views
replace
'child_id' => $this->post('child_id[]')
with
'child_id' => $this->post('child_id')
I'm pretty new to PHP so bear with me here. I'm trying to iterate through the words in a string of text, look for specific words, categorize them, and then count the number of times each word category was hit. I was able to do the easy part but I'm having problems counting the number of times each category is matched. Here's the main function that accepts my string:
public function matchThemeTest($query){
$marriageNum = 0;
$criminalNum = 0;
$contactNum = 0;
$keywords = array(
'background'=> array('category'=>'criminal'),
'marriage' => array('category'=>'marriage'),
'criminal' => array('category'=>'criminal'),
'arrest' => array('category'=>'criminal'),
'divorce' => array('category'=>'marriage'),
'person' => array('category'=>'contact'),
'contact' => array('category'=>'contact')
);
foreach (preg_split("/\s/", $query) as $word)
{
if (isset($keywords[$word]))
{
echo $keywords[$word]['category'];
if ($keywords[$word]['category'] == 'marriage') {
$marriageNum++;
}
echo $marriageNum;
}
}
//return reset($matches);
}
I've got a php fiddle setup here: http://phpfiddle.org/main/code/i4g-mdu that I've been playing around with. In it's current form, I can get the words into categories but I'm not sure how to count how many times each category gets matched. I feel like I need an additional loop or something simple to count but I'm not exactly sure where. Any advice is greatly appreciated. Thanks in advance.
You may need another array of data, to store the counts. Use an array like this:
$counts = array(
'criminal' => 0,
'marriage' => 0,
'contact' => 0
);
Then when you iterate through your foreach loop, you can use the $keywords[$word]['category'] as the key in $counts and increment it:
if(isset($keywords[$word]) {
$counts[$keywords[$word]['category']]++;
}
Then you can return the $counts array so the caller can use it to find out what the counts of each theme were:
return $counts;
I am seeking for someone's knowledge out here.
Right now, I would need to merge several arrays into a bigger one, but all of those arrays depend on a function.
This function returns a numeric array containing different quantities of numbers between 1 and 7 :
function Possible($i, $j, $grid)
$possible = Possible($i, $j, $grid)
I'm working with a grid, and the function returns a different array for every case of the grid. What I would like to do is to merge those 7 arrays into another one. Some numbers may be present more than once in this big array, but I want it this way.
I tried using for loops, while loops and some other techniques, but nothing worked. In this case, it is not possible to manually define every array, since they change depending of what is contained in the grid and there are too many. It has to be done automatically, and this is where I get stuck.
for ($jj=0; $j<7; $j++){
$possRow = array_merge( ###what do I add here or what do I change in this code to make everything work###
Thank you if someone can help me out!
Etpi
hope this help:
$biggerOneArray = array();
for($k=0;$k<7;$k++) {
$biggerOneArray[] = Possible($i,$j,$grid);
}
Then you can check your bigger array, may contains all arrays of the iterations of the loop (7 arrays merged).
var_dump($biggerOneArray);
The output should be this:
array(
(int) 0 => array(
'key' => 'value',
'key2' => 'value2'
),
(int) 1 => array(
'key3' => 'value3',
'key4' => 'value4'
)
)
etc...
I'm sorry but your description isn't very clear. But just to get you started you might look at this solution.
function Possible($i, $j, $grid) {
// some code ... e.g. $array[] = "some data";
return $array;
}
By creating a small array for each grid and returning it using return $array you get a few little arrays which you can inturn place into a for loop to merge it into one larger array. However i believe the var $jj must have some meaning in the function it self as well.
for($jj=0;$jj<7;$jj++) {
$merged_array[$jj] = Possible($i,$j,$grid);
}
Maybe if you descripe your problem a little more and post an exmple of the array's your working with i can give you a better answer.