CodeIgniter Forums

Full Version: Check if exist in array (all in foreach)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!
I have little problem.
I need to check every row in another array, and if exist - do something.

This is my code:
PHP Code:
<?php $Sekcje explode','$Detail->SectionsPrefered ); ?>
                        <?php foreach ($SectionsList as $SectionOption): ?>
                                <div class="checkbox">
                                    <?php if ( in_array$SectionOption->Title $Sekcje ) ): ?>
                                        *
                                    <?php endif ?>
                                    <label>
                                        <input type="checkbox" value="<?php echo $SectionOption->Title?>">
                                        <?php echo $SectionOption->Title?>
                                    </label>
                                </div>
                        <?php endforeach ?>

It's display only one '*', but must display for 3 rows (Section1, Section2, Section3)

This is the array $Sekcje:
PHP Code:
array(3) { 
       [
0]=> string(8"Section1" 
       
[1]=> string(9" Section2" 
       
[2]=> string(9" Section3" 


Where is the problem?
(12-11-2015, 09:27 AM)vertisan Wrote: [ -> ]Hi!
I have little problem.
I need to check every row in another array, and if exist - do something.

This is my code:
PHP Code:
                       <?php $Sekcje explode','$Detail->SectionsPrefered ); ?>
                        <?php foreach ($SectionsList as $SectionOption): ?>
                                <div class="checkbox">
                                    <?php if ( in_array$SectionOption->Title $Sekcje ) ): ?>
                                        *
                                    <?php endif ?>
                                    <label>
                                        <input type="checkbox" value="<?php echo $SectionOption->Title?>">
                                        <?php echo $SectionOption->Title?>
                                    </label>
                                </div>
                        <?php endforeach ?>

It's display only one '*', but must display for 3 rows (Section1, Section2, Section3)

This is the array $Sekcje:
PHP Code:
array(3) { 
 
      [0]=> string(8"Section1" 
 
      [1]=> string(9" Section2" 
 
      [2]=> string(9" Section3" 


Where is the problem?

Was it display only one '*' corresponding to the "Section1"? What's your $SectionsList value?
Do a var_dump on your details array and make sure you are getting more then just one value.
Var_dump $SectionsList:
PHP Code:
array(5) {
 
 [0]=>
 
 object(stdClass)#33 (3) {
 
   ["ID"]=>
 
   string(1"1"
 
   ["Title"]=>
 
   string(8"Section1"
 
   ["Description"]=>
 
   string(13"Opis sekcji 1"
 
 }
 
 [1]=>
 
 object(stdClass)#34 (3) {
 
   ["ID"]=>
 
   string(1"2"
 
   ["Title"]=>
 
   string(8"Section2"
 
   ["Description"]=>
 
   string(13"Opis sekcji 2"
 
 }
 
 [2]=>
 
 object(stdClass)#35 (3) {
 
   ["ID"]=>
 
   string(1"3"
 
   ["Title"]=>
 
   string(8"Section3"
 
   ["Description"]=>
 
   string(13"Opis sekcji 3"
 
 }
 
 [3]=>
 
 object(stdClass)#36 (3) {
 
   ["ID"]=>
 
   string(1"4"
 
   ["Title"]=>
 
   string(8"Section4"
 
   ["Description"]=>
 
   string(13"Opis sekcji 4"
 
 }
 
 [4]=>
 
 object(stdClass)#37 (3) {
 
   ["ID"]=>
 
   string(1"5"
 
   ["Title"]=>
 
   string(8"Section5"
 
   ["Description"]=>
 
   string(13"Opis sekcji 5"
 
 }


Var_dump $Sekcje:
PHP Code:
array(3) {
 
 [0]=>
 
 string(8"Section1"
 
 [1]=>
 
 string(9" Section2"
 
 [2]=>
 
 string(9" Section3"

(12-11-2015, 12:42 PM)vertisan Wrote: [ -> ]Var_dump $SectionsList:
PHP Code:
array(5) {
 
 [0]=>
 
 object(stdClass)#33 (3) {
 
   ["ID"]=>
 
   string(1"1"
 
   ["Title"]=>
 
   string(8"Section1"
 
   ["Description"]=>
 
   string(13"Opis sekcji 1"
 
 }
 
 [1]=>
 
 object(stdClass)#34 (3) {
 
   ["ID"]=>
 
   string(1"2"
 
   ["Title"]=>
 
   string(8"Section2"
 
   ["Description"]=>
 
   string(13"Opis sekcji 2"
 
 }
 
 [2]=>
 
 object(stdClass)#35 (3) {
 
   ["ID"]=>
 
   string(1"3"
 
   ["Title"]=>
 
   string(8"Section3"
 
   ["Description"]=>
 
   string(13"Opis sekcji 3"
 
 }
 
 [3]=>
 
 object(stdClass)#36 (3) {
 
   ["ID"]=>
 
   string(1"4"
 
   ["Title"]=>
 
   string(8"Section4"
 
   ["Description"]=>
 
   string(13"Opis sekcji 4"
 
 }
 
 [4]=>
 
 object(stdClass)#37 (3) {
 
   ["ID"]=>
 
   string(1"5"
 
   ["Title"]=>
 
   string(8"Section5"
 
   ["Description"]=>
 
   string(13"Opis sekcji 5"
 
 }


Var_dump $Sekcje:
PHP Code:
array(3) {
 
 [0]=>
 
 string(8"Section1"
 
 [1]=>
 
 string(9" Section2"
 
 [2]=>
 
 string(9" Section3"


It's because you have white-spaces in $Sekcje elements. Trim it can solve the problem.

PHP Code:
<?php $Sekcje array_map('trim'explode','$Detail->SectionsPrefered )); ?>