How to create an associative array from two arrays?












9















I have two arrays one for keys and another for values. The values array is an array of array.



Keys array:



$keyArray = array("clientId","clientName","clientAdsress","clientPhone");


Values array:



$valuesArray = array(
"0"=>array("1001","aaaaa","ddddddd","22222222"),
"1"=>array("1002","bbbbbb","ddddddd","11111111"),
"2"=>array("1003","ccccc","ddddddd","33333333")
);


Expected Output:



$finalData = array(
"clientId"=>array("1001","1002","1003"),
"clientName"=>array("aaaaa","bbbbbb","ccccc"),
"clientAdsress"=>array("ddddddd","ddddddd","ddddddd"),
"clientPhone"=>array("22222222","11111111","33333333")
);


I have tried this code:



$finalData = array();
for($i=0;$i<count($keyArray);$i++){

for($j=0;$j<count($valuesArray);$j++){
$rowArray = $valuesArray[$j];
$finalData[$keyArray[$i]] = array($rowArray[$i]);
}
}

echo '<pre>';print_r($finalData);echo '</pre>';


Output:



Array
(
[clientId] => Array
(
[0] => 1003
)

[clientName] => Array
(
[0] => ccccc
)

[clientAdsress] => Array
(
[0] => ddddddd
)

[clientPhone] => Array
(
[0] => 33333333
)

)


I am getting the last values of arrays. How can I get my expected output?










share|improve this question

























  • What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?

    – Progrock
    yesterday











  • Actually, I have required Expected Output type format in my project.

    – Gufran Hasan
    17 hours ago
















9















I have two arrays one for keys and another for values. The values array is an array of array.



Keys array:



$keyArray = array("clientId","clientName","clientAdsress","clientPhone");


Values array:



$valuesArray = array(
"0"=>array("1001","aaaaa","ddddddd","22222222"),
"1"=>array("1002","bbbbbb","ddddddd","11111111"),
"2"=>array("1003","ccccc","ddddddd","33333333")
);


Expected Output:



$finalData = array(
"clientId"=>array("1001","1002","1003"),
"clientName"=>array("aaaaa","bbbbbb","ccccc"),
"clientAdsress"=>array("ddddddd","ddddddd","ddddddd"),
"clientPhone"=>array("22222222","11111111","33333333")
);


I have tried this code:



$finalData = array();
for($i=0;$i<count($keyArray);$i++){

for($j=0;$j<count($valuesArray);$j++){
$rowArray = $valuesArray[$j];
$finalData[$keyArray[$i]] = array($rowArray[$i]);
}
}

echo '<pre>';print_r($finalData);echo '</pre>';


Output:



Array
(
[clientId] => Array
(
[0] => 1003
)

[clientName] => Array
(
[0] => ccccc
)

[clientAdsress] => Array
(
[0] => ddddddd
)

[clientPhone] => Array
(
[0] => 33333333
)

)


I am getting the last values of arrays. How can I get my expected output?










share|improve this question

























  • What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?

    – Progrock
    yesterday











  • Actually, I have required Expected Output type format in my project.

    – Gufran Hasan
    17 hours ago














9












9








9


1






I have two arrays one for keys and another for values. The values array is an array of array.



Keys array:



$keyArray = array("clientId","clientName","clientAdsress","clientPhone");


Values array:



$valuesArray = array(
"0"=>array("1001","aaaaa","ddddddd","22222222"),
"1"=>array("1002","bbbbbb","ddddddd","11111111"),
"2"=>array("1003","ccccc","ddddddd","33333333")
);


Expected Output:



$finalData = array(
"clientId"=>array("1001","1002","1003"),
"clientName"=>array("aaaaa","bbbbbb","ccccc"),
"clientAdsress"=>array("ddddddd","ddddddd","ddddddd"),
"clientPhone"=>array("22222222","11111111","33333333")
);


I have tried this code:



