Search code examples
phpvariablesarray-multisort

can a php variable contain a 'array' and 'sort_asc' to be used in array_multisort?


Essentially I would like to create a variable with a string to be used later in array_multisort. Is this possible? E.g.

<?php

$variable = "$array['name'], SORT_ASC";

array_multisort($variable, $rows);

I realise that it's got something to do with the SORT_ASC being constant but I wouldn't know why.I also realise there might be a better way to achieve what I'm trying to do. Thanks.


Solution

  • You can use a multidimensional array for that purpose:

    <?php
    
    $examp = array("array" => $array,
              "SORT" => SORT_ASC);
    
    if(isset($examp)){
        array_multisort($examp["array"], $examp["SORT"]);
    }
    
    ?>