JamesG
JamesG

Reputation: 2018

Array with different Key => Value pairs into a table PHP

I have generated a "master" array which looks a little like this:

Array ( [userID] => 152 [email] => [email protected] [name] => Jay jay go go [stream] => 616 ) 
Array ( [userID] => 133 [email] => [email protected] [name] => Damian T [stream] => 616 ) 
Array ( [userID] => 154 [email] => [email protected] [name] => Julie1000 E [stream] => 615 [PROGRAMME] => Designer ) 
Array ( [userID] => 153 [email] => [email protected] [name] => James1000 G [stream] => 616 [PROGRAMME] => Apple Develpepr ) 

This is the output from a loop through a single array, so each of these 4 arrays are have keys 0 - 3.

My problem is that I want a table creating to hold all this data, with each row representing a user, but the array keys can be slightly different. For example, the first 2 users have no "programme" array key/value, but I want there to be a column saying "programme" but have it empty for users that dont have this in their array.

Hope that makes sense.

Upvotes: 0

Views: 1709

Answers (3)

Kerem
Kerem

Reputation: 11576

If I didn't mistake your question, by doing this, you can create new array with max array keys;

$data = array(
    array('userID' => 152, 'email' => '[email protected]', 'name' => 'Jay jay go go', 'stream' => 616),
    array('userID' => 133, 'email' => '[email protected]', 'name' => 'Damian T', 'stream' => 616),
    array('userID' => 154, 'email' => '[email protected]', 'name' => 'Julie1000 E', 'stream' => 615, 'PROGRAMME' => 'Designer'),
    array('userID' => 153, 'email' => '[email protected]', 'name' => 'James1000 G', 'stream' => 616, 'PROGRAMME' => 'Apple Develpepr'),
);
$max_len = null;
$max_arr = null;
// First we find max array to grab its keys
foreach ($data as $i => $a) {
    $len = count($a);
    if ($max_len === null || $len > $max_len) {
        $max_len = $len;
        $max_arr = $data[$i];
    }
}
$max_arr_keys = array_keys($max_arr);
$data_new = array();
foreach ($data as $i => $a) {
    // and using max array keys here
    foreach ($max_arr_keys as $k) {
        // key exists? get value, or set as NULL
        $data_new[$i][$k] = isset($a[$k]) ? $a[$k] : null;
    }
}
print_r($data_new);

And output should look like this;

Array ( [userID] => 152 [email] => [email protected] [name] => Jay jay go go [stream] => 616 [PROGRAMME] => null )
Array ( [userID] => 133 [email] => [email protected] [name] => Damian T [stream] => 616 [PROGRAMME] => null ) 
Array ( [userID] => 154 [email] => [email protected] [name] => Julie1000 E [stream] => 615 [PROGRAMME] => Designer ) 
Array ( [userID] => 153 [email] => [email protected] [name] => James1000 G [stream] => 616 [PROGRAMME] => Apple Develpepr ) 

Upvotes: 1

PleaseStand
PleaseStand

Reputation: 32102

You could make a list of columns to include in the table, and then for each user, use isset to check whether there is a value for each column:

$columnNames = array('userID', 'email', 'name', 'stream', 'PROGRAMME');

foreach ($users as $user) {
    echo '<tr>';
    foreach ($columnNames as $columnName) {
        echo '<td>';
        if (isset($user[$columnName])) {
            echo htmlspecialchars($user[$columnName]);
        }
        echo '</td>';
    }
    echo '</tr>';
}

Upvotes: 1

four43
four43

Reputation: 1750

This can be accomplished by merging all of those arrays into a single array. Loop through every item in the array, gathering the keys (in_array might help there). Loop through your keys array to print out all the column headers in your table, then when your script needs to output the array you can simply run in a loop over each person, looping through each property as it maps to the column. That's kind of abstract, but I think you can fill in the rest. Good luck!

Upvotes: 2

Related Questions