Reputation: 147
I have an array that I have counted and it equals 4. That is what it should equal. Now I want the foreach loop inside the for loop to only run 4 times. As it stands, I am getting to many results. Below is my latest attempt that does not work.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i <= $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
$i++;
}
the networks array is populated from a WordPress widget that has a repeating field section. The section allows the user to set social media icons. I currently have 4 social media icons set. On the front end, the page shows every social media icon available even though there are only 4 set. So I am trying to get the nested foreach loop to only run 4 times.
Upvotes: 0
Views: 2961
Reputation: 589
I have something more interesting for you.. it's a widget parent with children! You can change field to load new fields from other classes here's the link:
https://github.com/hishamdalal/parent_widget
Upvotes: 0
Reputation: 1066
Currently your loop is executing 5 times..and you want it to run only 4 times... So Change this condition...for($i = 0; $i <= $size; $i++)
to either for($i = 0; $i <$size; $i++)
or for($i = 1; $i <= $size; $i++)
like i.e.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i < $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
}
Upvotes: 1