2

I have an array

$assid=Array
(
    [0] => Array
        (
            [0] => 6
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 6
        )

    [3] => Array
        (
            [0] => 2
            [1] => 3
        )

)

and

$key1=Array
    (
        [0] => 0
        [1] => 1
        [2] => 2
        [3] => 3
    )

Here $key1 means index of $assid

And a foreach loop

 @foreach($roles as $id=>$name)



<option value="{{$id}}"@if(in_array($id, $assid))selected="selected"@endif>{{$name}}</option>

   @endforeach

Here roles have 12 values.so array will iterate 12 times.my problem is that I want to append $assid[0],[1],[2],[3] so i $key1 values with foreach so I tried

$a=0;
     @foreach($roles as $id=>$name)



    <option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
    <?php  $a++;?>
       @endforeach

Because $key1 array as only index 0 1 2 3.so i need $a should be incremented as 0 to 3 inside foreach loop 12 iterations are there so after 3 it shows undefined index 4.i didn't get a proper solution. please help me.Please

juva jacob
  • 115
  • 1
  • 11
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – LF00 Nov 28 '17 at 06:17
  • can you show use the values of `$assid`? is it array? if yes then print it and add the output to your question what you did for `$key1` – Alive to die - Anant Nov 28 '17 at 06:26
  • @AlivetoDie--Anantsingh I have updated the question Please help me – juva jacob Nov 28 '17 at 06:32
  • @juvajacob actually i am unable to understand your question. `$roles` array data is also needed. so that i can check your code. Please add that also. Or may be any-one answer is already helped you. If not add `$roles` array in your question too – Alive to die - Anant Nov 28 '17 at 06:59

4 Answers4

0
$a=0;
 @foreach($roles as $id=>$name)
<option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
<?php  $a++;?
 if(a>4){
 a = 0;
   }>
   @endforeach

it make value zero if you are more then 4 so it start agin from zero

0

If i understand your problem you are looking for this stuff...Try this :-

$assid=Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
$roles=Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 6
[6] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11

)
@foreach($roles as $id => $name)
  @if(isset($assid[$id]))
    <option value="{{$id}}"@if(in_array($id, $assid))selected="selected"@endif>{{$name}}</option>
  @else 
    <option value="{{$id}}">{{$name}}</option>
  @endif
@endforeach

Hope it helps!

kunal
  • 4,122
  • 12
  • 40
  • 75
0

Possibly it works as expected.

$a=0;
@foreach($roles as $id=>$name)
<?php if($a>3) $a=0; ?>
<option value="{{$id}}"@if(in_array($id, $assid[$a]))selected="selected"@endif>{{$name}}</option>
<?php  $a++; ?>
@endforeach
Ashok
  • 128
  • 1
  • 8
0

You need to round-robin back from N to 0. Using an index that is modulo N + 1 of the counter guarantees this.

To round-robin through the first 3 items of an array, where N=2 because arrays have zero-based indexing, we'll take modulo 3

@php ($a = 0)

@foreach($roles as $id=>$name)
   <option
     value="{{$id}}" 
     @if(in_array($id, $assid[$a % 3])) 
         selected="selected"
     @endif
   >{{ $name }}</option>

   @php ($a = $a + 1)
@endforeach
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • thanks for your reply..but its get always 4 value selected.my requirement is first iteration$assid[0]) should selected it depends $assid array. ihave updated my question can you please help me once more. – juva jacob Nov 28 '17 at 06:35
  • Taking modulo `4` of the counter puts a stop at `3` and goes back to `0`. Are you concerned with doing a round-robin using the first 3 arrays in the `assid`? – Oluwafemi Sule Nov 28 '17 at 06:37
  • -yes ..so first array with index 0 so index 0 have value 6 2 3 sholud be selected.second iteration index 1 means 4,5,6 should be selected.silmilarly so on .please help me.here assign[0] means first array,assgn[1] means 2second array similarly so – juva jacob Nov 28 '17 at 06:41
  • Okay, this means you take modulo 3 of your counter to use in indexing `assid` – Oluwafemi Sule Nov 28 '17 at 06:47
  • -four iteration always same value is get selected – juva jacob Nov 28 '17 at 07:22
  • Yeah, that will be the case. The last option with a `selected` attribute is rendered by the browser as selected one. Check the generated markup to confirm this behaviour. I don't think this is as much of a server side issue as it is an issue with how this part of your application has been organized. – Oluwafemi Sule Nov 28 '17 at 07:33