PHP - How to remove duplicate array elements when print from inner loop based on the number of elemenets












0















$sports = array
(
array('id'=> 1, 'Name'=>'Soccer','Popularity'=>'High'),
array('id' => 2,'Name'=>'Baseball','Popularity'=>'Low', 'IsTrending' => 'Yes')

);

$sport_history = array(
array('OwnerId' => 1, 'View'=>'Worldwide'),
array('OwnerId'=> 1, 'View'=>'Usa'),
array('OwnerId'=> 1, 'View'=>'Europe'),
);

foreach($sports as $masterKey => $sport){

if($sport['IsTrending'] == 'Yes'){

var_dump($sport['Name']);//Here only prints onces

foreach ($sport_history as $key=>$history){

var_dump($sport['Name']);//Here prints several times but I would like to be printed here and based on the number of elements in the first array and not the second.

echo '<pre>' . print_r($sport_history[$key],1) . '</pre>';

}
}
}


I'm having an issue where the first array value is output multiple times when placing inside of second inner loop but when placed on the outter loop it prints only once. I want to be able to output the values in the inner loop based on the number of elements on array and not the outter loop. I was thinking in using "if not inarray()" or "array_uniquie()" but not sure.



UPDATE:
Desire output:
Instead of this



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Usa
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Europe
)


I want this within the inner loop



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
Array
(
[OwnerId] => 1
[View] => Usa
)
Array
(
[OwnerId] => 1
[View] => Europe
)









share|improve this question

























  • It's not clear what exactly you are trying to achieve. Why not just output the sport name in the outer loop instead of the inner one?

    – Nick
    Nov 24 '18 at 23:08











  • Please, be more clear. Write the desired output.

    – Stefano Coletta
    Nov 24 '18 at 23:09











  • @Nick because I want to do an insert into a database table that requires a sport id .

    – Alex Garcia
    Nov 24 '18 at 23:42











  • Perhaps you could show that code?

    – Nick
    Nov 24 '18 at 23:44











  • @Nick I updated my code.

    – Alex Garcia
    Nov 24 '18 at 23:47
















0















$sports = array
(
array('id'=> 1, 'Name'=>'Soccer','Popularity'=>'High'),
array('id' => 2,'Name'=>'Baseball','Popularity'=>'Low', 'IsTrending' => 'Yes')

);

$sport_history = array(
array('OwnerId' => 1, 'View'=>'Worldwide'),
array('OwnerId'=> 1, 'View'=>'Usa'),
array('OwnerId'=> 1, 'View'=>'Europe'),
);

foreach($sports as $masterKey => $sport){

if($sport['IsTrending'] == 'Yes'){

var_dump($sport['Name']);//Here only prints onces

foreach ($sport_history as $key=>$history){

var_dump($sport['Name']);//Here prints several times but I would like to be printed here and based on the number of elements in the first array and not the second.

echo '<pre>' . print_r($sport_history[$key],1) . '</pre>';

}
}
}


I'm having an issue where the first array value is output multiple times when placing inside of second inner loop but when placed on the outter loop it prints only once. I want to be able to output the values in the inner loop based on the number of elements on array and not the outter loop. I was thinking in using "if not inarray()" or "array_uniquie()" but not sure.



UPDATE:
Desire output:
Instead of this



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Usa
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Europe
)


I want this within the inner loop



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
Array
(
[OwnerId] => 1
[View] => Usa
)
Array
(
[OwnerId] => 1
[View] => Europe
)









share|improve this question

























  • It's not clear what exactly you are trying to achieve. Why not just output the sport name in the outer loop instead of the inner one?

    – Nick
    Nov 24 '18 at 23:08











  • Please, be more clear. Write the desired output.

    – Stefano Coletta
    Nov 24 '18 at 23:09











  • @Nick because I want to do an insert into a database table that requires a sport id .

    – Alex Garcia
    Nov 24 '18 at 23:42











  • Perhaps you could show that code?

    – Nick
    Nov 24 '18 at 23:44











  • @Nick I updated my code.

    – Alex Garcia
    Nov 24 '18 at 23:47














0












0








0


1






$sports = array
(
array('id'=> 1, 'Name'=>'Soccer','Popularity'=>'High'),
array('id' => 2,'Name'=>'Baseball','Popularity'=>'Low', 'IsTrending' => 'Yes')

);

$sport_history = array(
array('OwnerId' => 1, 'View'=>'Worldwide'),
array('OwnerId'=> 1, 'View'=>'Usa'),
array('OwnerId'=> 1, 'View'=>'Europe'),
);

foreach($sports as $masterKey => $sport){

if($sport['IsTrending'] == 'Yes'){

var_dump($sport['Name']);//Here only prints onces

foreach ($sport_history as $key=>$history){

var_dump($sport['Name']);//Here prints several times but I would like to be printed here and based on the number of elements in the first array and not the second.

echo '<pre>' . print_r($sport_history[$key],1) . '</pre>';

}
}
}


