• Sorting arrays

      0 comments

    What is the best way to sort an array by maintaining it’s key associations as well?

    $array = array(‘a’=>’foo’,'b’=>’bar’,'c’=>’baz’);
    asort($array);
    print_r($array);

    Array
    (
    [b] => bar
    => baz
    [a] => foo
    )

  • Passive Iteration

      0 comments

    What will be the out put of the following code snippet ?

    <?php
    function setcase(&$value, &$key){
    $value = strtoupper($value);
    }
    $type = array('internal','external');
    $format[] = array('rss','html','xml');
    $format[] = array('csv','jason');
    $map = array_combine($type, $format);
    array_walk_recursive($map, 'setcase');
    print_r($map);
    ?>

    Only scalar values will be converted to upper cases. ‘ internal’ and ‘external’ are not passed into the user defined function.

    Array
    (
        [internal] =&gt; Array
            (
                [0] =&gt; RSS
                [1] =&gt; HTML
                [2] =&gt; XML
            )
    
        [external] =&gt; Array
            (
                [0] =&gt; CSV
                [1] =&gt; JASON
            )
    
    )