【问题标题】:preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in laravelpreg_replace():参数不匹配,pattern是字符串,而replacement是laravel中的数组
【发布时间】:2023-03-29 17:00:01
【问题描述】:

我通过 JSON 获得 $id 值,当我在 $tracks WHERE 子句中使用 $id 时,它向我显示了这个错误

preg_replace():参数不匹配,pattern是字符串而replacement是数组

显示如下:

Array
    $id = (
    [0] => Array
        (
            [4843] => 4843
            [4844] => 4844


        )

    [1] => Array
        (
            [396] => 396

        )

    [2] => Array
        (
            [3963] => 3963

        )
    )

我的控制器:

$track = Input::get('tracks');
$id = [];
foreach($track as $key => $tracks)
{
  $id[] = json_decode($tracks, true);
}

$tracks = Element::with(
    'composers', 
    'format_mp3s', 
    'loopsets.format_mp3s', 
    'mixes.format_mp3s', 
    'opening_key', 
    'closing_key', 
    'speed', 
    'mood', 
    'metre', 
    'tonic'
)
->where('id', $id)
->orderBy($sortBy, $dir)
->get();

【问题讨论】:

  • 先生,当我在 where 子句中使用 $id 时,它会告诉我这个错误
  • 显示var_dump($track)的输出

标签: php laravel


【解决方案1】:

您在 where 条件中使用 $ids 是关联数组,这就是您收到此错误的原因。

您可以尝试以下代码仅获取ids 数组,然后将它们作为whereIn 条件传递以获得所需的结果

$newIds = [];
  foreach ($ids as $value) {
    foreach($value as $finalValue) {
      $newIds[] = $finalValue;
   }
}

使用whereIn 而不是where

->whereIn('id', $newIds);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2015-06-19
  • 2013-07-11
相关资源
最近更新 更多