I'm having an issue where the first array value is output multiple times when placing inside of second inner loop but when placed on the outter loop it prints only once. I want to be able to output the values in the inner loop based on the number of elements on array and not the outter loop. I was thinking in using "if not inarray()" or "array_uniquie()" but not sure.



UPDATE:
Desire output:
Instead of this



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Usa
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Europe
)


I want this within the inner loop



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
Array
(
[OwnerId] => 1
[View] => Usa
)
Array
(
[OwnerId] => 1
[View] => Europe
)









share|improve this question
















$sports = array
(
array('id'=> 1, 'Name'=>'Soccer','Popularity'=>'High'),
array('id' => 2,'Name'=>'Baseball','Popularity'=>'Low', 'IsTrending' => 'Yes')

);

$sport_history = array(
array('OwnerId' => 1, 'View'=>'Worldwide'),
array('OwnerId'=> 1, 'View'=>'Usa'),
array('OwnerId'=> 1, 'View'=>'Europe'),
);

foreach($sports as $masterKey => $sport){

if($sport['IsTrending'] == 'Yes'){

var_dump($sport['Name']);//Here only prints onces

foreach ($sport_history as $key=>$history){

var_dump($sport['Name']);//Here prints several times but I would like to be printed here and based on the number of elements in the first array and not the second.

echo '<pre>' . print_r($sport_history[$key],1) . '</pre>';

}
}
}


I'm having an issue where the first array value is output multiple times when placing inside of second inner loop but when placed on the outter loop it prints only once. I want to be able to output the values in the inner loop based on the number of elements on array and not the outter loop. I was thinking in using "if not inarray()" or "array_uniquie()" but not sure.



UPDATE:
Desire output:
Instead of this



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Usa
)
string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Europe
)


I want this within the inner loop



string(8) "Baseball"
Array
(
[OwnerId] => 1
[View] => Worldwide
)
Array
(
[OwnerId] => 1
[View] => Usa
)
Array
(
[OwnerId] => 1
[View] => Europe
)






php loops multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 23:46







Alex Garcia

















asked Nov 24 '18 at 23:02









Alex GarciaAlex Garcia

177




177













  • It's not clear what exactly you are trying to achieve. Why not just output the sport name in the outer loop instead of the inner one?

    – Nick
    Nov 24 '18 at 23:08











  • Please, be more clear. Write the desired output.

    – Stefano Coletta
    Nov 24 '18 at 23:09











  • @Nick because I want to do an insert into a database table that requires a sport id .

    – Alex Garcia
    Nov 24 '18 at 23:42











  • Perhaps you could show that code?

    – Nick
    Nov 24 '18 at 23:44











  • @Nick I updated my code.

    – Alex Garcia
    Nov 24 '18 at 23:47



















  • It's not clear what exactly you are trying to achieve. Why not just output the sport name in the outer loop instead of the inner one?

    – Nick
    Nov 24 '18 at 23:08











  • Please, be more clear. Write the desired output.

    – Stefano Coletta
    Nov 24 '18 at 23:09











  • @Nick because I want to do an insert into a database table that requires a sport id .

    – Alex Garcia
    Nov 24 '18 at 23:42











  • Perhaps you could show that code?

    – Nick
    Nov 24 '18 at 23:44











  • @Nick I updated my code.

    – Alex Garcia
    Nov 24 '18 at 23:47

















It's not clear what exactly you are trying to achieve. Why not just output the sport name in the outer loop instead of the inner one?

– Nick
Nov 24 '18 at 23:08





It's not clear what exactly you are trying to achieve. Why not just output the sport name in the outer loop instead of the inner one?

– Nick
Nov 24 '18 at 23:08













Please, be more clear. Write the desired output.

– Stefano Coletta
Nov 24 '18 at 23:09





Please, be more clear. Write the desired output.

– Stefano Coletta
Nov 24 '18 at 23:09













@Nick because I want to do an insert into a database table that requires a sport id .

– Alex Garcia
Nov 24 '18 at 23:42





@Nick because I want to do an insert into a database table that requires a sport id .

– Alex Garcia
Nov 24 '18 at 23:42













Perhaps you could show that code?

– Nick
Nov 24 '18 at 23:44





Perhaps you could show that code?

– Nick
Nov 24 '18 at 23:44













@Nick I updated my code.

– Alex Garcia
Nov 24 '18 at 23:47





@Nick I updated my code.

– Alex Garcia
Nov 24 '18 at 23:47












1 Answer
1






active

oldest

votes


















1














