配列内の特定のキーワードを検索するときに問題が発生しています。私はLaravelを使用しています。array_search()は第2パラメータにエラーをスローする
Twitterの機能とまったく同じ特定のキーワードを持つ特定の投稿を検索しようとしています。 www.foobar.com/shoutouts/$hashtag次の関数を訪問が実行される場合の
:
public function shoutoutSpecific($hashtag) {
$hashtag = '#' . $hashtag;
$shoutouts = Shoutout::orderBy('created_at', 'DESC')->get();
foreach($shoutouts as $shoutout) {
if (array_search($hashtag, $shoutout->hashtag) !== false) {
$shoutouts2 = $shoutouts2 . $shoutout;
}
}
return view('shoutout', compact('shoutouts2'));
}
データベーステーブルshoutout-用>ハッシュタグだから
a:1:{i:0;s:5:"#test";}
ときは、次の例が含まれていますwww.foobar.com/shoutouts/testにアクセスすると、関数は#testというハッシュタグを持つ特定のshoutoutsを見つけ、配列内にループを投げてビューにループさせることができます。私はwww.foobar.com/shoutouts/testを訪問したときに
はしかし、私は
array_search() expects parameter 2 to be array, string given
を取得し、2番目のパラメータは、データベース内の配列であるので、私は、これを取得いけません。
すべてのお役に立ちました。だから、最初のあなたは、すなわちunserialize($string)
機能を使用して、それをデシリアライズする必要が
array(1) {
[0]=> string(5) "#test"
}
:
。あなたは文字列を渡した。 'array_search()'は与えられた文字列の配列の値を検索します。 – Jaime