Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分解字符串操作_Php_Explode - Fatal编程技术网

Php 分解字符串操作

Php 分解字符串操作,php,explode,Php,Explode,我已经看到了使用“explode”进行字符串操作的示例,但没有一个能胜任我的工作 我有一个分解字符串数组: $grand_result = 12,5,7,3,2,1 ; $grandparent_id = explode(",",$grand_result); 我想在db中插入一些id=$grand\u result的值,但在本例中返回的第一个值(12)除外 $grand\u result可以有许多值,我只想获取前7个值。。 我该怎么做…提前谢谢你 设置爆炸极限 $grand_result

我已经看到了使用“explode”进行字符串操作的示例,但没有一个能胜任我的工作

我有一个分解字符串数组:

$grand_result = 12,5,7,3,2,1 ;
$grandparent_id =  explode(",",$grand_result);
  • 我想在db中插入一些id=
    $grand\u result
    的值,但在本例中返回的第一个值(12)除外

  • $grand\u result
    可以有许多值,我只想获取前7个值。。 我该怎么做…提前谢谢你

    • 设置爆炸极限

      $grand_result = 12,5,7,3,2,1 ;
      $grandparent_id =  explode(",",$grand_result,8);
      
      
      unset($grandparent_id[7]); or $grandparent_id = array_slice($grandparent_id, 0, 7);
      

      可以使用数组\ U切片

      $output = array_slice($grandparent_id, 0, 7);
      
      第一次更新

      $avoid = grandparent_id[0];
      foreach ($grandparent_id as $id) {
          if ($avoid != $id) {
               //run your update query
          }
      }
      
      第二次取回

      $query = "select * from `table_name` where id in (".$grand_result.") limit 7";
      

      您有一个带有分解整数的数组。不要在数据库列中存储逗号分隔的值,请规范化database@MarkBaker此值由存储函数返回…感谢您的关注,您可以使用array_slice获取所需的数组部分。你到底想对数据库做什么?如果你不在它周围加引号,它就不是一个可以分解的字符串。它只是将
      $grand\u result
      设置为
      1
      。应该是
      explode(“,”,$grand\u result,8)-将返回7个元素和字符串的其余部分作为第8个元素。@u_mulder感谢您的更新您在结束分解的“8”之后缺少一个“')”statement@Xatenev谢谢你的更新,所有的…阵列片为我工作。。。
      
      $query = "select * from `table_name` where id in (".$grand_result.") limit 7";