php 数组只有一个数据转字符串,php – 将数组转换为单个字符串

我有以下数组,我想将它们中的每一个转换为单独的字符串.换句话说,将阵列分成单个部分.

$formatsArray = $_POST['formats'];

$topicsArray = $_POST['topics'];

这是因为我想在以下查询中包含单个字符串“

$resources = "select * from resources where

stage LIKE '%".$stage."%'

AND format LIKE '%".$formats."%'";

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

这是因为格式需要一个单独的字符串进行比较,例如让我们假设数组是[“视频”,“博客”,“文章”],如果要将格式与视频,博客,文章进行比较,它将无效而是视频,博客或文章.

我希望这很清楚,如有任何澄清,请指教.

祝一切顺利,

更新:

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

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

$resources = "select * from resources where

stage LIKE '%".$stage."%'

AND format LIKE '%".$formats."%' AND topic LIKE '%".$topics."%' ";

更新:

$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']

);

}

更新

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']

);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值