-1

I am using PHP and a multidimensional array. I have stored points and other information as string variables. It is very important that I use string variables in my array.

I would like to sort the array and add 3 new items. I should be able to sort my subarrays. I also should be able to sort my string variables of points (pts_sting) and competitions (c_string).

I need some kind of foreach loop to do the job automatically.

Maybe the following example helps more than my words.

Array (
  [25] => Array (
    [1] => Array (
      [pts_string] => 00450
      [c_string] => 00011
    )

    [2] => Array (
      [pts_string] => 00600
      [c_string] => 00025
    )

    [3] => Array (
      [pts_string] => 00375
      [c_string] => 00033
    )
  )
)

The result should look like this:

Array (
  [25] => Array (
    [pts_total] = 1425 /* 600 + 450 + 375 */
    [all_pts_strings] = 00600 00450 00375 /* biggest points, 2nd biggest, etc. */
    [all_c_strings] = 00025 00011 00033 /* competition of biggest points, 2nd biggest, etc. */
    [no_of_competitions] = 3 /* [1], [2], and [3] = 3 in total */

/* biggest points first... */

    [2] => Array (
      [pts_string] => 00600
      [c_string] => 00025
    )

/* 2nd biggest points... */

    [1] => Array (
      [pts_string] => 00450
      [c_string] => 00011
    )

/* 3rd biggest points... */

    [3] => Array (
      [pts_string] => 00375
      [c_string] => 00033
    )
  )
)
xms
  • 341
  • 2
  • 16
  • Possible duplicate of [How can I sort arrays and data in PHP?](http://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – zett42 Mar 26 '17 at 19:12

1 Answers1

0

You can do that:

$arr = [
    "25" => [
        "1" => ["pts_string" => "00450", "c_string" => "00011"],
        "2" => ["pts_string" => "00600", "c_string" => "00025"],
        "3" => ["pts_string" => "00375", "c_string" => "00033"]
    ]
];

uasort($arr["25"], function ($a, $b) { return $b['pts_string'] - $a['pts_string']; }); 

$pts = array_column($arr["25"], "pts_string");
$c = array_column($arr["25"], "c_string");

$arr["25"] = [ "pts_total" => array_sum($pts),
               "all_pts_strings" => implode(' ', $pts),
               "all_c_strings" => implode(' ', $c),
               "no_of_competitions" => count($arr["25"])
             ] + $arr["25"];

print_r($arr);

If you have to do it for each item in the array, put all the code in a foreach loop and replace $arr["25"] with $item:

foreach ($arr as &$item) {
    ...
}
Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
  • Thanks, looks nice. If I do not know that the subarray is [25], how could I modify the code? The subarray number can be almost anything. – xms Mar 26 '17 at 18:24
  • So, if I have several subarrays, let's say [20], [23], and [25], will the foreach loop go through all of them? – xms Mar 26 '17 at 18:27
  • uasort row gives me: Warning: Illegal string offset... What to do? – xms Mar 26 '17 at 18:48
  • @xms: Change the function to handle cases when the key `pts_string` doesn't exist. – Casimir et Hippolyte Mar 26 '17 at 18:52
  • But actually the code fully answers the question with the data you posted. – Casimir et Hippolyte Mar 26 '17 at 18:54
  • I added `if (isset($b['pts_string']) && isset($a['pts_string']))` to uasort. No error messages now, but is this the best way? If that is false, nothing happens. – xms Mar 26 '17 at 19:03
  • @xms: how a comparison functions work is explained [here](http://php.net/manual/en/function.usort.php) – Casimir et Hippolyte Mar 26 '17 at 20:03
  • @xms: as an aside, I don't know what you are trying to do, but instead of building a monster multidimensional array, you should build a simple array of objects that have the properties and methods you need. – Casimir et Hippolyte Mar 26 '17 at 20:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139103/discussion-between-xms-and-casimir-et-hippolyte). – xms Mar 26 '17 at 22:16