How to create an associative array from two arrays?
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
add a comment |
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
What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?
– Progrock
yesterday
Actually, I have requiredExpected Output
type format in my project.
– Gufran Hasan
17 hours ago
add a comment |
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
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
php arrays
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 requiredExpected Output
type format in my project.
– Gufran Hasan
17 hours ago
add a comment |
What does the $finalData format buy you? It looks less manageable and obvious than the 'rows' style: $valuesArray?
– Progrock
yesterday
Actually, I have requiredExpected 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
add a comment |
4 Answers
4
active
oldest
votes
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
$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
add a comment |
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)
1
Don't just post code, explain what was wrong with the OP's code and how you fixed it.
– Barmar
yesterday
add a comment |
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.
It's awesome :)
– Gufran Hasan
yesterday
That's very nice.
– Nick
yesterday
add a comment |
<?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
)
)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
$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
add a comment |
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
$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
add a comment |
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
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
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
add a comment |
$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
add a comment |
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)
1
Don't just post code, explain what was wrong with the OP's code and how you fixed it.
– Barmar
yesterday
add a comment |
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)
1
Don't just post code, explain what was wrong with the OP's code and how you fixed it.
– Barmar
yesterday
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
It's awesome :)
– Gufran Hasan
yesterday
That's very nice.
– Nick
yesterday
add a comment |
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.
It's awesome :)
– Gufran Hasan
yesterday
That's very nice.
– Nick
yesterday
add a comment |
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.
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.
answered yesterday
Ilmari KaronenIlmari Karonen
37.8k669129
37.8k669129
It's awesome :)
– Gufran Hasan
yesterday
That's very nice.
– Nick
yesterday
add a comment |
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
add a comment |
<?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
)
)
add a comment |
<?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
)
)
add a comment |
<?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
)
)
<?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
)
)
answered yesterday
ProgrockProgrock
4,47111022
4,47111022
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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