You can get the output you want (since you want to perform the output in the inner loop) by just outputting for the first key in $sport_history. Based on your code, that will be 0 so change



var_dump($sport['Name']);


to



if ($key == 0) var_dump($sport['Name']);





share|improve this answer
























  • Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

    – Alex Garcia
    Nov 24 '18 at 23:54











  • Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

    – Nick
    Nov 24 '18 at 23:55











  • Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

    – Alex Garcia
    Nov 24 '18 at 23:57











  • Just tried it out and it works! Thank you Nick. Just what I needed.

    – Alex Garcia
    Nov 25 '18 at 0:00











  • Great. Glad I could help.

    – Nick
    Nov 25 '18 at 0:00











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%2f53463128%2fphp-how-to-remove-duplicate-array-elements-when-print-from-inner-loop-based-on%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can get the output you want (since you want to perform the output in the inner loop) by just outputting for the first key in $sport_history. Based on your code, that will be 0 so change



var_dump($sport['Name']);


to



if ($key == 0) var_dump($sport['Name']);





share|improve this answer
























  • Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

    – Alex Garcia
    Nov 24 '18 at 23:54











  • Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

    – Nick
    Nov 24 '18 at 23:55











  • Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

    – Alex Garcia
    Nov 24 '18 at 23:57











  • Just tried it out and it works! Thank you Nick. Just what I needed.

    – Alex Garcia
    Nov 25 '18 at 0:00











  • Great. Glad I could help.

    – Nick
    Nov 25 '18 at 0:00
















1














You can get the output you want (since you want to perform the output in the inner loop) by just outputting for the first key in $sport_history. Based on your code, that will be 0 so change



var_dump($sport['Name']);


to



if ($key == 0) var_dump($sport['Name']);





share|improve this answer
























  • Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

    – Alex Garcia
    Nov 24 '18 at 23:54











  • Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

    – Nick
    Nov 24 '18 at 23:55











  • Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

    – Alex Garcia
    Nov 24 '18 at 23:57











  • Just tried it out and it works! Thank you Nick. Just what I needed.

    – Alex Garcia
    Nov 25 '18 at 0:00











  • Great. Glad I could help.

    – Nick
    Nov 25 '18 at 0:00














1












1








1







You can get the output you want (since you want to perform the output in the inner loop) by just outputting for the first key in $sport_history. Based on your code, that will be 0 so change



var_dump($sport['Name']);


to



if ($key == 0) var_dump($sport['Name']);





share|improve this answer













You can get the output you want (since you want to perform the output in the inner loop) by just outputting for the first key in $sport_history. Based on your code, that will be 0 so change



var_dump($sport['Name']);


to



if ($key == 0) var_dump($sport['Name']);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 23:51









NickNick

34.5k132043




34.5k132043













  • Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

    – Alex Garcia
    Nov 24 '18 at 23:54











  • Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

    – Nick
    Nov 24 '18 at 23:55











  • Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

    – Alex Garcia
    Nov 24 '18 at 23:57











  • Just tried it out and it works! Thank you Nick. Just what I needed.

    – Alex Garcia
    Nov 25 '18 at 0:00











  • Great. Glad I could help.

    – Nick
    Nov 25 '18 at 0:00



















  • Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

    – Alex Garcia
    Nov 24 '18 at 23:54











  • Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

    – Nick
    Nov 24 '18 at 23:55











  • Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

    – Alex Garcia
    Nov 24 '18 at 23:57











  • Just tried it out and it works! Thank you Nick. Just what I needed.

    – Alex Garcia
    Nov 25 '18 at 0:00











  • Great. Glad I could help.

    – Nick
    Nov 25 '18 at 0:00

















Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

– Alex Garcia
Nov 24 '18 at 23:54





Ok thanks, but what about if there are multiple elements? $key == 0 refers to the first index matching 0 correct?

– Alex Garcia
Nov 24 '18 at 23:54













Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

– Nick
Nov 24 '18 at 23:55





Correct - but there will only be one index in $sport_history matching 0, which should be the first one.

– Nick
Nov 24 '18 at 23:55













Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

– Alex Garcia
Nov 24 '18 at 23:57





Ok sir thanks, I'll try now and if it works, I'll let you know shortly.

– Alex Garcia
Nov 24 '18 at 23:57













Just tried it out and it works! Thank you Nick. Just what I needed.

– Alex Garcia
Nov 25 '18 at 0:00





Just tried it out and it works! Thank you Nick. Just what I needed.

– Alex Garcia
Nov 25 '18 at 0:00













Great. Glad I could help.

– Nick
Nov 25 '18 at 0:00





Great. Glad I could help.

– Nick
Nov 25 '18 at 0:00




















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%2f53463128%2fphp-how-to-remove-duplicate-array-elements-when-print-from-inner-loop-based-on%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

Costa Masnaga

Fotorealismo

Sidney Franklin