0

I have 20 items in my foreach statement. I have been added counter using $index = 0; $index++

$index = 0;
foreach( $partialStructure as $map ) {

 // i am getting specific number of $tmp for specific subject ( member )
 $tmp = $this->getFieldValueString($field, $value, $subject, $map, $partialStructure);

 $array = array(
      $index => $index,
 );

if(count($array) < 20) {
   // redirect
} else {
  // do nothing
}

$index++;
}

In my case, i want to redirect member to another page if there are not 20 items. But when i check ['20'] and setup else, other 1 to 19 are still there, and redirect are going also if member have ['20'] exist.

How i can make it happend, if member have 20 items to dont redirect, and if there is less then 20 items to redirect?

Thanks !

5
  • It's not clear what you're trying to do here. It looks like your whole code could be reduced to something like $index = count($partialStructure); $array = array($index => $index); Commented Aug 3, 2014 at 19:13
  • 1
    You dont show any code that is making a decision on when to do what. Add the code you have now to make that decision and the question may become more clear to us all Commented Aug 3, 2014 at 19:16
  • OR you can add direct check on $partialStructure variable like count($partialStructure) < 20 Commented Aug 3, 2014 at 19:26
  • The problem is getting more complicated. In my foreach i have another comment calling $tmp = $this->getFieldValueString($field, $value, $subject, $map, $partialStructure); This when i do count($tmp) its show 1, but there are 20 items to my member. Now, i dont know, but the with index as array , i can count that $tmp have 20 items. Problem is that foreach goes 1 to 19, if member have 20 or less, all are redirected, instead only one who have less then 20 ... Commented Aug 3, 2014 at 20:03
  • I make small improvment in my code to understand what i am doing. Commented Aug 3, 2014 at 20:11

3 Answers 3

1

You can just check the length of the array rather than computing the length through iterating through it.

if(count($array)) < 20) {
  //redirect
}else{
  //do whatever you need to do
}

count()

Sign up to request clarification or add additional context in comments.

Comments

1

Try using the php function count() to get the number of elements in your array ;) https://www.php.net/manual/en/function.count.php

Comments

0

I fix my code like this:

$counter = 0;
$tmp= array();
foreach( $partialStructure as $map ) {

 // i am getting specific number of $tmp for specific subject ( member )
 $tmp[] = $this->getFieldValueString($field, $value, $subject, $map, $partialStructure);

if($tmp['20'])
$counter++;


}
// out of foreach element, i setup this
if($counter == 0) {
   header("Location: /members/edit/profile?redirect=1");     
}

$counter will give you 1, if tmp['20'] is there, else, you can do the redirect...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.