1

I have two arrays:

 $array1 = array('name', 'email', 'phone');
 $array2 = array('name' => 'John', 'phone' => '55-555-555');

I have created a HTML table from this two arrays, array1 is the table heads, and array2 is the td contents.

what happens now is that becouse array2 is missing a value (email in this case) I will get the phone data under the mail column. i would like to get this result:

 $array2 = array('name' => 'John', 'email' => ' ', 'phone' => '55-555-555');

I tried to follow this answer, but it has two main problems:

  1. the array keys desapear.
  2. the 0 values are added at the end of the array and not in their original location.
5
  • 1
    "I have created a table ..." -- do you mean an HTML <table>? Commented Nov 10, 2016 at 11:55
  • yes, sorry HTML table. Commented Nov 10, 2016 at 11:56
  • @DavSev Did you see the answers? Commented Nov 10, 2016 at 12:18
  • yes i did, thank you. I am tring them. Commented Nov 10, 2016 at 12:28
  • Related: how to set default value for array(php) Commented Sep 26, 2022 at 20:40

3 Answers 3

3

Your source array is wrong. You need to have something like this:

$array1 = array('name' => '', 'email' => '', 'phone' => '');

And if you want to extend with the empty values, you can use:

$array2 = array('name' => 'John', 'phone' => '55-555-555');

The final array can be made by using array_merge:

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

$array2 = array_merge($array1, $array2);

You will get this as output:

array('name' => 'John', 'email' => '', 'phone' => '55-555-555');

Full Code

$array1 = array('name' => '', 'email' => '', 'phone' => '');
$array2 = array('name' => 'John', 'phone' => '55-555-555');
$array2 = array_merge($array1, $array2);
var_export($array2);

Output

array (
  'name' => 'John',
  'email' => '',
  'phone' => '55-555-555',
)

Demo: http://ideone.com/7zlab7


Better Explanation

// Have a base array that has all the required fields.
$baseArr = array('name', 'email', 'phone', 'password');
// Get your array with the initial values, (without a few fields).
$myArray = array('name' => 'david', 'email' => '[email protected]', 'phone'=> '123456789');
// Now make a new array on the fly with array_fill_keys using the baseArr
// and merge with the original user array.
$myArray = array_merge(array_fill_keys($baseArr, ''), $myArray);
// The resultant array will have all the fields.
print_r($myArray);

Output

Array
(
    [name] => david
    [email] => [email protected]
    [phone] => 123456789
    [password] => 
)

Demo: http://ideone.com/XLCMN9

Sign up to request clarification or add additional context in comments.

7 Comments

Any pointers on why this is downvoted will be helpful. Well I am happy to learn.
I cant get to empty the values in the $array1 array, How can i do it so i will get the result you have published above?
@DavSev How can you do what?
Set all values to ' ' like you explaind on the second row. now i manage to get an assocciative array but all the keys have date. i tried to use array_fill_keys but it converts the array values into array keys!
@DavSev Yes, you should use array_fill_keys. That should work for you. And send the original array. What's wrong is happening in it?
|
1

You can use the PHP function array_key_exists() to find out if the data array contains a value for the column you want to print.

Something like this:

$array1 = array('name', 'email', 'phone');
$array2 = array('name' => 'John', 'phone' => '55-555-555');

// Echo <table>, table header row etc here

// Print the data row
echo('<tr>');
foreach ($array1 as $key) {
    if (array_key_exists($key, $array2)) {
        echo('<td>'.$array2[$key].'</td>');
    } else {
        echo('<td></td>');
    }
}
echo('</tr>');

You can use the same technique to fill the missing values in $array2 (and keep the rendering code unchanged):

foreach ($array1 as $key) {
    if (! array_key_exists($key, $array2) {
        $array1[$key] = '';
    }
}

Comments

1

Praveen Kumar's answer should be the accepted one, but here's another approach which also works:

$array_1 = array('name', 'phone', 'address');
$array_2 = array('name' => 'john', 'address' => 'street number');

foreach ($array_1 as $key) {
    if (!array_key_exists($key, $array_2)) {
        $array_2[$key] = '';
    }
}

var_export($array_2);

This will output the following result:

array ( 'name' => 'john', 'address' => 'street number', 'phone' => '' )

Comments

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.