Search code examples
phparrayssorting

Sorting an array before inserting to database


I hate to beat a dead horse, but here goes. I have an Apache2 server running and I create my own php/html forms for in and output with mysql. Works great, I love it.

My problem is I have a huge array that I want to insert to MySQL but it needs to be sorted first (0<00). I've gone through the many Q & A covering the subject here and the code supplied works on a single array just fine. However, I need to loop through about 7000 sets and I'd like the output to be in original format but I have had no luck making that work as desired.

$data = array(
    array(29,11,15,30,33),
    array(30,11,25,18,02),
    array(12,15,08,06,18),
    array(17,20,03,21,02),
    array(26,27,12,30,11),
    array(05,25,34,11,16),
    array(29,11,06,30,14),
    array(05,26,12,18,33),
    array(23,28,05,22,09),
    array(05,36,31,32,27),
    array(02,06,03,05,14)
);

Solution

  • An easy way to sort each sub array:

    $data = array(
        array(29,11,15,30,33),
        array(30,11,25,18,02),
        array(12,15,08,06,18),
        array(17,20,03,21,02),
        array(26,27,12,30,11),
        array(05,25,34,11,16),
        array(29,11,06,30,14),
        array(05,26,12,18,33),
        array(23,28,05,22,09),
        array(05,36,31,32,27),
        array(02,06,03,05,14)
    );
    
    foreach($data as &$value){ // Mind the & byref value
        sort($value);
    }
    
    var_dump($data);
    // $data is now sorted over here
    

    The output:

    array(11) {
      [0]=>
      array(5) {
        [0]=>
        int(11)
        [1]=>
        int(15)
        [2]=>
        int(29)
        [3]=>
        int(30)
        [4]=>
        int(33)
      }
      [1]=>
      array(5) {
        [0]=>
        int(2)
        [1]=>
        int(11)
        [2]=>
        int(18)
        [3]=>
        int(25)
        [4]=>
        int(30)
      }
      [2]=>
      array(5) {
        [0]=>
        int(0)
        [1]=>
        int(6)
        [2]=>
        int(12)
        [3]=>
        int(15)
        [4]=>
        int(18)
      }
      [3]=>
      array(5) {
        [0]=>
        int(2)
        [1]=>
        int(3)
        [2]=>
        int(17)
        [3]=>
        int(20)
        [4]=>
        int(21)
      }
      [4]=>
      array(5) {
        [0]=>
        int(11)
        [1]=>
        int(12)
        [2]=>
        int(26)
        [3]=>
        int(27)
        [4]=>
        int(30)
      }
      [5]=>
      array(5) {
        [0]=>
        int(5)
        [1]=>
        int(11)
        [2]=>
        int(16)
        [3]=>
        int(25)
        [4]=>
        int(34)
      }
      [6]=>
      array(5) {
        [0]=>
        int(6)
        [1]=>
        int(11)
        [2]=>
        int(14)
        [3]=>
        int(29)
        [4]=>
        int(30)
      }
      [7]=>
      array(5) {
        [0]=>
        int(5)
        [1]=>
        int(12)
        [2]=>
        int(18)
        [3]=>
        int(26)
        [4]=>
        int(33)
      }
      [8]=>
      array(5) {
        [0]=>
        int(0)
        [1]=>
        int(5)
        [2]=>
        int(22)
        [3]=>
        int(23)
        [4]=>
        int(28)
      }
      [9]=>
      array(5) {
        [0]=>
        int(5)
        [1]=>
        int(27)
        [2]=>
        int(31)
        [3]=>
        int(32)
        [4]=>
        int(36)
      }
      [10]=>
      &array(5) {
        [0]=>
        int(2)
        [1]=>
        int(3)
        [2]=>
        int(5)
        [3]=>
        int(6)
        [4]=>
        int(14)
      }
    }
    

    EDIT

    Or like @JREAM suggest, you can use array_walk:

    function sort_on_walk(&$value, $key) 
    {
        sort($value);
    }
    
    array_walk($data, 'sort_on_walk');