0

I am working with php, and made this little function to only return an array with the same month, but I need this to be returned in order of day too, I tryed to use sort, usort and neither worked out.

$jsonFile     = file_get_contents(BASE_TEMPLATE . '/templates/json/' . AMBIENTE . '/' . 'demo_birthday' . '-data.json');
$jsonData     = json_decode($jsonFile);

If I print $jsonData I get and array with 25 objects, something like this:

array(25) { 
    [0]=> object(stdClass)#91 (6) { 
            ["id"]=> string(3) "105" 
            ["day"]=> string(1) "6" 
            ["month"]=> string(7) "febrero" 
            ["name"]=> string(15) "diego fernandez" 
            ["created"]=> string(19) "2021-02-18 07:16:05" 
            ["modified"]=> string(19) "2021-02-18 07:34:18" 
            }

            .
            .
            .

    [24]=> object(stdClass)#115 (6) { 
            ["id"]=> string(3) "129" 
            ["day"]=> string(2) "25" 
            ["month"]=> string(1) "2" 
            ["name"]=> string(15) "martin riquelme" 
            ["created"]=> string(19) "2021-02-18 07:16:06" 
            ["modified"]=> string(19) "2021-02-18 07:16:06" 
            } 
};


// this is used as an array map to compare with actual month
$arrayMonth = array(
    array('ENERO', 'ENE', '01', '1'),
    array('FEBRERO', 'FEB', '02', '2'),
    array('MARZO', 'MAR', '03', '3'),
    array('ABRIL', 'ABR', '04', '4'),
    array('MAYO', 'MAY', '05', '5'),
    array('JUNIO', 'JUN', '06', '6'),
    array('JULIO', 'JUL', '07', '7'),
    array('AGOSTO', 'AGO', '08', '8'),
    array('SEPTIEMBRE', 'SEPT', '09', '9'),
    array('OCTUBRE', 'OCT', '10'),
    array('NOVIEMBRE', 'NOV', '11'),
    array('DICIEMBRE', 'DIC', '12')
);

// this puts the json data on Slides
foreach ($jsonData as &$slide) {
    $resultNull = getBirthdayMonth($actualMonth, $slide, $arrayMonth);

    if (!empty($resultNull)) {
        array_push($result, getBirthdayMonth($actualMonth, $slide, $arrayMonth));    
    }
}


// This is the function that I need to return in order of day
function getBirthdayMonth($actualMonth, $slide, $arrayMonth)
{
    $month = array();

    if (in_array(strtoupper($slide->month), $arrayMonth[$actualMonth - 1])) {
        array_push($month, array($slide->day, $arrayMonth[$actualMonth - 1][0], $slide->name));
    }

    return $month;
}

I need that this array, month, get saved in orden of day, I don't seem to understand what I'm doing wrong here.

This is my actual output when I use print_r(getBirthdayMonth($actualMonth, $slide, $arrayMonth));

Array ( [0] => Array ( [0] => 6 [1] => FEBRERO [2] => diego fernandez ) )
Array ( [0] => Array ( [0] => 2 [1] => FEBRERO [2] => mirko valencia ) )
Array ( [0] => Array ( [0] => 5 [1] => FEBRERO [2] => pablo soto ) )
Array ( [0] => Array ( [0] => 1 [1] => FEBRERO [2] => lorena onate ) )
Array ( [0] => Array ( [0] => 5 [1] => FEBRERO [2] => daniela zuñiga ) )

But I need to get this:

Array ( [0] => Array ( [0] => 1 [1] => FEBRERO [2] => lorena onate ) )
Array ( [0] => Array ( [0] => 2 [1] => FEBRERO [2] => mirko valencia ) )
Array ( [0] => Array ( [0] => 5 [1] => FEBRERO [2] => daniela zuñiga ) )
Array ( [0] => Array ( [0] => 5 [1] => FEBRERO [2] => pablo soto ) )
Array ( [0] => Array ( [0] => 6 [1] => FEBRERO [2] => diego fernandez ) )
  • 2
    I think it would be useful if we knew what the things in your code contain. What is `$slide`? What is in `$arrayMonth`? Etc. We cannot guess. What should this function accomplish? In short: it would be nice to know what the input is, and what output you expect. – KIKO Software Feb 18 '21 at 15:25
  • You might want to include an example of your current output and wanted output as well. Including your sorting attempts might also be a good thing (you might get an explanation why they failed to work and learn a thing or two). – El_Vanja Feb 18 '21 at 15:27
  • @KIKOSoftware Thanks for the feedback, hope this edit helps – Daniela Paz Feb 18 '21 at 15:37
  • I think it does. Thank you. – KIKO Software Feb 18 '21 at 15:38
  • @El_Vanja ok, thanks, I put the ouput I'm getting, and what I need no get but I don't seem to understand what I should do, cause sort doesn't work – Daniela Paz Feb 18 '21 at 15:38
  • Does this answer your question? [How to Sort Multi-dimensional Array by Value?](https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value) – El_Vanja Feb 18 '21 at 15:41
  • Sorry, but `$jsonData` containing the `$slide`s is still unknown. We don't need all the code, just something we can work with. – KIKO Software Feb 18 '21 at 15:41
  • I'm guessing your sorts didn't work because you operated on the whole array (which actually only has one element, which is another array). `sort` won't work with the deeper levels. For that, consult the linked duplicate. – El_Vanja Feb 18 '21 at 15:42
  • @KIKOSoftware thanks for taking the time, hope this new edit helps more – Daniela Paz Feb 18 '21 at 16:01
  • OK, did you look at the link El_Vanja gave you? You will have to use `usort()` at the end of `obtenerCumpleanosMes()` to sort the result. – KIKO Software Feb 18 '21 at 16:10

1 Answers1

2

Given what you show with print_r you can either use array_multisort:

$values = getBirthdayMonth($actualMonth, $slide, $arrayMonth);
array_multisort($values); // The default options work for your case

Or usort like you said:

$values = getBirthdayMonth($actualMonth, $slide, $arrayMonth);
function cmp($a, $b)
{
    if ($a[0] == $b[0]) {
        return 0;
    }
    return ($a[0] < $b[0]) ? -1 : 1;
}

usort($values, "cmp");

//Or even shorter:
usort($values, function ($a, $b) {
  return gmp_sign($a[0] - $b[0]);
}); 
Mohameth
  • 376
  • 2
  • 10