$finalData = array();
for($i=0;$i<count($keyArray);$i++){

for($j=0;$j<count($valuesArray);$j++){
$rowArray = $valuesArray[$j];
$finalData[$keyArray[$i]] = array($rowArray[$i]);
}
}

echo '<pre>';print_r($finalData);echo '</pre>';


Output:



Array
(
[clientId] => Array
(
[0] => 1003
)

[clientName] => Array
(
[0] => ccccc
)

[clientAdsress] => Array
(
[0] => ddddddd
)

[clientPhone] => Array
(
[0] => 33333333
)

)


I am getting the last values of arrays. How can I get my expected output?










share|improve this question
















I have two arrays one for keys and another for values. The values array is an array of array.



Keys array:



$keyArray = array("clientId","clientName","clientAdsress","clientPhone");


Values array:



$valuesArray = array(
"0"=>array("1001","aaaaa","ddddddd","22222222"),
"1"=>array("1002","bbbbbb","ddddddd","11111111"),
"2"=>array("1003","ccccc","ddddddd","33333333")
);


Expected Output:



$finalData = array(
"clientId"=>array("1001","1002","1003"),
"clientName"=>array("aaaaa","bbbbbb","ccccc"),
"clientAdsress"=>array("ddddddd","ddddddd","ddddddd"),
"clientPhone"=>array("22222222","11111111","33333333")
);


I have tried this code:



$finalData = array();
for($i=0;$i<count($keyArray);$i++){

for($j=0;$j<count($valuesArray);$j++){
$rowArray = $valuesArray[$j];
$finalData[$keyArray[$i]] = array($rowArray[$i]);
}
}

echo '<pre>';print_r($finalData);echo '</pre>';


Output:



Array
(
[clientId] => Array
(
[0] => 1003
)

[clientName] => Array
(
[0] => ccccc
)

[clientAdsress] => Array
(
[0] => ddddddd
)

[clientPhone] => Array
(
[0] => 33333333
)

)


I am getting the last values of arrays. How can I get my expected output?







php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







Gufran Hasan

















asked yesterday









Gufran HasanGufran Hasan

3,65141527




3,65141527













  • What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?

    – Progrock
    yesterday











  • Actually, I have required Expected Output type format in my project.

    – Gufran Hasan
    17 hours ago



















  • What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?

    – Progrock
    yesterday











  • Actually, I have required Expected Output type format in my project.

    – Gufran Hasan
    17 hours ago

















What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?

– Progrock
yesterday





What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?

– Progrock
yesterday













Actually, I have required Expected Output type format in my project.

– Gufran Hasan
17 hours ago





Actually, I have required Expected Output type format in my project.

– Gufran Hasan
17 hours ago












4 Answers
4






active

oldest

votes


















7














One way to do it is with array_combine and array_column:



$finalData = array_combine($keyArray, array(array_column($valuesArray, 0),
array_column($valuesArray, 1),
array_column($valuesArray, 2),
array_column($valuesArray, 3)));
print_r($finalData);


Output:



Array ( 
[clientId] => Array ( [0] => 1001 [1] => 1002 [2] => 1003 )
[clientName] => Array ( [0] => aaaaa [1] => bbbbbb [2] => ccccc )
[clientAdsress] => Array ( [0] => ddddddd [1] => ddddddd [2] => ddddddd )
[clientPhone] => Array ( [0] => 22222222 [1] => 11111111 [2] => 33333333 )
)


Demo on 3v4l.org






share|improve this answer


























  • $valuesArray is a dynamic array. there may more or fewer row.

    – Gufran Hasan
    yesterday













  • @GufranHasan not a problem for this code: 3v4l.org/LpsCI

    – Nick
    yesterday











  • I have checked it, it's working perfectly. Thanks :)

    – Gufran Hasan
    yesterday






  • 1





    @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

    – dWinder
    yesterday





















10














A simple foreach solution:



https://3v4l.org/gbBad



<?php

