1

I have an array which has these cells:

update_table('0', 'Real Klarin', '1', '2', '3', '4', '5', '6', '12', '8');
update_table('1', 'RAKoun', '1', '2', '3', '4', '5', '6', '11', '9');
update_table('2', 'Panklouviakos', '1', '2', '3', '4', '5', '6', '10', '10');
update_table('3', 'Ouza&Zabon', '1', '2', '3', '4', '5', '6', '9', '11');
update_table('4', 'ui48975', '1', '2', '3', '4', '5', '6', '8', '12');
update_table('5', 'Genia tou 98', '1', '2', '3', '4', '5', '6', '7', '13');

and is constructed in a loop like this:

$arrayOfCalls[] = "update_table('".($inc-1)."', '".$team."', '".$gp."', '".$w."', '".$d."', '".$l."', '".$gf."', '".$ga."', '".$gd."', '".$pts."');";

I would like to sort by the last parameter of update_table(), i.e. $pts. So, I read this question: Sort Multi-dimensional Array by Value, which implies that I should have a multidimensional array, which is not what I have. I tried $arrayOfCalls['.$pts.'], but that did not have any effect.

What should I do (notice that I am a beginner in PHP)?

Oh and in case of a tie, I would like to sort by $gd, but we can get the sort run by the first key, I think I will find a way for the second!

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • @Memor-X I think I understand, but I am still not sure. Can you post an answer please? :) – gsamaras Dec 09 '15 at 01:52
  • is that a mysql user defined function? my suggestion is just create a multi dimensional array with all the values, sort then thru usort just like the answer/question you linked, then prepare the statement with your mysql function and bind those instead, including a use of a simple foreach – Kevin Dec 09 '15 at 01:58
  • @Ghost it boils down to them just being values from a file. I am having issues with creating the arrays, I am still trying and if I have something good, I will update.. – gsamaras Dec 09 '15 at 02:07

1 Answers1

1

considering that update_table looks like a function all the data you are passing could be put into a multi-dimensional array, eg.

$updateArr = array('0' => array('Real Klarin', '1', '2', '3', '4', '5', '6', '12', '8'),
'1' => array('RAKoun', '1', '2', '3', '4', '5', '6', '11', '9'),
'2' => array('Panklouviakos', '1', '2', '3', '4', '5', '6', '10', '10'),
'3' => array('Ouza&Zabon', '1', '2', '3', '4', '5', '6', '9', '111'),
'4' => array('ui48975', '1', '2', '3', '4', '5', '6', '8', '12'),
'5' => array('Genia tou 98', '1', '2', '3', '4', '5', '6', '7', '13'));

and then you can populate you $arrayofCalls array

$arrayOfCalls = array();
foreach($updateArr as $id => $values)
{
  array_push($arrayOfCalls,"update_table('".$id."', '".$values[0]."', '".$values[1]."', '".$values[2]."', '".$values[3]."', '".$values[4]."', '".$values[5]."', '".$values[6]."', '".$values[7]."', '".$values[8]."');");
}

this way you can do your multi-dimensional array sorting before your foreach loop. as you can see $updateArr is more or less in the same format as what the question describes

Community
  • 1
  • 1
Memor-X
  • 2,870
  • 6
  • 33
  • 57
  • This will throw an error, since `update_table()` is not a PHP function. What exactly do you want to do in the loop? – gsamaras Dec 09 '15 at 02:14
  • @gsamaras what exactly is your first code block? is it PHP or a big MySQL string – Memor-X Dec 09 '15 at 02:16
  • These are the cells of my array. They are just strings, sorry for not mentioning earlier! – gsamaras Dec 09 '15 at 02:17
  • @gsamaras i think that fixed it....was wondering what `$arrayOfCalls` was supposed to be. the basic idea however is the same, your problem is that you found a way to sort multi-dimensional arrays but you didn't have one so you make your data look like one – Memor-X Dec 09 '15 at 02:24
  • Maybe I am echoing the array wrongly, because I see no difference than my previous array. Sorry! I corrected a syntax error you had. – gsamaras Dec 09 '15 at 02:28
  • @gsamaras actually looking at the original data the last value is ordered. maybe changed that 11 to 111 and don't forget your sorting before the `foreach` – Memor-X Dec 09 '15 at 02:29
  • OK good point. How am I going to tell the sorting function to take into account the last parameter? – gsamaras Dec 09 '15 at 02:31
  • 1
    @gsamaras i'm not familiar with the sorting code but assuming your using code from the [accepted answer](http://stackoverflow.com/a/2699159/1028804) then `$a['order']` and `$b['order']` would be changed to `$a[8]` and `$b[8]`. or with the arrays you can go `array('Ouza&Zabon', '1', '2', '3', '4', '5', '6', '9', 'order' => '111')` but then you would have to remember that `$values[8]` gets changed to `$values['order']` – Memor-X Dec 09 '15 at 02:38