Stoopkid
Stoopkid

Reputation: 1955

Create 2D array from several arrays with matching keys with possible omissions

I will have a few arrays that I want to display as a table. They have matching keys, however some might be missing some keys, some might have keys that none of the others do. Is there a simple way to combine these arrays into a 2D array that will not omit elements that are missing from some of them and will not collapse the array so that some of the values are placed into the wrong "column"?

More specifically, I am wondering if there is a function that is meant for this or if I would just need to code it myself.

Upvotes: 0

Views: 30

Answers (1)

lakotuts
lakotuts

Reputation: 61

I will try to produce here something as a 2D array with a similar problem like yours. Let`s suppose that we have an X number of arrays where each array represents a person.

$person1= array(
 "name" => "Lako",
 "surname" => "Tuts",
 "age" =>25
          );

$person2 = array(
 "name" => "Igor",
 "country" => "Croatia",
 "age" =>25
);

Here we have two person arrays with similar but different information. The main difference is in the keys surname and country which do not exist in both arrays.

We will need to iterate them but to make our job a little easier lets combine their variable names in an array which then we can iterate.

$arrays = array("person1","person2");

We could directly save both arrays in the variable $arrays but there is no need to fill the memory with duplicate information.

We need to know now all keys in all arrays so that we can afterwards check which keys exist and which do not.

$arrayKeys = array();              
foreach( $arrays as $value ){

 $thisArrayKeys = array_keys($$value);

 $arrayKeys = array_merge($arrayKeys ,$thisArrayKeys );

 }

We have made an empty array to store keys arrayKeys. Then we iterate the array with the name of variables that hold person information arrays. We are using double dollar signs to get the variable that will have the same name as the value in these array. "person" => $"person" => $person.

Now that we have all the keys that are in all arrays, let`s make them unique so that we do not have duplicate keys.

$arrayKeys = array_unique($arrayKeys);

We need a new array which will be the 2D array we need that will hold all the formated information about each person.

//the new array
$theNewArray = array();

foreach( $arrays as $value ){
    //get the array info for the person
    //first iteration will be $person1
    $personArray = $$value;

    //for each unique key we have, we will check if the key does exist
    //in the current person array. If it does not exist we then make a 
    //new entry in the array with that key and an empty value
    foreach($arrayKeys as $key){
      if(!array_key_exists($key, $personArray)) {
         $personArray[$key] = "";
      }
    }
    //Now that we have an array filled with missing keys lets sort it by 
    //the keys so that we have each person information with the same key order
    ksort($personArray);

    //Push that person array in the new array
    $theNewArray[] = $personArray;

}

If you print the variable theNewArray you will get this:

Array
(
    [0] => Array
        (
            [age] => 25
            [country] => 
            [name] => Lako
            [surname] => Tuts
        )

    [1] => Array
        (
            [age] => 25
            [country] => Croatia
            [name] => Igor
            [surname] => 
        )

)

I hope that this is what you need and that will help you solve your problem.

Upvotes: 1

Related Questions