Continuous array key in foreach loop outputs












0














I have two arrays (say, $arrayA and $arrayB) with numerous values in them.



I would like to fill a list of 8 divs with these values using a foreach loop which has has just 4 divs in it, with each div increasing an incremental key from their respective array.



The following should hopefully illustrate:



foreach ($values as $value) {
echo
'<div class="type-a">' . $arrayA[i] . '</div>
<div class="type-b">' . $arrayB[i] . '</div>
<div class="type-a">' . $arrayA[i+1] . '</div>
<div class="type-b">' . $arrayB[i+1] . '</div>';
}

<div class="type-a"> A1 </div>
<div class="type-b"> B1 </div>
<div class="type-a"> A2 </div>
<div class="type-b"> B2 </div>
<div class="type-a"> A3 </div>
<div class="type-b"> B3 </div>
<div class="type-a"> A4 </div>
<div class="type-b"> B4 </div>


Any suggestions would be great!










share|improve this question


















  • 1




    you can try to do loop based on len(array), then i:0 i+=2, then get the first and second array from a and b
    – Evinn
    Dec 27 '18 at 2:57
















0














I have two arrays (say, $arrayA and $arrayB) with numerous values in them.



I would like to fill a list of 8 divs with these values using a foreach loop which has has just 4 divs in it, with each div increasing an incremental key from their respective array.



The following should hopefully illustrate:



foreach ($values as $value) {
echo
'<div class="type-a">' . $arrayA[i] . '</div>
<div class="type-b">' . $arrayB[i] . '</div>
<div class="type-a">' . $arrayA[i+1] . '</div>
<div class="type-b">' . $arrayB[i+1] . '</div>';
}

<div class="type-a"> A1 </div>
<div class="type-b"> B1 </div>
<div class="type-a"> A2 </div>
<div class="type-b"> B2 </div>
<div class="type-a"> A3 </div>
<div class="type-b"> B3 </div>
<div class="type-a"> A4 </div>
<div class="type-b"> B4 </div>


Any suggestions would be great!










share|improve this question


















  • 1




    you can try to do loop based on len(array), then i:0 i+=2, then get the first and second array from a and b
    – Evinn
    Dec 27 '18 at 2:57














0












0








0







I have two arrays (say, $arrayA and $arrayB) with numerous values in them.



I would like to fill a list of 8 divs with these values using a foreach loop which has has just 4 divs in it, with each div increasing an incremental key from their respective array.



The following should hopefully illustrate:



foreach ($values as $value) {
echo
'<div class="type-a">' . $arrayA[i] . '</div>
<div class="type-b">' . $arrayB[i] . '</div>
<div class="type-a">' . $arrayA[i+1] . '</div>
<div class="type-b">' . $arrayB[i+1] . '</div>';
}

<div class="type-a"> A1 </div>
<div class="type-b"> B1 </div>
<div class="type-a"> A2 </div>
<div class="type-b"> B2 </div>
<div class="type-a"> A3 </div>
<div class="type-b"> B3 </div>
<div class="type-a"> A4 </div>
<div class="type-b"> B4 </div>


Any suggestions would be great!










share|improve this question













I have two arrays (say, $arrayA and $arrayB) with numerous values in them.



I would like to fill a list of 8 divs with these values using a foreach loop which has has just 4 divs in it, with each div increasing an incremental key from their respective array.



The following should hopefully illustrate:



foreach ($values as $value) {
echo
'<div class="type-a">' . $arrayA[i] . '</div>
<div class="type-b">' . $arrayB[i] . '</div>
<div class="type-a">' . $arrayA[i+1] . '</div>
<div class="type-b">' . $arrayB[i+1] . '</div>';
}

<div class="type-a"> A1 </div>
<div class="type-b"> B1 </div>
<div class="type-a"> A2 </div>
<div class="type-b"> B2 </div>
<div class="type-a"> A3 </div>
<div class="type-b"> B3 </div>
<div class="type-a"> A4 </div>
<div class="type-b"> B4 </div>


Any suggestions would be great!







php arrays loops foreach key






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 '18 at 1:19









user2798841

961112




961112








  • 1




    you can try to do loop based on len(array), then i:0 i+=2, then get the first and second array from a and b
    – Evinn
    Dec 27 '18 at 2:57














  • 1




    you can try to do loop based on len(array), then i:0 i+=2, then get the first and second array from a and b
    – Evinn
    Dec 27 '18 at 2:57








1




1




you can try to do loop based on len(array), then i:0 i+=2, then get the first and second array from a and b
– Evinn
Dec 27 '18 at 2:57




you can try to do loop based on len(array), then i:0 i+=2, then get the first and second array from a and b
– Evinn
Dec 27 '18 at 2:57












1 Answer
1






active

oldest

votes


















0














After your comment regarding differences in length, you can count the size of each to determine which is bigger and then iterate over that one as the main array:



<?php
# Make sure each value is an array
$arrayA = (empty($arrayA))? : array_values($arrayA);
$arrayB = (empty($arrayB))? : array_values($arrayB);
# Determine which array is longer
if(count($arrayA) >= count($arrayB)) {
$arr_L = $arrayA;
$arr_S = $arrayB;
}
else {
$arr_L = $arrayB;
$arr_S = $arrayA;
}
# Iterate over the larger array
foreach($arr_L as $key => $value): ?>

<!-- Just echo the value here because you are iterating this array -->
<div class="type-a"> <?php echo $value ?> </div>

<!-- Use the current key from this array to fetch the value from the B array -->
<div class="type-b"> <?php echo $arr_S[$key] ?> </div>

<?php endforeach ?>





share|improve this answer























  • In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
    – user2798841
    Dec 27 '18 at 14:24










  • You have to determine which array is larger and iterate over that one. See edit.
    – Rasclatt
    Dec 27 '18 at 16:08











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%2f53938782%2fcontinuous-array-key-in-foreach-loop-outputs%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









0














After your comment regarding differences in length, you can count the size of each to determine which is bigger and then iterate over that one as the main array:



<?php
# Make sure each value is an array
$arrayA = (empty($arrayA))? : array_values($arrayA);
$arrayB = (empty($arrayB))? : array_values($arrayB);
# Determine which array is longer
if(count($arrayA) >= count($arrayB)) {
$arr_L = $arrayA;
$arr_S = $arrayB;
}
else {
$arr_L = $arrayB;
$arr_S = $arrayA;
}
# Iterate over the larger array
foreach($arr_L as $key => $value): ?>

<!-- Just echo the value here because you are iterating this array -->
<div class="type-a"> <?php echo $value ?> </div>

<!-- Use the current key from this array to fetch the value from the B array -->
<div class="type-b"> <?php echo $arr_S[$key] ?> </div>

<?php endforeach ?>





share|improve this answer























  • In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
    – user2798841
    Dec 27 '18 at 14:24










  • You have to determine which array is larger and iterate over that one. See edit.
    – Rasclatt
    Dec 27 '18 at 16:08
















0














After your comment regarding differences in length, you can count the size of each to determine which is bigger and then iterate over that one as the main array:



<?php
# Make sure each value is an array
$arrayA = (empty($arrayA))? : array_values($arrayA);
$arrayB = (empty($arrayB))? : array_values($arrayB);
# Determine which array is longer
if(count($arrayA) >= count($arrayB)) {
$arr_L = $arrayA;
$arr_S = $arrayB;
}
else {
$arr_L = $arrayB;
$arr_S = $arrayA;
}
# Iterate over the larger array
foreach($arr_L as $key => $value): ?>

<!-- Just echo the value here because you are iterating this array -->
<div class="type-a"> <?php echo $value ?> </div>

<!-- Use the current key from this array to fetch the value from the B array -->
<div class="type-b"> <?php echo $arr_S[$key] ?> </div>

<?php endforeach ?>





share|improve this answer























  • In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
    – user2798841
    Dec 27 '18 at 14:24










  • You have to determine which array is larger and iterate over that one. See edit.
    – Rasclatt
    Dec 27 '18 at 16:08














0












0








0






After your comment regarding differences in length, you can count the size of each to determine which is bigger and then iterate over that one as the main array:



<?php
# Make sure each value is an array
$arrayA = (empty($arrayA))? : array_values($arrayA);
$arrayB = (empty($arrayB))? : array_values($arrayB);
# Determine which array is longer
if(count($arrayA) >= count($arrayB)) {
$arr_L = $arrayA;
$arr_S = $arrayB;
}
else {
$arr_L = $arrayB;
$arr_S = $arrayA;
}
# Iterate over the larger array
foreach($arr_L as $key => $value): ?>

<!-- Just echo the value here because you are iterating this array -->
<div class="type-a"> <?php echo $value ?> </div>

<!-- Use the current key from this array to fetch the value from the B array -->
<div class="type-b"> <?php echo $arr_S[$key] ?> </div>

<?php endforeach ?>





share|improve this answer














After your comment regarding differences in length, you can count the size of each to determine which is bigger and then iterate over that one as the main array:



<?php
# Make sure each value is an array
$arrayA = (empty($arrayA))? : array_values($arrayA);
$arrayB = (empty($arrayB))? : array_values($arrayB);
# Determine which array is longer
if(count($arrayA) >= count($arrayB)) {
$arr_L = $arrayA;
$arr_S = $arrayB;
}
else {
$arr_L = $arrayB;
$arr_S = $arrayA;
}
# Iterate over the larger array
foreach($arr_L as $key => $value): ?>

<!-- Just echo the value here because you are iterating this array -->
<div class="type-a"> <?php echo $value ?> </div>

<!-- Use the current key from this array to fetch the value from the B array -->
<div class="type-b"> <?php echo $arr_S[$key] ?> </div>

<?php endforeach ?>






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 27 '18 at 21:35

























answered Dec 27 '18 at 6:33









Rasclatt

11.1k31527




11.1k31527












  • In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
    – user2798841
    Dec 27 '18 at 14:24










  • You have to determine which array is larger and iterate over that one. See edit.
    – Rasclatt
    Dec 27 '18 at 16:08


















  • In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
    – user2798841
    Dec 27 '18 at 14:24










  • You have to determine which array is larger and iterate over that one. See edit.
    – Rasclatt
    Dec 27 '18 at 16:08
















In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
– user2798841
Dec 27 '18 at 14:24




In actuality the arrays are not equal, in some cases ArrayA may have more values, and in other ArrayB may have more
– user2798841
Dec 27 '18 at 14:24












You have to determine which array is larger and iterate over that one. See edit.
– Rasclatt
Dec 27 '18 at 16:08




You have to determine which array is larger and iterate over that one. See edit.
– Rasclatt
Dec 27 '18 at 16:08


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53938782%2fcontinuous-array-key-in-foreach-loop-outputs%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas