转换数组为单独的字符串 [英] convert an array into individual strings

查看:127
本文介绍了转换数组为单独的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下阵列,我想他们中的每一个转换成单独的字符串。换句话说,打破阵列成​​单独的块。

  $ formatsArray = $ _ POST ['格式'];
      $ topicsArray = $ _ POST ['主题'];

这是因为我想包括在下面的查询个人字符串

  $资源=从资源投入到选择*
                    舞台LIKE'%。$阶段。%
                    和格式LIKE'%%$格式。';
      $ run_query = mysqli_query($ CON,$资源);

这是因为格式的期望相比个人字符串,如让我们假设数组是 [视频,博客,公司章程] ,它不会告发'T工作,如果格式是与视频,博客,文章而是视频,博客或文章进行比较。

我希望这是明确的,对于任何澄清,请大家指教。

所有最优秀的,

更新:

  $格式=爆炸('',$ formatsArray);
      $主题=爆炸(,,$ topicsArray);
      $资源=从资源投入到选择*
                    舞台LIKE'%。$阶段。%
                    和格式LIKE'%%$格式。主题LIKE'%%$主题。';

更新:

  $ run_query = mysqli_query($ CON,$资源);
  而($行= mysqli_fetch_array($ run_query)){    $数据[] =数组(
      '格式'=> $行['格式化'],
      '标题'=> $行['标题'],
      '成本'=> $行['成本'],
      舞台= GT; $行['舞台'],
      主题= GT; $行['主题'],
      '链接'=> $行['链接']
    );
  }