$keyArray = array("clientId", "clientName", "clientAdsress", "clientPhone");
$valuesArray = array(
"0" => array("1001", "aaaaa", "ddddddd", "22222222"),
"1" => array("1002", "bbbbbb", "ddddddd", "11111111"),
"2" => array("1003", "ccccc", "ddddddd", "33333333")
);
$expected = array(
"clientId" => array("1001", "1002", "1003"),
"clientName" => array("aaaaa", "bbbbbb", "ccccc"),
"clientAdsress" => array("ddddddd", "ddddddd", "ddddddd"),
"clientPhone" => array("22222222", "11111111", "33333333")
);

$result = ;
foreach ($keyArray as $key => $keyName) {
foreach ($valuesArray as $value) {
$result[$keyName] = $value[$key];
}
}

var_dump($result === $expected);


Output for 7.1.25 - 7.3.2




bool(true)







share|improve this answer





















  • 1





    Don't just post code, explain what was wrong with the OP's code and how you fixed it.

    – Barmar
    yesterday



















5














Building on Nick's answer, a simpler solution that doesn't require hardcoding the number of keys would be:



$finalData = array_combine($keyArray, array_map(null, ...$valuesArray));


(Demo on 3v4l.org)



Basically, this first transposes the two-dimensional $valuesArray using array_map as described in this answer, changing the columns into rows and vice versa, and then combines the result with $keyArray using array_combine as in Nick's answer.






share|improve this answer
























  • It's awesome :)

    – Gufran Hasan
    yesterday











  • That's very nice.

    – Nick
    yesterday



















2














<?php
$keyArray = array(
"clientId","clientName","clientAdsress","clientPhone"
);

$valuesArray = array(
"0"=>array("1001","aaaaa","ddddddd","22222222"),
"1"=>array("1002","bbbbbb","ddddddd","11111111"),
"2"=>array("1003","ccccc","ddddddd","33333333")
);
foreach($keyArray as $keyIndex => $keyName)
$finalData[$keyName] = array_column($valuesArray, $keyIndex);

print_r($finalData);


Output:



