0

I am a little rusty on my PHP and I am trying to create a function that will build an array. Essentially, I am trying to have an associative array of contacts ($contacts), with each individual element being a separate array ($contact). For starters, I would like it to be able to have a UniqueID,FirstName,and LastName within the elements. Here is the direction I am heading in right now:

    <?php

$contact=array();
function createContact($Unique_ID,$FirstName,$LastName){
global $contact;
$contact=array("Unique_ID"=>$Unique_ID,"First_Name"=>$FirstName,"Last_Name"=>$LastName);
};
createContact("123456","John","Smith");
createContact("654321","Jane","Doe");
createContact("331143","Steve","Sample");

foreach($contact as $key=>$value){
echo $key.",",$value."<br>";
};

?>

This should create the $contact array with 3 separate records, but it only adds the last entry (In this case Steve Sample because it is the last one that was run). I remember learning somewhat about the global variables, but I think I am using it incorrectly. After I solve this, I will find a way to make an array containing all of these arrays.

1
  • You need to append to the array: $contact[] = array(...);. Commented Jun 13, 2014 at 20:25

2 Answers 2

2

You are overwriting the array $contact every time you invoke createContact(). You have to append the new records

function createContact($Unique_ID,$FirstName,$LastName){
    $contact[] = array("Unique_ID" => $Unique_ID, "First_Name" => $FirstName, "Last_Name" => $LastName);
};

Note the [] to indicate that you want to append another entry.

If you want to print these you probably want to do something like this

foreach ($contact as $item) {
    echo "ID = " . $item["Unique_ID"] . "<br>";
    echo "First name = " . $item["First_Name"] . "<br>";
    echo "Last name = " . $item["Last_Name"] . "<br>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need additional array.

$final_array = array();

and use

array_push($final_array, $contact);

inside the createContact function

3 Comments

This works as well, thank you. One more question though. How would I go about echo-ing out this final array in a readable format? Like how could I use a foreach to print out every $contact within the $final_array?
Lets say, you have the following array after you push the data: $final_array = array ( array ( 'name' => 'prashant', 'phone' => '12345'), array ( 'name' => 'chris', 'phone' => '1234567'), ); To print this: if(true===is_array($final_array)){ foreach($final_array as $contact) { if(true==is_array($contact)) { echo "Name : ".$contact['name']. ", Phone: ".$contact['phone']."<br />"; } } }
Hello, the only issue with this is that it is printing all of the records every time it tries to print the next one. For example, it will echo out "Name: John, Phone:123456; Name: John, Phone: 123456 Name: Jane, Phone:654321;"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.