Home > Mobile >  Best way of combining two arrays in PHP
Best way of combining two arrays in PHP

Time:10-11

Hey there so I have combined two arrays but I think that's not the right way to do it in PHP. Can you tell me a shorter and better way to do this:

So I have created two arrays called Developers and Projects.

#Creating Developers Array.
$developers = [
    'Ammar',
    'Waqar',
    'Sheraz',
    'Suleman',
];

#Creating Project Array.
$projects = [
    'Sail Caribe',
    'Hacker Craft',
    'Jackson Demolition',
    'Carribean Yacht Charter',
];

#Adding a name key to Developers Array.
$devNames = [];
foreach($developers as $developer){
    $devNames[] = [
        'name' => $developer,
    ];
}


#Adding a project name key to Projects Array.
$projectName = [];
foreach($projects as $project){
    $projectName[] = [
        'project name' => $project,
    ];
}

Then I created a function merge_keys to combine the values from two arrays:

#function for merging values of Project and Developers Array.
function merge_keys($array, $key, $array2, $key2){
    for ($i=0; $i < sizeof($array); $i  ){
        $array[$i][$key] = $array[$i][$key]; 
        $array[$i][$key2] = $array2[$i][$key2]; 
    }
    return $array;
}

#passing project and developer keys parameters to merge_keys function.
$merged_array = merge_keys($devNames, 'name', $projectName, 'project name');


#displaying the merged array.
echo '<pre>';
print_r($merged_array);

CodePudding user response:

$result = array_map(function($developer, $project) {
    return ['name'=>$developer, 'project name'=>$project];
}, $developers, $projects);

Result:

Array
(
    [0] => Array
        (
            [name] => Ammar
            [project name] => Sail Caribe
        )

    [1] => Array
        (
            [name] => Waqar
            [project name] => Hacker Craft
        )

    [2] => Array
        (
            [name] => Sheraz
            [project name] => Jackson Demolition
        )

    [3] => Array
        (
            [name] => Suleman
            [project name] => Carribean Yacht Charter
        )

)

Demo: https://paiza.io/projects/AuoYfhBYEuoYbMO2Vl2xBw

CodePudding user response:

If you just want to put those two arrays in a new array with name and project name (based in the array indexes), one single foreach should do it:

$developers = [
    'Ammar',
    'Waqar',
    'Sheraz',
    'Suleman',
];

$projects = [
    'Sail Caribe',
    'Hacker Craft',
    'Jackson Demolition',
    'Carribean Yacht Charter',
];

$result = [];
foreach ($developers as $index => $developer) {
    $result[] = [
        'name' => $developer,
        'project name' => $projects[$index],  
    ];
}

The $result array will now look like:

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Ammar"
    ["project name"]=>
    string(11) "Sail Caribe"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(5) "Waqar"
    ["project name"]=>
    string(12) "Hacker Craft"
  }
  ...

Here's a demo: https://3v4l.org/o9Sd8

  • Related