Array
(
[clientId] => Array
(
[0] => 1001
[1] => 1002
[2] => 1003
)

[clientName] => Array
(
[0] => aaaaa
[1] => bbbbbb
[2] => ccccc
)

[clientAdsress] => Array
(
[0] => ddddddd
[1] => ddddddd
[2] => ddddddd
)

[clientPhone] => Array
(
[0] => 22222222
[1] => 11111111
[2] => 33333333
)

)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55022287%2fhow-to-create-an-associative-array-from-two-arrays%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    One way to do it is with array_combine and array_column:



    $finalData = array_combine($keyArray, array(array_column($valuesArray, 0),
    array_column($valuesArray, 1),
    array_column($valuesArray, 2),
    array_column($valuesArray, 3)));
    print_r($finalData);


    Output:



    Array ( 
    [clientId] => Array ( [0] => 1001 [1] => 1002 [2] => 1003 )
    [clientName] => Array ( [0] => aaaaa [1] => bbbbbb [2] => ccccc )
    [clientAdsress] => Array ( [0] => ddddddd [1] => ddddddd [2] => ddddddd )
    [clientPhone] => Array ( [0] => 22222222 [1] => 11111111 [2] => 33333333 )
    )


    Demo on 3v4l.org






    share|improve this answer


























    • $valuesArray is a dynamic array. there may more or fewer row.

      – Gufran Hasan
      yesterday













    • @GufranHasan not a problem for this code: 3v4l.org/LpsCI

      – Nick
      yesterday











    • I have checked it, it's working perfectly. Thanks :)

      – Gufran Hasan
      yesterday






    • 1





      @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

      – dWinder
      yesterday


















    7














    One way to do it is with array_combine and array_column:



    $finalData = array_combine($keyArray, array(array_column($valuesArray, 0),
    array_column($valuesArray, 1),
    array_column($valuesArray, 2),
    array_column($valuesArray, 3)));
    print_r($finalData);


    Output:



    Array ( 
    [clientId] => Array ( [0] => 1001 [1] => 1002 [2] => 1003 )
    [clientName] => Array ( [0] => aaaaa [1] => bbbbbb [2] => ccccc )
    [clientAdsress] => Array ( [0] => ddddddd [1] => ddddddd [2] => ddddddd )
    [clientPhone] => Array ( [0] => 22222222 [1] => 11111111 [2] => 33333333 )
    )


    Demo on 3v4l.org






    share|improve this answer


























    • $valuesArray is a dynamic array. there may more or fewer row.

      – Gufran Hasan
      yesterday













    • @GufranHasan not a problem for this code: 3v4l.org/LpsCI

      – Nick
      yesterday











    • I have checked it, it's working perfectly. Thanks :)

      – Gufran Hasan
      yesterday






    • 1





      @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

      – dWinder
      yesterday
















    7












    7








    7







    One way to do it is with array_combine and array_column:



    $finalData = array_combine($keyArray, array(array_column($valuesArray, 0),
    array_column($valuesArray, 1),
    array_column($valuesArray, 2),
    array_column($valuesArray, 3)));
    print_r($finalData);


    Output:



    Array ( 
    [clientId] => Array ( [0] => 1001 [1] => 1002 [2] => 1003 )
    [clientName] => Array ( [0] => aaaaa [1] => bbbbbb [2] => ccccc )
    [clientAdsress] => Array ( [0] => ddddddd [1] => ddddddd [2] => ddddddd )
    [clientPhone] => Array ( [0] => 22222222 [1] => 11111111 [2] => 33333333 )
    )


    Demo on 3v4l.org






    share|improve this answer















    One way to do it is with array_combine and array_column:



    $finalData = array_combine($keyArray, array(array_column($valuesArray, 0),
    array_column($valuesArray, 1),
    array_column($valuesArray, 2),
    array_column($valuesArray, 3)));
    print_r($finalData);


    Output:



    Array ( 
    [clientId] => Array ( [0] => 1001 [1] => 1002 [2] => 1003 )
    [clientName] => Array ( [0] => aaaaa [1] => bbbbbb [2] => ccccc )
    [clientAdsress] => Array ( [0] => ddddddd [1] => ddddddd [2] => ddddddd )
    [clientPhone] => Array ( [0] => 22222222 [1] => 11111111 [2] => 33333333 )
    )


    Demo on 3v4l.org







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    NickNick

    34.6k132043




    34.6k132043













    • $valuesArray is a dynamic array. there may more or fewer row.

      – Gufran Hasan
      yesterday













    • @GufranHasan not a problem for this code: 3v4l.org/LpsCI

      – Nick
      yesterday











    • I have checked it, it's working perfectly. Thanks :)

      – Gufran Hasan
      yesterday






    • 1





      @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

      – dWinder
      yesterday





















    • $valuesArray is a dynamic array. there may more or fewer row.

      – Gufran Hasan
      yesterday













    • @GufranHasan not a problem for this code: 3v4l.org/LpsCI

      – Nick
      yesterday











    • I have checked it, it's working perfectly. Thanks :)

      – Gufran Hasan
      yesterday






    • 1





      @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

      – dWinder
      yesterday



















    $valuesArray is a dynamic array. there may more or fewer row.

    – Gufran Hasan
    yesterday







    $valuesArray is a dynamic array. there may more or fewer row.

    – Gufran Hasan
    yesterday















    @GufranHasan not a problem for this code: 3v4l.org/LpsCI

    – Nick
    yesterday





    @GufranHasan not a problem for this code: 3v4l.org/LpsCI

    – Nick
    yesterday













    I have checked it, it's working perfectly. Thanks :)

    – Gufran Hasan
    yesterday





    I have checked it, it's working perfectly. Thanks :)

    – Gufran Hasan
    yesterday




    1




    1





    @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

    – dWinder
    yesterday







    @Nick - I also offer this more dynamic way: for ($i = 0; $i < count($keyArray); $i++) $arr = array_column($valuesArray, $i); $finalData = array_combine($keyArray, $arr); This way it will work whatever num of keys the OP has

    – dWinder
    yesterday















    10














    A simple foreach solution:



    https://3v4l.org/gbBad



    <?php

    $keyArray = array("clientId", "clientName", "clientAdsress", "clientPhone");
    $valuesArray = array(
    "0" => array("1001", "aaaaa", "ddddddd", "22222222"),
    "1" => array("1002", "bbbbbb", "ddddddd", "11111111"),
    "2" => array("1003", "ccccc", "ddddddd", "33333333")
    );
    $expected = array(
    "clientId" => array("1001", "1002", "1003"),
    "clientName" => array("aaaaa", "bbbbbb", "ccccc"),
    "clientAdsress" => array("ddddddd", "ddddddd", "ddddddd"),
    "clientPhone" => array("22222222", "11111111", "33333333")
    );

    $result = ;
    foreach ($keyArray as $key => $keyName) {
    foreach ($valuesArray as $value) {
    $result[$keyName] = $value[$key];
    }
    }

    var_dump($result === $expected);


    Output for 7.1.25 - 7.3.2




    bool(true)







    share|improve this answer





















    • 1





      Don't just post code, explain what was wrong with the OP's code and how you fixed it.

      – Barmar
      yesterday
















    10














    A simple foreach solution:



    https://3v4l.org/gbBad



    <?php

    $keyArray = array("clientId", "clientName", "clientAdsress", "clientPhone");
    $valuesArray = array(
    "0" => array("1001", "aaaaa", "ddddddd", "22222222"),
    "1" => array("1002", "bbbbbb", "ddddddd", "11111111"),
    "2" => array("1003", "ccccc", "ddddddd", "33333333")
    );
    $expected = array(
    "clientId" => array("1001", "1002", "1003"),
    "clientName" => array("aaaaa", "bbbbbb", "ccccc"),
    "clientAdsress" => array("ddddddd", "ddddddd", "ddddddd"),
    "clientPhone" => array("22222222", "11111111", "33333333")
    );

    $result = ;
    foreach ($keyArray as $key => $keyName) {
    foreach ($valuesArray as $value) {
    $result[$keyName] = $value[$key];
    }
    }

    var_dump($result === $expected);


    Output for 7.1.25 - 7.3.2




    bool(true)







    share|improve this answer





















    • 1





      Don't just post code, explain what was wrong with the OP's code and how you fixed it.

      – Barmar
      yesterday














    10












    10








    10







    A simple foreach solution:



    https://3v4l.org/gbBad



    <?php

    $keyArray = array("clientId", "clientName", "clientAdsress", "clientPhone");
    $valuesArray = array(
    "0" => array("1001", "aaaaa", "ddddddd", "22222222"),
    "1" => array("1002", "bbbbbb", "ddddddd", "11111111"),
    "2" => array("1003", "ccccc", "ddddddd", "33333333")
    );
    $expected = array(
    "clientId" => array("1001", "1002", "1003"),
    "clientName" => array("aaaaa", "bbbbbb", "ccccc"),
    "clientAdsress" => array("ddddddd", "ddddddd", "ddddddd"),
    "clientPhone" => array("22222222", "11111111", "33333333")
    );

    $result = ;
    foreach ($keyArray as $key => $keyName) {
    foreach ($valuesArray as $value) {
    $result[$keyName] = $value[$key];
    }
    }

    var_dump($result === $expected);


    Output for 7.1.25 - 7.3.2




    bool(true)







    share|improve this answer















    A simple foreach solution:



    https://3v4l.org/gbBad



    <?php

    $keyArray = array("clientId", "clientName", "clientAdsress", "clientPhone");
    $valuesArray = array(
    "0" => array("1001", "aaaaa", "ddddddd", "22222222"),
    "1" => array("1002", "bbbbbb", "ddddddd", "11111111"),
    "2" => array("1003", "ccccc", "ddddddd", "33333333")
    );
    $expected = array(
    "clientId" => array("1001", "1002", "1003"),
    "clientName" => array("aaaaa", "bbbbbb", "ccccc"),
    "clientAdsress" => array("ddddddd", "ddddddd", "ddddddd"),
    "clientPhone" => array("22222222", "11111111", "33333333")
    );

    $result = ;
    foreach ($keyArray as $key => $keyName) {
    foreach ($valuesArray as $value) {
    $result[$keyName] = $value[$key];
    }
    }

    var_dump($result === $expected);


    Output for 7.1.25 - 7.3.2




    bool(true)








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    XatenevXatenev

    5,71121235




    5,71121235








    • 1





      Don't just post code, explain what was wrong with the OP's code and how you fixed it.

      – Barmar
      yesterday














    • 1





      Don't just post code, explain what was wrong with the OP's code and how you fixed it.

      – Barmar
      yesterday








    1




    1





    Don't just post code, explain what was wrong with the OP's code and how you fixed it.

    – Barmar
    yesterday





    Don't just post code, explain what was wrong with the OP's code and how you fixed it.

    – Barmar
    yesterday











    5














    Building on Nick's answer, a simpler solution that doesn't require hardcoding the number of keys would be:



    $finalData = array_combine($keyArray, array_map(null, ...$valuesArray));


    (Demo on 3v4l.org)



    Basically, this first transposes the two-dimensional $valuesArray using array_map as described in this answer, changing the columns into rows and vice versa, and then combines the result with $keyArray using array_combine as in Nick's answer.






    share|improve this answer
























    • It's awesome :)

      – Gufran Hasan
      yesterday











    • That's very nice.

      – Nick
      yesterday
















    5














    Building on Nick's answer, a simpler solution that doesn't require hardcoding the number of keys would be:



    $finalData = array_combine($keyArray, array_map(null, ...$valuesArray));


    (Demo on 3v4l.org)



    Basically, this first transposes the two-dimensional $valuesArray using array_map as described in this answer, changing the columns into rows and vice versa, and then combines the result with $keyArray using array_combine as in Nick's answer.






    share|improve this answer
























    • It's awesome :)

      – Gufran Hasan
      yesterday











    • That's very nice.

      – Nick
      yesterday














    5












    5








    5







    Building on Nick's answer, a simpler solution that doesn't require hardcoding the number of keys would be:



    $finalData = array_combine($keyArray, array_map(null, ...$valuesArray));


    (Demo on 3v4l.org)



    Basically, this first transposes the two-dimensional $valuesArray using array_map as described in this answer, changing the columns into rows and vice versa, and then combines the result with $keyArray using array_combine as in Nick's answer.






    share|improve this answer













    Building on Nick's answer, a simpler solution that doesn't require hardcoding the number of keys would be:



    $finalData = array_combine($keyArray, array_map(null, ...$valuesArray));


    (Demo on 3v4l.org)



    Basically, this first transposes the two-dimensional $valuesArray using array_map as described in this answer, changing the columns into rows and vice versa, and then combines the result with $keyArray using array_combine as in Nick's answer.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    Ilmari KaronenIlmari Karonen

    37.8k669129




    37.8k669129













    • It's awesome :)

      – Gufran Hasan
      yesterday











    • That's very nice.

      – Nick
      yesterday



















    • It's awesome :)

      – Gufran Hasan
      yesterday











    • That's very nice.

      – Nick
      yesterday

















    It's awesome :)

    – Gufran Hasan
    yesterday





    It's awesome :)

    – Gufran Hasan
    yesterday













    That's very nice.

    – Nick
    yesterday





    That's very nice.

    – Nick
    yesterday











    2














    <?php
    $keyArray = array(
    "clientId","clientName","clientAdsress","clientPhone"
    );

    $valuesArray = array(
    "0"=>array("1001","aaaaa","ddddddd","22222222"),
    "1"=>array("1002","bbbbbb","ddddddd","11111111"),
    "2"=>array("1003","ccccc","ddddddd","33333333")
    );
    foreach($keyArray as $keyIndex => $keyName)
    $finalData[$keyName] = array_column($valuesArray, $keyIndex);

    print_r($finalData);


    Output:



    Array
    (
    [clientId] => Array
    (
    [0] => 1001
    [1] => 1002
    [2] => 1003
    )

    [clientName] => Array
    (
    [0] => aaaaa
    [1] => bbbbbb
    [2] => ccccc
    )

    [clientAdsress] => Array
    (
    [0] => ddddddd
    [1] => ddddddd
    [2] => ddddddd
    )

    [clientPhone] => Array
    (
    [0] => 22222222
    [1] => 11111111
    [2] => 33333333
    )

    )





    share|improve this answer




























      2














      <?php
      $keyArray = array(
      "clientId","clientName","clientAdsress","clientPhone"
      );

      $valuesArray = array(
      "0"=>array("1001","aaaaa","ddddddd","22222222"),
      "1"=>array("1002","bbbbbb","ddddddd","11111111"),
      "2"=>array("1003","ccccc","ddddddd","33333333")
      );
      foreach($keyArray as $keyIndex => $keyName)
      $finalData[$keyName] = array_column($valuesArray, $keyIndex);

      print_r($finalData);


      Output:



      Array
      (
      [clientId] => Array
      (
      [0] => 1001
      [1] => 1002
      [2] => 1003
      )

      [clientName] => Array
      (
      [0] => aaaaa
      [1] => bbbbbb
      [2] => ccccc
      )

      [clientAdsress] => Array
      (
      [0] => ddddddd
      [1] => ddddddd
      [2] => ddddddd
      )

      [clientPhone] => Array
      (
      [0] => 22222222
      [1] => 11111111
      [2] => 33333333
      )

      )





      share|improve this answer


























        2












        2








        2







        <?php
        $keyArray = array(
        "clientId","clientName","clientAdsress","clientPhone"
        );

        $valuesArray = array(
        "0"=>array("1001","aaaaa","ddddddd","22222222"),
        "1"=>array("1002","bbbbbb","ddddddd","11111111"),
        "2"=>array("1003","ccccc","ddddddd","33333333")
        );
        foreach($keyArray as $keyIndex => $keyName)
        $finalData[$keyName] = array_column($valuesArray, $keyIndex);

        print_r($finalData);


        Output:



        Array
        (
        [clientId] => Array
        (
        [0] => 1001
        [1] => 1002
        [2] => 1003
        )

        [clientName] => Array
        (
        [0] => aaaaa
        [1] => bbbbbb
        [2] => ccccc
        )

        [clientAdsress] => Array
        (
        [0] => ddddddd
        [1] => ddddddd
        [2] => ddddddd
        )

        [clientPhone] => Array
        (
        [0] => 22222222
        [1] => 11111111
        [2] => 33333333
        )

        )





        share|improve this answer













        <?php
        $keyArray = array(
        "clientId","clientName","clientAdsress","clientPhone"
        );

        $valuesArray = array(
        "0"=>array("1001","aaaaa","ddddddd","22222222"),
        "1"=>array("1002","bbbbbb","ddddddd","11111111"),
        "2"=>array("1003","ccccc","ddddddd","33333333")
        );
        foreach($keyArray as $keyIndex => $keyName)
        $finalData[$keyName] = array_column($valuesArray, $keyIndex);

        print_r($finalData);


        Output:



        Array
        (
        [clientId] => Array
        (
        [0] => 1001
        [1] => 1002
        [2] => 1003
        )

        [clientName] => Array
        (
        [0] => aaaaa
        [1] => bbbbbb
        [2] => ccccc
        )

        [clientAdsress] => Array
        (
        [0] => ddddddd
        [1] => ddddddd
        [2] => ddddddd
        )

        [clientPhone] => Array
        (
        [0] => 22222222
        [1] => 11111111
        [2] => 33333333
        )

        )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        ProgrockProgrock

        4,47111022




        4,47111022






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55022287%2fhow-to-create-an-associative-array-from-two-arrays%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana