Get the nth element from array_chunk returned results











up vote
-1
down vote

favorite
1












I have a very large array which I have split with array_chunk into six, I'm looking to get remove/access the 5th (all for the price) item in every chunk. Here are 2 of many chunks



 Array
(
[0] => Scottsdale
[1] => Santa Ana John Wayne Airport
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $10 980)
)

Array
(
[0] => Milwaukee Mitchell Intl
[1] => Chicago Midway International
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $6 300)
)









share|improve this question




















  • 1




    I'm looking to get remove/access the 5th (all for the price) item in every chunk-> in an array or just want to print through loop?
    – Alive to Die
    Nov 12 at 11:44






  • 1




    foreach(array_chunk($arr, 6) as $a) { echo $a[5]; }
    – Federico klez Culloca
    Nov 12 at 11:44












  • So do you want to access them or remove them?
    – iainn
    Nov 12 at 11:46










  • I wanted to access them bthatnks i found an easier way
    –  kariuki
    Nov 12 at 11:55










  • @kariuki it's your responsibility to mark an answer as accepted answer if you got the solution.
    – Alive to Die
    Nov 13 at 7:10

















up vote
-1
down vote

favorite
1












I have a very large array which I have split with array_chunk into six, I'm looking to get remove/access the 5th (all for the price) item in every chunk. Here are 2 of many chunks



 Array
(
[0] => Scottsdale
[1] => Santa Ana John Wayne Airport
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $10 980)
)

Array
(
[0] => Milwaukee Mitchell Intl
[1] => Chicago Midway International
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $6 300)
)









share|improve this question




















  • 1




    I'm looking to get remove/access the 5th (all for the price) item in every chunk-> in an array or just want to print through loop?
    – Alive to Die
    Nov 12 at 11:44






  • 1




    foreach(array_chunk($arr, 6) as $a) { echo $a[5]; }
    – Federico klez Culloca
    Nov 12 at 11:44












  • So do you want to access them or remove them?
    – iainn
    Nov 12 at 11:46










  • I wanted to access them bthatnks i found an easier way
    –  kariuki
    Nov 12 at 11:55










  • @kariuki it's your responsibility to mark an answer as accepted answer if you got the solution.
    – Alive to Die
    Nov 13 at 7:10















up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





I have a very large array which I have split with array_chunk into six, I'm looking to get remove/access the 5th (all for the price) item in every chunk. Here are 2 of many chunks



 Array
(
[0] => Scottsdale
[1] => Santa Ana John Wayne Airport
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $10 980)
)

Array
(
[0] => Milwaukee Mitchell Intl
[1] => Chicago Midway International
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $6 300)
)









share|improve this question















I have a very large array which I have split with array_chunk into six, I'm looking to get remove/access the 5th (all for the price) item in every chunk. Here are 2 of many chunks



 Array
(
[0] => Scottsdale
[1] => Santa Ana John Wayne Airport
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $10 980)
)

Array
(
[0] => Milwaukee Mitchell Intl
[1] => Chicago Midway International
[2] => Cessna C750 Citation X
[3] => 8
[4] => Mon Nov 12 00:00:00 GMT 2018
[5] => Call for Price (Was $6 300)
)






php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 11:44









Gufran Hasan

3,25831225




3,25831225










asked Nov 12 at 11:40









kariuki

11




11








  • 1




    I'm looking to get remove/access the 5th (all for the price) item in every chunk-> in an array or just want to print through loop?
    – Alive to Die
    Nov 12 at 11:44






  • 1




    foreach(array_chunk($arr, 6) as $a) { echo $a[5]; }
    – Federico klez Culloca
    Nov 12 at 11:44












  • So do you want to access them or remove them?
    – iainn
    Nov 12 at 11:46










  • I wanted to access them bthatnks i found an easier way
    –  kariuki
    Nov 12 at 11:55










  • @kariuki it's your responsibility to mark an answer as accepted answer if you got the solution.
    – Alive to Die
    Nov 13 at 7:10
















  • 1




    I'm looking to get remove/access the 5th (all for the price) item in every chunk-> in an array or just want to print through loop?
    – Alive to Die
    Nov 12 at 11:44






  • 1




    foreach(array_chunk($arr, 6) as $a) { echo $a[5]; }
    – Federico klez Culloca
    Nov 12 at 11:44












  • So do you want to access them or remove them?
    – iainn
    Nov 12 at 11:46










  • I wanted to access them bthatnks i found an easier way
    –  kariuki
    Nov 12 at 11:55










  • @kariuki it's your responsibility to mark an answer as accepted answer if you got the solution.
    – Alive to Die
    Nov 13 at 7:10










1




1




I'm looking to get remove/access the 5th (all for the price) item in every chunk-> in an array or just want to print through loop?
– Alive to Die
Nov 12 at 11:44




I'm looking to get remove/access the 5th (all for the price) item in every chunk-> in an array or just want to print through loop?
– Alive to Die
Nov 12 at 11:44




1




1




foreach(array_chunk($arr, 6) as $a) { echo $a[5]; }
– Federico klez Culloca
Nov 12 at 11:44