更新

 包括('db.php中');
  $查询=选择资源,其中*';
  。$查询='阶段,如:舞台';
  $执行[':舞台'] ='%'。 $阶段。 %;
  如果(!空($ _ POST ['格式'])){
  的foreach($ _ POST ['格式']为$关键=> $格式){
      。$查询='格式,如:格式。 $键。 '与';
      $执行[':格式。 $关键] ='%'。修剪($格式)。 %;
  }
  }
  如果(!空($ _ POST ['主题'])){
  的foreach($ _ POST ['主题']为$关键=> $主题){
      $查询='的话题,如:主题。 $键。 '与';
      $执行[:主题。 $关键] ='%'。修剪($主题)。 %;
  }
  }
  $查询= RTRIM($查询和);
  如果(!空($执行)){
      $语句= $ CON-> prepare($查询);
      $ stmt->执行($执行);
  }其他{
      回声你必须寻找的东西;
  }
      而($行= mysqli_fetch_array($查询)){        $数据[] =数组(
          '格式'=> $行['格式化'],
          '标题'=> $行['标题'],
          '成本'=> $行['成本'],
          舞台= GT; $行['舞台'],
          主题= GT; $行['主题'],
          '链接'=> $行['链接']
        );
      }


解决方案

我认为这将做到这一点。这也将使用prepared报表解决的喷射孔。

  $查询=从资源选择*其中;
如果(!空($ _ POST ['格式'])){
的foreach($ _ POST ['格式']为$关键=> $格式){
    $查询='阶段,如:阶段。 $键。 ' 要么 ';
    $执行[':舞台。 $关键] ='%'。修剪($格式)。 %;
}
}
如果(!空($ _ POST ['主题'])){
的foreach($ _ POST ['主题']为$关键=> $主题){
    $查询='的话题,如:主题。 $键。 ' 要么 ';
    $执行[:主题。 $关键] ='%'。修剪($主题)。 %;
}
}
$查询= RTRIM($查询,或);
如果(!空($执行)){
    回声$查询;
    的print_r($执行);
    // $语句= $ mysqli-> prepare($查询);
    // $ stmt->执行($执行);
}其他{
    回声你必须寻找的东西;
}

为您提供了一个查询


  

SELECT * FROM资源,其中阶段,如:STAGE0或阶段,如:阶段1或主题,如:topic0或主题,如:TOPIC1或主题,如:TOPIC2或主题,如:topic3​​


和束缚值:

 阵列

    [:STAGE0] => %测试%
    [:阶段1] => %测试1%
    [:topic0] => %值1%
    [:TOPIC1] => %值2%
    [:TOPIC2] => %值3%
    [:topic3​​] => %值4%

下面是初始code我有,当我以为数据是成对的。

 的foreach($格式为$关键=> $格式){
    $主题= $主题[$关键];
    。$查询='(LIKE阶段:阶段'。$键主题,如:主题$关键。)或;
    $执行[':舞台。 $关键] ='%'。修剪($格式)。 %;
    $执行[:主题。 $关键] ='%'。修剪($主题)。 %;
}

在$ P $几个环节ppared声明:结果
http://php.net/manual/en/mysqli。快速启动。prepared-statements.php 结果
http://php.net/manual/en/mysqli.$p$ppare .PHP 结果
http://php.net/manual/en/mysqli-stmt.execute.php

I have the following arrays and I would like to convert each one of them into individual strings. In other words, break the array into individual pieces.

  $formatsArray = $_POST['formats'];
      $topicsArray = $_POST['topics'];

This is because I would like to include the individual strings in the following query "

  $resources = "select * from resources where
                    stage LIKE '%".$stage."%'
                    AND format LIKE '%".$formats."%'";


      $run_query = mysqli_query($con, $resources);

This is because format expect an individual string for comparison, such as lets assume the array is ["video", "blogs", "articles"], it wouldn't work if format was to be compared with video,blogs,articles but rather video, blogs or articles.

I hope this is clear, and for any clarification, please advise.

All the best,

Update:

$formats = explode(',', $formatsArray);
      $topics = explode(',', $topicsArray);


      $resources = "select * from resources where
                    stage LIKE '%".$stage."%'
                    AND format LIKE '%".$formats."%' AND topic LIKE '%".$topics."%' ";

update:

$run_query = mysqli_query($con, $resources);


  while($row = mysqli_fetch_array($run_query)) {

    $data[] = array(
      'format' => $row['format'],
      'title' => $row['title'],
      'costs' => $row['cost'],
      'stage' => $row['stage'],
      'topic' => $row['topic'],
      'link' => $row['link']
    );
  }

Update

  include('db.php');


  $query = 'select * from resources where ';
  $query .= 'stage LIKE :stage and';
  $execute[':stage'] = '%' . $stage . '%';
  if(!empty($_POST['formats'])){
  foreach($_POST['formats'] as $key => $format) {
      $query .= 'format LIKE :format' . $key . ' and ';
      $execute[':format' . $key] = '%' . trim($format) . '%';
  }
  }
  if(!empty($_POST['topics'])){
  foreach($_POST['topics'] as $key => $topic) {
      $query .= 'topic LIKE :topic' . $key . ' and ';
      $execute[':topic' . $key] = '%' . trim($topic)  . '%';
  }
  }
  $query = rtrim($query, ' and ');
  if(!empty($execute)) {
      $stmt = $con->prepare($query);
      $stmt->execute($execute);
  } else {
      echo 'You must search for something';
  }


      while($row = mysqli_fetch_array($query)) {

        $data[] = array(
          'format' => $row['format'],
          'title' => $row['title'],
          'costs' => $row['cost'],
          'stage' => $row['stage'],
          'topic' => $row['topic'],
          'link' => $row['link']
        );
      }

解决方案

I think this would do it. This also will resolve the injection hole by using prepared statements.

$query = 'select * from resources where ';
if(!empty($_POST['formats'])){ 
foreach($_POST['formats'] as $key => $format) {
    $query .= 'stage LIKE :stage' . $key . ' or ';
    $execute[':stage' . $key] = '%' . trim($format) . '%';
}
}
if(!empty($_POST['topics'])){
foreach($_POST['topics'] as $key => $topic) {
    $query .= 'topic LIKE :topic' . $key . ' or ';
    $execute[':topic' . $key] = '%' . trim($topic)  . '%';
}
}
$query = rtrim($query, ' or ');
if(!empty($execute)) {
    echo $query;
    print_r($execute);
    //$stmt = $mysqli->prepare($query);
    //$stmt->execute($execute);
} else {
    echo 'You must search for something';
}

Gives you a query of

select * from resources where stage LIKE :stage0 or stage LIKE :stage1 or topic LIKE :topic0 or topic LIKE :topic1 or topic LIKE :topic2 or topic LIKE :topic3

and bound values of:

Array
(
    [:stage0] => %test%
    [:stage1] => %test1%
    [:topic0] => %value1%
    [:topic1] => %value2%
    [:topic2] => %value3%
    [:topic3] => %value4%
)

Here's the initial code I had for when I thought the data was paired..

foreach($formats as $key => $format) {
    $topic = $topics[$key];
    $query .= '(stage LIKE :stage' . $key . ' and topic LIKE :topic' . $key . ') or ';
    $execute[':stage' . $key] = '%' . trim($format) . '%';
    $execute[':topic' . $key] = '%' . trim($topic)  . '%';
}

A few links on prepared statements:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
http://php.net/manual/en/mysqli.prepare.php
http://php.net/manual/en/mysqli-stmt.execute.php

这篇关于转换数组为单独的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