Reputation: 7688
Having 3 multidimensional arrays, to whom I do a foreach
how can I limit the multidimensional response inside foreach from X items to lets say 20.
Code:
$i = 0;
foreach ($value->channel->item as $item)
{
$data['data'][$keySection]['item1'][$i]['url'] = $item->url;
$data['data'][$keySection]['item1'][$i]['title'] = $item->title;
$data['data'][$keySection]['item1'][$i]['img'] = $item->thumb;
$i++;
}
where $value
is contained within
foreach ($homeData as $keySection => $valueSection)
{
foreach($valueSection as $key => $value)
{
switch ($key)
{
I've tried aplying some for
s both within foreach ($value->channel->item as $item)
as outside but I just can't get it to work properly, I get either doubled results or not working at all.
How can I make this work??
Edit:
$i
has nothing to do with it... I need to limit $value->channel->item
where item
contains X results
Edit2:
$i
is for $homeData
where $homeData
contains three values and each and one of those will later contain 3 different values of $value->channel->item
so if item contains 20 results, will be 3x20 = 60 and $i is ment to separate each 20 results...
Edit3: ok, now I get it... sorry for the misunderstanding
Upvotes: 0
Views: 851
Reputation: 1032
After you start the foreach, add:
if($i > 19) {
break;
}
This checks if $i is greater than 19 (which means 20 iterations) and then breaks this foreach loop. More information about break, here.
Upvotes: 2
Reputation: 6335
You can do it like :
$i = 0;
foreach ($value->channel->item as $item)
{
if($i > 19) {
break;
}
$data['data'][$keySection]['item1'][$i]['url'] = $item->url;
$data['data'][$keySection]['item1'][$i]['title'] = $item->title;
$data['data'][$keySection]['item1'][$i]['img'] = $item->thumb;
$i++;
}
This will give you 20 items.
Hope this is what you want :)
Upvotes: 0