foreach(array_chunk($arr, 6) as $a) { echo $a[5]; }
– Federico klez Culloca
Nov 12 at 11:44














So do you want to access them or remove them?
– iainn
Nov 12 at 11:46




So do you want to access them or remove them?
– iainn
Nov 12 at 11:46












I wanted to access them bthatnks i found an easier way
–  kariuki
Nov 12 at 11:55




I wanted to access them bthatnks i found an easier way
–  kariuki
Nov 12 at 11:55












@kariuki it's your responsibility to mark an answer as accepted answer if you got the solution.
– Alive to Die
Nov 13 at 7:10






@kariuki it's your responsibility to mark an answer as accepted answer if you got the solution.
– Alive to Die
Nov 13 at 7:10














2 Answers
2






active

oldest

votes

















up vote
2
down vote













1. If you want to access them and put them into separate array then use array_column()



$price_array = array_column($array,5);
print_r($price_array);


Output:-https://3v4l.org/1slp0



2. If you want to access them for printing purpose then use foreach()



foreach($array as $arr){

echo $arr[5].PHP_EOL;
}


Output:-https://3v4l.org/NXVEb



3. If you want to remove them then again use foreach()



foreach($array as &$arr){

unset($arr[5]);
}


Output:-https://3v4l.org/W5WYO



4. if you want to remove that value from child array as well as want to print then use unset() and print_r() along with foreach()



foreach ($array as $arr) {
unset($arr[5]); //this will not remove the value from original array
echo "<pre/>";print_r($arr);
}


Output:- https://3v4l.org/Td4u3



Reference:- Passing by Reference






share|improve this answer























  • Thanks i will try it out but i have found another way
    –  kariuki
    Nov 12 at 11:54










  • @kariuki check my currently edited solution
    – Alive to Die
    Nov 12 at 11:54


















up vote
-4
down vote













I solved it:-



foreach ($myrows as $myrows) {
unset($myrows[5]);
echo "<pre>";
print_r($myrows);
echo "</pre>";
}





share|improve this answer



















  • 1




    Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
    – Andreas
    Nov 12 at 12:08






  • 1




    @Andreas lot of magic happens on stack now a days.
    – Alive to Die
    Nov 12 at 12:18












  • array_chunk($rows,10,false);
    –  kariuki
    Nov 13 at 14:22










  • Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
    –  kariuki
    Nov 13 at 14:34











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',
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%2f53261397%2fget-the-nth-element-from-array-chunk-returned-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













1. If you want to access them and put them into separate array then use array_column()



$price_array = array_column($array,5);
print_r($price_array);


Output:-https://3v4l.org/1slp0



2. If you want to access them for printing purpose then use foreach()



foreach($array as $arr){

echo $arr[5].PHP_EOL;
}


Output:-https://3v4l.org/NXVEb



3. If you want to remove them then again use foreach()



foreach($array as &$arr){

unset($arr[5]);
}


Output:-https://3v4l.org/W5WYO



4. if you want to remove that value from child array as well as want to print then use unset() and print_r() along with foreach()



foreach ($array as $arr) {
unset($arr[5]); //this will not remove the value from original array
echo "<pre/>";print_r($arr);
}


Output:- https://3v4l.org/Td4u3



Reference:- Passing by Reference






share|improve this answer























  • Thanks i will try it out but i have found another way
    –  kariuki
    Nov 12 at 11:54










  • @kariuki check my currently edited solution
    – Alive to Die
    Nov 12 at 11:54















up vote
2
down vote













1. If you want to access them and put them into separate array then use array_column()



$price_array = array_column($array,5);
print_r($price_array);


Output:-https://3v4l.org/1slp0



2. If you want to access them for printing purpose then use foreach()



foreach($array as $arr){

echo $arr[5].PHP_EOL;
}


Output:-https://3v4l.org/NXVEb



3. If you want to remove them then again use foreach()



foreach($array as &$arr){

unset($arr[5]);
}


Output:-https://3v4l.org/W5WYO



4. if you want to remove that value from child array as well as want to print then use unset() and print_r() along with foreach()



foreach ($array as $arr) {
unset($arr[5]); //this will not remove the value from original array
echo "<pre/>";print_r($arr);
}


Output:- https://3v4l.org/Td4u3



Reference:- Passing by Reference






share|improve this answer























  • Thanks i will try it out but i have found another way
    –  kariuki
    Nov 12 at 11:54










  • @kariuki check my currently edited solution
    – Alive to Die
    Nov 12 at 11:54













up vote
2
down vote










up vote
2
down vote









1. If you want to access them and put them into separate array then use array_column()



$price_array = array_column($array,5);
print_r($price_array);


Output:-https://3v4l.org/1slp0



2. If you want to access them for printing purpose then use foreach()



foreach($array as $arr){

echo $arr[5].PHP_EOL;
}


Output:-https://3v4l.org/NXVEb



3. If you want to remove them then again use foreach()



foreach($array as &$arr){

unset($arr[5]);
}


Output:-https://3v4l.org/W5WYO



4. if you want to remove that value from child array as well as want to print then use unset() and print_r() along with foreach()



foreach ($array as $arr) {
unset($arr[5]); //this will not remove the value from original array
echo "<pre/>";print_r($arr);
}


Output:- https://3v4l.org/Td4u3



Reference:- Passing by Reference






share|improve this answer














1. If you want to access them and put them into separate array then use array_column()



$price_array = array_column($array,5);
print_r($price_array);


Output:-https://3v4l.org/1slp0



2. If you want to access them for printing purpose then use foreach()



foreach($array as $arr){

echo $arr[5].PHP_EOL;
}


Output:-https://3v4l.org/NXVEb



3. If you want to remove them then again use foreach()



foreach($array as &$arr){

unset($arr[5]);
}


Output:-https://3v4l.org/W5WYO



4. if you want to remove that value from child array as well as want to print then use unset() and print_r() along with foreach()



foreach ($array as $arr) {
unset($arr[5]); //this will not remove the value from original array
echo "<pre/>";print_r($arr);
}


Output:- https://3v4l.org/Td4u3



Reference:- Passing by Reference







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 7:08

























answered Nov 12 at 11:51









Alive to Die

55k82868




55k82868












  • Thanks i will try it out but i have found another way
    –  kariuki
    Nov 12 at 11:54










  • @kariuki check my currently edited solution
    – Alive to Die
    Nov 12 at 11:54


















  • Thanks i will try it out but i have found another way
    –  kariuki
    Nov 12 at 11:54










  • @kariuki check my currently edited solution
    – Alive to Die
    Nov 12 at 11:54
















Thanks i will try it out but i have found another way
–  kariuki
Nov 12 at 11:54




Thanks i will try it out but i have found another way
–  kariuki
Nov 12 at 11:54












@kariuki check my currently edited solution
– Alive to Die
Nov 12 at 11:54




@kariuki check my currently edited solution
– Alive to Die
Nov 12 at 11:54












up vote
-4
down vote













I solved it:-



foreach ($myrows as $myrows) {
unset($myrows[5]);
echo "<pre>";
print_r($myrows);
echo "</pre>";
}





share|improve this answer



















  • 1




    Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
    – Andreas
    Nov 12 at 12:08






  • 1




    @Andreas lot of magic happens on stack now a days.
    – Alive to Die
    Nov 12 at 12:18












  • array_chunk($rows,10,false);
    –  kariuki
    Nov 13 at 14:22










  • Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
    –  kariuki
    Nov 13 at 14:34















up vote
-4
down vote













I solved it:-



foreach ($myrows as $myrows) {
unset($myrows[5]);
echo "<pre>";
print_r($myrows);
echo "</pre>";
}





share|improve this answer



















  • 1




    Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
    – Andreas
    Nov 12 at 12:08






  • 1




    @Andreas lot of magic happens on stack now a days.
    – Alive to Die
    Nov 12 at 12:18












  • array_chunk($rows,10,false);
    –  kariuki
    Nov 13 at 14:22










  • Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
    –  kariuki
    Nov 13 at 14:34













up vote
-4
down vote










up vote
-4
down vote









I solved it:-



foreach ($myrows as $myrows) {
unset($myrows[5]);
echo "<pre>";
print_r($myrows);
echo "</pre>";
}





share|improve this answer














I solved it:-



foreach ($myrows as $myrows) {
unset($myrows[5]);
echo "<pre>";
print_r($myrows);
echo "</pre>";
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 4:39









Alive to Die

55k82868




55k82868










answered Nov 12 at 11:52









kariuki

11




11








  • 1




    Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
    – Andreas
    Nov 12 at 12:08






  • 1




    @Andreas lot of magic happens on stack now a days.
    – Alive to Die
    Nov 12 at 12:18












  • array_chunk($rows,10,false);
    –  kariuki
    Nov 13 at 14:22










  • Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
    –  kariuki
    Nov 13 at 14:34














  • 1




    Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
    – Andreas
    Nov 12 at 12:08






  • 1




    @Andreas lot of magic happens on stack now a days.
    – Alive to Die
    Nov 12 at 12:18












  • array_chunk($rows,10,false);
    –  kariuki
    Nov 13 at 14:22










  • Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
    –  kariuki
    Nov 13 at 14:34








1




1




Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
– Andreas
Nov 12 at 12:08




Wow you must have tried really hard before posting the question. I mean 10 minutes later you have a working code.
– Andreas
Nov 12 at 12:08




1




1




@Andreas lot of magic happens on stack now a days.
– Alive to Die
Nov 12 at 12:18






@Andreas lot of magic happens on stack now a days.
– Alive to Die
Nov 12 at 12:18














array_chunk($rows,10,false);
–  kariuki
Nov 13 at 14:22




array_chunk($rows,10,false);
–  kariuki
Nov 13 at 14:22












Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
–  kariuki
Nov 13 at 14:34




Very cheeky @Andrea the reason it dint work i was using array_chunk($rows,10,true); somewhere in the code instead of array_chunk($rows,10,false ); that way i could not get to the item . i changed to false and then pasted the array here before tasting it , then realized the array keys have changed and can easily accessed .
–  kariuki
Nov 13 at 14:34


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261397%2fget-the-nth-element-from-array-chunk-returned-results%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents