title: getting emptied while checking if an array value exists - laravel?
body: <p>I wanted to insert the <code>id</code> values of <code>$courseCat</code> if that id is not present in <code>$m</code>.In the below code, i feel the <code>$m</code> array is getting emptied every time and so, all id's are getting inserted to the <code>$m</code> ,as the result of <code>dump($m)</code> indicates.How can i fix this?</p>
  <pre><code>$m=[];
  $courseCat = MyCourse::where('course_id', $key['courseId'])-&gt;get()-&gt;toArray();
  foreach($courseCat as $k=&gt;$c){
    // dump($courseCat);
      if(!in_array($c['id'],$m)){
             array_push($m,$c['id']);
        }
  }
  //dump($m); 
  </code></pre>
  <p><code>dump($courseCat);</code> shows the following result</p>
  <pre><code>    array:2 [
        0 =&gt; array:19 [
          &quot;id&quot; =&gt; 2
          &quot;course_id&quot; =&gt; 18
        ]
        1 =&gt; array:19 [
          &quot;id&quot; =&gt; 3
          &quot;course_id&quot; =&gt; 18
        ]                                   
      ]
     array:2 [
        0 =&gt; array:19 [
          &quot;id&quot; =&gt; 2
          &quot;course_id&quot; =&gt; 18
        ]
        1 =&gt; array:19 [
          &quot;id&quot; =&gt; 3
          &quot;course_id&quot; =&gt; 18
        ]                                   
      ]
      array:2 [
        0 =&gt; array:19 [
          &quot;id&quot; =&gt; 5
          &quot;course_id&quot; =&gt; 1
        ]                                
        1 =&gt; array:19 [
          &quot;id&quot; =&gt; 6
          &quot;course_id&quot; =&gt; 1
        ]
      ]
      array:2 [
        0 =&gt; array:19 [
          &quot;id&quot; =&gt; 5
          &quot;course_id&quot; =&gt; 1
        ]                                 
        1 =&gt; array:19 [
          &quot;id&quot; =&gt; 6
          &quot;course_id&quot; =&gt; 1
        ]
      ]
  </code></pre>
  <p><code>dump($m)</code> shows the below result:</p>
  <pre><code>array:2 [
    0 =&gt; 2
    1 =&gt; 3
  ]
  array:2 [
    0 =&gt; 2
    1 =&gt; 3
  ]
  array:2 [
    0 =&gt; 6
    1 =&gt; 5
  ]
  array:2 [
    0 =&gt; 6
    1 =&gt; 5
  ]
  </code></pre>
  <p>Below attached images are the result of the query <code>MyCourse::where('course_id', $key['courseId'])-&gt;get()</code>.</p>
  <p>[<a href="https://i.stack.imgur.com/K47P1.png" rel="nofollow noreferrer">enter image description here</a>]<a href="https://i.stack.imgur.com/K47P1.png" rel="nofollow noreferrer">1</a></p>
  <p>[<a href="https://i.stack.imgur.com/OerOH.png" rel="nofollow noreferrer">enter image description here</a>]<a href="https://i.stack.imgur.com/OerOH.png" rel="nofollow noreferrer">2</a></p>
  <p>[<a href="https://i.stack.imgur.com/muVIF.png" rel="nofollow noreferrer">enter image description here</a>]<a href="https://i.stack.imgur.com/muVIF.png" rel="nofollow noreferrer">3</a></p>
  <p>Expected result :</p>
  <p><code>dump($m)</code> should show the below result</p>
  <pre><code>[0=&gt;2,
  1=&gt;3,
  2=&gt;5,
  3=&gt;6]
  </code></pre>