Hello,

I want to learn the principal of this code, it works but I don't know if it can be done a different way without creating the variable $new

function fruits(){
$basket=array('john'=>'apple','lisa'=>'pear','peter'=>'cherry','jack'=>'grapes');
return $basket;
}//function fruits(){

$new=fruits();

print_r($new);

maybe an echo directly to the function fruits? How?

    spighita wrote:

    maybe an echo directly to the function fruits? How?

    Instead of putting $new as the first parameter to [man]print_r/man, try putting fruits() instead.

      I've tried using it, but I don't know how to code it.

        Show us what you tried so we can see what you're doing wrong.

          Thanks for such a quick reply.

          function fruits(){
          $basket=array('john'=>'apple','lisa'=>'pear','peter'=>'cherry','jack'=>'grapes');
          return $basket;
          }//function fruits(){
          
          //this obviously doesn't work
          echo fruits()['john'];
          
          //and I've tried this also and no success
          echo fruits['john'];
          

            Oh... I thought you were simply trying to pass the entire array to another function (e.g. [man]print_r/man).

            If you're trying to select a specific index from the returned array, then you will have to first store the array in a variable (as you were doing before).

              Thanks for the feedback and insight on this. Greatly appreciated.

                Write a Reply...