Combining 2 Arrays when value is matched
So I have these arrays that is fetched from 2 different database, I would like to combine them in one array when the ['item_name'] and ['name'] is matched then getting the ['id'] from the Array2
I tried doing the in_array but since it's multi dimensional, I can't get the right output I want, I tried foreach also but I can't also get the right output or maybe I'm doing it wrong, I'm running out of idea how I could do the output I wanted.
Example Array1:
Array
(
[0] => Array
(
[item_id] => 1
[item_name] => Bag
[Color] => Purple
)
[1] => Array
(
[item_id] => 2
[item_name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[item_id] => 3
[item_name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[item_id] => 4
[item_name] => Shirt
[Color] => Red
)
)
Example Array2:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
)
[1] => Array
(
[id] => 22
[name] => Pencil
)
[2] => Array
(
[id] => 33
[name] => Tumbler
)
[3] => Array
(
[id] => 44
[name] => Shirt
)
[4] => Array
(
[id] => 55
[name] => Paper
)
[5] => Array
(
[id] => 66
[name] => Chair
)
[6] => Array
(
[id] => 4
[name] => Notebook
)
)
So my expected output would be:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
[Color] => Purple
)
[1] => Array
(
[id] => 22
[name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[id] => 33
[name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[id] => 44
[name] => Shirt
[Color] => Red
)
)
php html arrays
add a comment |
So I have these arrays that is fetched from 2 different database, I would like to combine them in one array when the ['item_name'] and ['name'] is matched then getting the ['id'] from the Array2
I tried doing the in_array but since it's multi dimensional, I can't get the right output I want, I tried foreach also but I can't also get the right output or maybe I'm doing it wrong, I'm running out of idea how I could do the output I wanted.
Example Array1:
Array
(
[0] => Array
(
[item_id] => 1
[item_name] => Bag
[Color] => Purple
)
[1] => Array
(
[item_id] => 2
[item_name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[item_id] => 3
[item_name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[item_id] => 4
[item_name] => Shirt
[Color] => Red
)
)
Example Array2:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
)
[1] => Array
(
[id] => 22
[name] => Pencil
)
[2] => Array
(
[id] => 33
[name] => Tumbler
)
[3] => Array
(
[id] => 44
[name] => Shirt
)
[4] => Array
(
[id] => 55
[name] => Paper
)
[5] => Array
(
[id] => 66
[name] => Chair
)
[6] => Array
(
[id] => 4
[name] => Notebook
)
)
So my expected output would be:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
[Color] => Purple
)
[1] => Array
(
[id] => 22
[name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[id] => 33
[name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[id] => 44
[name] => Shirt
[Color] => Red
)
)
php html arrays
1
Have you tried anything?
– Web Artisan
Nov 21 '18 at 6:35
Yes, I tried in_array, I also tried the recursive array I found but I still can't get it, currently I am using the array_intersect() but I can only use it to only get the name, not the other details like the id and the color.
– kairi
Nov 21 '18 at 6:38
1
Please show us what you've tried and what result it produces. We're glad to help you with your existing code, but we won't write it all for you. Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
Nov 21 '18 at 6:43
$filtered = array_intersect($array1, $array2); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } $compare = array($filtered, $array1); foreach($array2 as $val){ if (in_array_r($val['name'], $compare)){ echo "<pre>"; print_r($val['name']); echo "</pre>"; } }
– kairi
Nov 21 '18 at 6:51
Please update your question to include the code. Code in comments are pretty unreadable.
– Magnus Eriksson
Nov 21 '18 at 6:53
add a comment |
So I have these arrays that is fetched from 2 different database, I would like to combine them in one array when the ['item_name'] and ['name'] is matched then getting the ['id'] from the Array2
I tried doing the in_array but since it's multi dimensional, I can't get the right output I want, I tried foreach also but I can't also get the right output or maybe I'm doing it wrong, I'm running out of idea how I could do the output I wanted.
Example Array1:
Array
(
[0] => Array
(
[item_id] => 1
[item_name] => Bag
[Color] => Purple
)
[1] => Array
(
[item_id] => 2
[item_name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[item_id] => 3
[item_name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[item_id] => 4
[item_name] => Shirt
[Color] => Red
)
)
Example Array2:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
)
[1] => Array
(
[id] => 22
[name] => Pencil
)
[2] => Array
(
[id] => 33
[name] => Tumbler
)
[3] => Array
(
[id] => 44
[name] => Shirt
)
[4] => Array
(
[id] => 55
[name] => Paper
)
[5] => Array
(
[id] => 66
[name] => Chair
)
[6] => Array
(
[id] => 4
[name] => Notebook
)
)
So my expected output would be:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
[Color] => Purple
)
[1] => Array
(
[id] => 22
[name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[id] => 33
[name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[id] => 44
[name] => Shirt
[Color] => Red
)
)
php html arrays
So I have these arrays that is fetched from 2 different database, I would like to combine them in one array when the ['item_name'] and ['name'] is matched then getting the ['id'] from the Array2
I tried doing the in_array but since it's multi dimensional, I can't get the right output I want, I tried foreach also but I can't also get the right output or maybe I'm doing it wrong, I'm running out of idea how I could do the output I wanted.
Example Array1:
Array
(
[0] => Array
(
[item_id] => 1
[item_name] => Bag
[Color] => Purple
)
[1] => Array
(
[item_id] => 2
[item_name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[item_id] => 3
[item_name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[item_id] => 4
[item_name] => Shirt
[Color] => Red
)
)
Example Array2:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
)
[1] => Array
(
[id] => 22
[name] => Pencil
)
[2] => Array
(
[id] => 33
[name] => Tumbler
)
[3] => Array
(
[id] => 44
[name] => Shirt
)
[4] => Array
(
[id] => 55
[name] => Paper
)
[5] => Array
(
[id] => 66
[name] => Chair
)
[6] => Array
(
[id] => 4
[name] => Notebook
)
)
So my expected output would be:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
[Color] => Purple
)
[1] => Array
(
[id] => 22
[name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[id] => 33
[name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[id] => 44
[name] => Shirt
[Color] => Red
)
)
php html arrays
php html arrays
asked Nov 21 '18 at 6:34
kairikairi
1129
1129
1
Have you tried anything?
– Web Artisan
Nov 21 '18 at 6:35
Yes, I tried in_array, I also tried the recursive array I found but I still can't get it, currently I am using the array_intersect() but I can only use it to only get the name, not the other details like the id and the color.
– kairi
Nov 21 '18 at 6:38
1
Please show us what you've tried and what result it produces. We're glad to help you with your existing code, but we won't write it all for you. Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
Nov 21 '18 at 6:43
$filtered = array_intersect($array1, $array2); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } $compare = array($filtered, $array1); foreach($array2 as $val){ if (in_array_r($val['name'], $compare)){ echo "<pre>"; print_r($val['name']); echo "</pre>"; } }
– kairi
Nov 21 '18 at 6:51
Please update your question to include the code. Code in comments are pretty unreadable.
– Magnus Eriksson
Nov 21 '18 at 6:53
add a comment |
1
Have you tried anything?
– Web Artisan
Nov 21 '18 at 6:35
Yes, I tried in_array, I also tried the recursive array I found but I still can't get it, currently I am using the array_intersect() but I can only use it to only get the name, not the other details like the id and the color.
– kairi
Nov 21 '18 at 6:38
1
Please show us what you've tried and what result it produces. We're glad to help you with your existing code, but we won't write it all for you. Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
Nov 21 '18 at 6:43
$filtered = array_intersect($array1, $array2); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } $compare = array($filtered, $array1); foreach($array2 as $val){ if (in_array_r($val['name'], $compare)){ echo "<pre>"; print_r($val['name']); echo "</pre>"; } }
– kairi
Nov 21 '18 at 6:51
Please update your question to include the code. Code in comments are pretty unreadable.
– Magnus Eriksson
Nov 21 '18 at 6:53
1
1
Have you tried anything?
– Web Artisan
Nov 21 '18 at 6:35
Have you tried anything?
– Web Artisan
Nov 21 '18 at 6:35
Yes, I tried in_array, I also tried the recursive array I found but I still can't get it, currently I am using the array_intersect() but I can only use it to only get the name, not the other details like the id and the color.
– kairi
Nov 21 '18 at 6:38
Yes, I tried in_array, I also tried the recursive array I found but I still can't get it, currently I am using the array_intersect() but I can only use it to only get the name, not the other details like the id and the color.
– kairi
Nov 21 '18 at 6:38
1
1
Please show us what you've tried and what result it produces. We're glad to help you with your existing code, but we won't write it all for you. Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
Nov 21 '18 at 6:43
Please show us what you've tried and what result it produces. We're glad to help you with your existing code, but we won't write it all for you. Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
Nov 21 '18 at 6:43
$filtered = array_intersect($array1, $array2); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } $compare = array($filtered, $array1); foreach($array2 as $val){ if (in_array_r($val['name'], $compare)){ echo "<pre>"; print_r($val['name']); echo "</pre>"; } }
– kairi
Nov 21 '18 at 6:51
$filtered = array_intersect($array1, $array2); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } $compare = array($filtered, $array1); foreach($array2 as $val){ if (in_array_r($val['name'], $compare)){ echo "<pre>"; print_r($val['name']); echo "</pre>"; } }
– kairi
Nov 21 '18 at 6:51
Please update your question to include the code. Code in comments are pretty unreadable.
– Magnus Eriksson
Nov 21 '18 at 6:53
Please update your question to include the code. Code in comments are pretty unreadable.
– Magnus Eriksson
Nov 21 '18 at 6:53
add a comment |
3 Answers
3
active
oldest
votes
You can create a map with the names and the ID from the second array and then loop to modify the first one.
Consider the following code:
$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);
$map = ; // here keys are names and value are the id
foreach($arr2 as $elem)
$map[$elem["item_name"]] = $elem["item_id"];
$ans = ;
foreach ($arr1 as $elem) {
if (array_key_exists($elem["item_name"], $map)) {
$elem["item_id"] = $map[$elem["item_name"]];
$ans = $elem;
}
}
echo print_r($ans);
In this way you achieve complexity of O(n)
instead of O(n^2)
if trying nested for-loop
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
add a comment |
Try by using foreach loop.
Check if in foreach loop if any index value match with another array then create a new array with all those values.
You can use counter here to get the value of array inside loop.
3
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
add a comment |
Been trying to simplify my code earlier and I think I got it, thanks to anju and David Winder, if anyone interested how I did it, this is how:
index = 0;
foreach ($array1 as $val){
foreach ($array2 as $val2){
if ($val['item_id'] == $val2['id']){
$filtered[$index]['id'] = $val2['id'];
$filtered[$index]['name'] = $val2['name'];
$filtered[$index]['color'] = $val['color'];
$index++;
}
}
}
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
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%2f53406441%2fcombining-2-arrays-when-value-is-matched%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create a map with the names and the ID from the second array and then loop to modify the first one.
Consider the following code:
$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);
$map = ; // here keys are names and value are the id
foreach($arr2 as $elem)
$map[$elem["item_name"]] = $elem["item_id"];
$ans = ;
foreach ($arr1 as $elem) {
if (array_key_exists($elem["item_name"], $map)) {
$elem["item_id"] = $map[$elem["item_name"]];
$ans = $elem;
}
}
echo print_r($ans);
In this way you achieve complexity of O(n)
instead of O(n^2)
if trying nested for-loop
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
add a comment |
You can create a map with the names and the ID from the second array and then loop to modify the first one.
Consider the following code:
$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);
$map = ; // here keys are names and value are the id
foreach($arr2 as $elem)
$map[$elem["item_name"]] = $elem["item_id"];
$ans = ;
foreach ($arr1 as $elem) {
if (array_key_exists($elem["item_name"], $map)) {
$elem["item_id"] = $map[$elem["item_name"]];
$ans = $elem;
}
}
echo print_r($ans);
In this way you achieve complexity of O(n)
instead of O(n^2)
if trying nested for-loop
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
add a comment |
You can create a map with the names and the ID from the second array and then loop to modify the first one.
Consider the following code:
$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);
$map = ; // here keys are names and value are the id
foreach($arr2 as $elem)
$map[$elem["item_name"]] = $elem["item_id"];
$ans = ;
foreach ($arr1 as $elem) {
if (array_key_exists($elem["item_name"], $map)) {
$elem["item_id"] = $map[$elem["item_name"]];
$ans = $elem;
}
}
echo print_r($ans);
In this way you achieve complexity of O(n)
instead of O(n^2)
if trying nested for-loop
You can create a map with the names and the ID from the second array and then loop to modify the first one.
Consider the following code:
$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);
$map = ; // here keys are names and value are the id
foreach($arr2 as $elem)
$map[$elem["item_name"]] = $elem["item_id"];
$ans = ;
foreach ($arr1 as $elem) {
if (array_key_exists($elem["item_name"], $map)) {
$elem["item_id"] = $map[$elem["item_name"]];
$ans = $elem;
}
}
echo print_r($ans);
In this way you achieve complexity of O(n)
instead of O(n^2)
if trying nested for-loop
edited Nov 21 '18 at 6:51
answered Nov 21 '18 at 6:46
dWinderdWinder
5,76631130
5,76631130
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
add a comment |
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
I saw this a little bit late, been trying to simplify my code and made it work my code seems similar to yours.
– kairi
Nov 21 '18 at 8:24
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
@kairi - if you can improve my answer as you got better solution please do. If it fine and helped you please mark it as accepted
– dWinder
Nov 21 '18 at 8:26
add a comment |
Try by using foreach loop.
Check if in foreach loop if any index value match with another array then create a new array with all those values.
You can use counter here to get the value of array inside loop.
3
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
add a comment |
Try by using foreach loop.
Check if in foreach loop if any index value match with another array then create a new array with all those values.
You can use counter here to get the value of array inside loop.
3
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
add a comment |
Try by using foreach loop.
Check if in foreach loop if any index value match with another array then create a new array with all those values.
You can use counter here to get the value of array inside loop.
Try by using foreach loop.
Check if in foreach loop if any index value match with another array then create a new array with all those values.
You can use counter here to get the value of array inside loop.
answered Nov 21 '18 at 6:37
anju dhimananju dhiman
11
11
3
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
add a comment |
3
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
3
3
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
Instead of explaining what the code should look like, you should write a proper example. Otherwise, it's more of a comment than an answer.
– Magnus Eriksson
Nov 21 '18 at 6:42
add a comment |
Been trying to simplify my code earlier and I think I got it, thanks to anju and David Winder, if anyone interested how I did it, this is how:
index = 0;
foreach ($array1 as $val){
foreach ($array2 as $val2){
if ($val['item_id'] == $val2['id']){
$filtered[$index]['id'] = $val2['id'];
$filtered[$index]['name'] = $val2['name'];
$filtered[$index]['color'] = $val['color'];
$index++;
}
}
}
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
add a comment |
Been trying to simplify my code earlier and I think I got it, thanks to anju and David Winder, if anyone interested how I did it, this is how:
index = 0;
foreach ($array1 as $val){
foreach ($array2 as $val2){
if ($val['item_id'] == $val2['id']){
$filtered[$index]['id'] = $val2['id'];
$filtered[$index]['name'] = $val2['name'];
$filtered[$index]['color'] = $val['color'];
$index++;
}
}
}
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
add a comment |
Been trying to simplify my code earlier and I think I got it, thanks to anju and David Winder, if anyone interested how I did it, this is how:
index = 0;
foreach ($array1 as $val){
foreach ($array2 as $val2){
if ($val['item_id'] == $val2['id']){
$filtered[$index]['id'] = $val2['id'];
$filtered[$index]['name'] = $val2['name'];
$filtered[$index]['color'] = $val['color'];
$index++;
}
}
}
Been trying to simplify my code earlier and I think I got it, thanks to anju and David Winder, if anyone interested how I did it, this is how:
index = 0;
foreach ($array1 as $val){
foreach ($array2 as $val2){
if ($val['item_id'] == $val2['id']){
$filtered[$index]['id'] = $val2['id'];
$filtered[$index]['name'] = $val2['name'];
$filtered[$index]['color'] = $val['color'];
$index++;
}
}
}
answered Nov 21 '18 at 8:26
kairikairi
1129
1129
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
add a comment |
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
Just pointing out that this is O(n^2). Better solution is possible with O(n) - See my post.
– dWinder
Nov 21 '18 at 8:30
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%2f53406441%2fcombining-2-arrays-when-value-is-matched%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
1
Have you tried anything?
– Web Artisan
Nov 21 '18 at 6:35
Yes, I tried in_array, I also tried the recursive array I found but I still can't get it, currently I am using the array_intersect() but I can only use it to only get the name, not the other details like the id and the color.
– kairi
Nov 21 '18 at 6:38
1
Please show us what you've tried and what result it produces. We're glad to help you with your existing code, but we won't write it all for you. Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
Nov 21 '18 at 6:43
$filtered = array_intersect($array1, $array2); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } $compare = array($filtered, $array1); foreach($array2 as $val){ if (in_array_r($val['name'], $compare)){ echo "<pre>"; print_r($val['name']); echo "</pre>"; } }
– kairi
Nov 21 '18 at 6:51
Please update your question to include the code. Code in comments are pretty unreadable.
– Magnus Eriksson
Nov 21 '18 at 6:53