Reputation: 169
I'm trying to teach myself how multidimensional arrays work and how I can compare and manipulate them in php I have created two arrays with the same scheme but with one different value in each.
first array
$datastuff1 = array( array( Ttitle => "rose",
Price => 1.25,
Number => 15
),
array( Ttitle => "daisy",
Price => 0.75,
Number => 25
),
array( Ttitle => "lilly",
Price => 1.75,
Number => 3
),
array( Ttitle => "orchid",
Price => 1.15,
Number => 7
)
);
second array
$datastuff2 = array( array( Title => "rose",
Price => 1.25,
Number => 15
),
array( Title => "daisy",
Price => 0.75,
Number => 25
),
array( Title => "nettle",
Price => 2.75,
Number => 33
),
array( Title => "orchid",
Price => 1.15,
Number => 7
)
);
I now want to loop through both arrays and foreach
item that matches (using the title as a key) in both arrays add to a new matching array and for each item that doesn't match in both arrays add to my not matching array
heres my code
$matchingarray = array();
$notmatchingarray = array();
foreach($datastuff1 as $data1){
foreach($datastuff2 as $data2){
if($data2['Title']== $data1['Ttitle'])
{
$matchingarray[] = $data1;
}
else {
$notmatchingarray[] = $data1;
}
}
}
but when I output the contents of the arrays using
echo "<pre>";
print_r($notmatchingarray);
echo "</pre>";
I get the output
Array
(
[0] => Array
(
[Ttitle] => rose
[Price] => 1.25
[Number] => 15
)
[1] => Array
(
[Ttitle] => rose
[Price] => 1.25
[Number] => 15
)
[2] => Array
(
[Ttitle] => rose
[Price] => 1.25
[Number] => 15
)
[3] => Array
(
[Ttitle] => daisy
[Price] => 0.75
[Number] => 25
)
[4] => Array
(
[Ttitle] => daisy
[Price] => 0.75
[Number] => 25
)
[5] => Array
(
[Ttitle] => daisy
[Price] => 0.75
[Number] => 25
)
[6] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[7] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[8] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[9] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[10] => Array
(
[Ttitle] => orchid
[Price] => 1.15
[Number] => 7
)
[11] => Array
(
[Ttitle] => orchid
[Price] => 1.15
[Number] => 7
)
[12] => Array
(
[Ttitle] => orchid
[Price] => 1.15
[Number] => 7
)
)
so to me it seems as though its looping round three times (the amount of items that match) and each time putting the matching items in the array.
What I want is all the items that DON'T match (using the Title as a key) in the non-matching array and the ones that DO in the matching array. I'm missing something painstakingly obvious I suppose.
Any help would be grand regard mike
Upvotes: 1
Views: 66
Reputation: 78994
I couldn't simply copy/paste your array definitions so I didn't test, but you need to check for equality and if found then break
out of the inner loop. Also, after the inner loop, check if it was added to $matchingarray
and if not, add to $notmatchingarray
:
foreach($datastuff1 as $key => $data1){
foreach($datastuff2 as $data2){
//match add to $matchingarray
if($data2['Title'] == $data1['Ttitle']) {
$matchingarray[$key] = $data1; //use $key so we can check later
break; //we have a match so why keep looping?
}
}
//if no match add to $notmatchingarray
if(!isset($matchingarray[$key])) { //we used $key so we can check for it
$notmatchingarray[$key] = $data1; //don't need $key here but oh well
}
}
Alternate way that may be easier to follow:
foreach($datastuff1 as $key => $data1){
$match = false; //no match
foreach($datastuff2 as $data2) {
//match add to $matchingarray
if($data2['Title'] == $data1['Ttitle']) {
$matchingarray[] = $data1;
$match = true; //match
break; //we have a match so why keep looping?
}
}
//if no match add to $notmatchingarray
if(!$match) {
$notmatchingarray[] = $data1;
}
}
Upvotes: 1