Jump to content

Need help echoing out an array using a function


eldan88
Go to solution Solved by PaulRyan,

Recommended Posts

Hey,

 

 I am new to learning PHP and I am trying to echo out an array using a function, but its just displaying "array" Can someone please tell me what am i doing incorrectly? Thanks!

<?php function get_stores() {
$store_array = array();
$stores = array('store1','store2');
foreach ($stores as $store_temp) {
	$store_array[] = $store_temp;
     	} return $store_array;
}

echo get_stores();



?>

Link to comment
Share on other sites


<?php function get_stores() {

$stores = array( "First Name" => "Kevin", "Last Name" => "Smith", "Occupation" => "Director" );
foreach ($stores as $key => $value) {
     echo "key = " . $key . " value = " . $value . "<br />";
    }
return $stores;
}

// use print_r instead to see how the array is setup
$my_array =  get_stores();
print_r($my_array);



?>

 

Edited by Strider64
Link to comment
Share on other sites

  • Solution

 

<?PHP

  function get_stores() {
    $store_array = array();
    $stores = array('store1','store2');
    foreach ($stores as $store_temp) {
      $store_array[] = $store_temp;
    }
    
    return print_r($store_array, TRUE);
  }

  echo get_stores();

?>
Link to comment
Share on other sites

<?PHP

  function get_stores() {
    $store_array = array();
    $stores = array('store1','store2');
    foreach ($stores as $store_temp) {
      $store_array[] = $store_temp;
    }
    
    return print_r($store_array, TRUE);
  }

  echo get_stores();

?>

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.