简体   繁体   English

为什么我在下面的代码中只获得数组“行”中的第一行值。 我需要与查询对应的所有值

[英]why am i only getting the first row value in array "row" in the below code. i need all the values corresponding to the query

  $s = $conn->prepare($q);
  $s->execute([$tagIdValue]);
  $d = $s->fetchAll();
  return $d;

function x($d) {
    foreach ($d as $row) {
      $val = $row["id"];
      $cont = trimContent($row);
      return $row;
    }
  }

i have a query, which returns all the values in the table and a function to convert it into an assosiate array.我有一个查询,它返回表中的所有值和一个将其转换为关联数组的函数。 But only getting the first row in the array但只获取数组中的第一行

Because ur using return inside loop, it only take first value and return it.因为你在循环中使用 return,所以它只获取第一个值并返回它。

  $s = $conn->prepare($q);
  $s->execute([$tagIdValue]);
  $d = $s->fetchAll();
  return $d;

function x($d) {
    $arr =[]
    foreach ($d as $row) {
      $val = $row["id"];
      $cont = trimContent($row);
      array_push($arr, $val)
    }
   return $arr;
  }

Put your return outside the loop so only it returns only the first loop将你的 return 放在循环之外,这样它只返回第一个循环

function x($d) {
   foreach ($d as $row) {
     $val = $row["id"];
     $cont = trimContent($row);
     return $row;
}
}

Because, you are returning the result set in the loop.因为,您正在循环中返回结果集。

So, only the first row is returned from the loop.因此,只有第一行从循环中返回。

As, in the first iteration, return statement is encountered, so, that function will return and control will get out of the function.由于在第一次迭代中遇到了return语句,因此该函数将返回并且控制权将脱离该函数。

And next iterations will not loop over the result set.下一次迭代不会遍历结果集。

We need to create an array before loop,我们需要在循环之前创建一个数组,

append results to that array将结果附加到该数组

and return the array.并返回数组。

Corrected Code:更正代码:

$s = $conn->prepare($q);
  $s->execute([$tagIdValue]);
  $d = $s->fetchAll();
  return $d;

function x($d) {
 $array = array();
    foreach ($d as $row) {
      $val = $row["id"];
      $cont = trimContent($row);
       $array[] = $row;
    }
 return $array;
}

Instead writing a return inside a loop, replace that line with an array push or array shift function.而不是在循环中编写 return,而是用数组推送或数组移位函数替换该行。

Once the loop has ended return the final result obtained from array_push/array_shift.一旦循环结束,返回从 array_push/array_shift 获得的最终结果。

You need to return outside the loop:您需要在循环外返回:

$s = $conn->prepare($q);
$s->execute([$tagIdValue]);
$d = $s->fetchAll();
return $d;

function x($d) {
  $arr =array();
  foreach ($d as $k=> $row) {
    $val = $row["id"];
    $cont = trimContent($row);
    $arr[$k] = $row;
  }
  return $arr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我只得到桌子的第一行 - I am getting only first row of my table 为什么我的脚本只获取我的php表的第一行? 下面是我的代码 - Why is my script is only getting the first row of my php table? below is my code 为什么我在我的代码中收到“不正确的整数值:'' 列 'id' 在第 1 行”? - Why am I getting "Incorrect integer value: '' for column 'id' at row 1" in my code? 为什么我只能在Session中存储的数组中得到第一个字符? - Why am I getting only first character in array stored in Session? 为什么我只收到数组每个元素的第一个值? - Why am I receiving only the first value for each element of the array? 为什么我没有在查询代码中得到所有结果? - Why I am not getting all the result in my query code? 我从数据库 php 中只得到一行 - I am getting only one row from database php 我得到执行查询后没有结果的所选行数? - i am getting the number of row selected after executing the query not the result? SELECT查询仅是选择/检查第一行,我如何选择/检查所有 - SELECT query is only SELECTING/CHECKING the first row, how do I SELECT/CHECK all 如果第一次使用mysqli_fetch_assoc而第二次使用mysqli_fetch_array,为什么我没有得到第一行? - Why I do not get first row if am using mysqli_fetch_assoc on first and mysqli_fetch_array second time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM