I can't put a value in the array?

25 views Asked by At

I made an iterative loop and created two matrices and I want when I find a specific name I put the corresponding value from the other row in the J variable but I gave me the wrong result which is the number 8 where the number was supposed to appear 7 enter image description here // my code:

<?php
include 'DB.php';
$db=DB::getInstance();
//$posts = $db->table('posts')->get();
//echo json_encode($posts);
//$users = $db->table("posts")->Qget();
$rows = $db->table('posts')->get();
$filter_value1 = [];
$filter_value2 = [];

$i=0; 
$j=0;

foreach($rows as $row){

  $filter_value1[]=$row->name;
  $filter_value2[]=$row->user_id;

  if($filter_value1[$i]="gmal"){

    $j= $filter_value2[$i];
  }
    // echo "$row->name <br>";

  $i++;
}

echo($j);
1

There are 1 answers

0
Ussaid Iqbal On BEST ANSWER

= : This is an assignment operator in any Language suppose we want to assign some value to a variable we will use (equal to) = sign. It does not return anything. e.g.

$name= "gmal";

== : This is a comparison operator. If we want to compare two values or values hold by variables we should use ==. This operator returns True /False bases on comparison e.g.

if("22" == 22) it will return true

=== : Checks the values as well as the type of operands.

if("22" === 22) it will return false

if($name == "gmal"){
echo "Name is : {$name}";
}

so in your code change the following line

if($filter_value1[$i]="gmal")

to

if($filter_value1[$i] == "gmal")