*** How to combine the arrays into one (query in a loop)?

$keywords array of keywords, MB. any number
$i = 0;
$sql_query = "";
foreach ($keywords as $keyword) {
 $sql_query[$i] = 'SELECT ID FROM' . $wpdb->posts . 'AS pos WHERE pos.post_type="attachment" AND ' . 
 '(pos.post_title LIKE "%' . $keyword . '%" OR pos.post_excerpt LIKE "%' . $keyword . '%")';
 //find the key word in the header or fields
$i++;
}
for($i = 0; $i < count($sql_query); $i++) {
 $imgt_img_id_sqls[$i] = $wpdb->get_results($sql_query[$i]); //Run MySQL
}
$imgt_img_id_sqls = array_merge ($imgt_img_id_sqls[0], $imgt_img_id_sqls[1], $imgt_img_id_sqls[2]);
// don't know how to unite for the General case, so I write for 3: [0] [1] [2]

$imgt_img_id_sqls vugdelic so:
Array
(
 [0] => stdClass Object
(
 [ID] => 483
)

 [1] => stdClass Object
(
 [ID] => 482
)

 [2] => stdClass Object
(
 [ID] => 485
)
)

1 question: How to combine the arrays into 1 in the General case for any $i?
2 question: I Have a query in a loop - is this permissible, if not - teach how to do
July 12th 19 at 17:23
2 answers
July 12th 19 at 17:25
Solution
Something like this (1 question):
$result = [];
for($i = 0; $i < count($sql_query); $i++) {
 $query_result = $wpdb->get_results($sql_query[$i]);
 $result = array_merge($result, $query_result); //Run MySQL
}

P. S. you Can do is for you to use foreach or count($sql_query) to make a variable before the loop

Second question:
Better in the cycle does not execute the query for your case would do well to tie shpinx or lucene.
and how, conversely, does not the Union and intersection of arrays to record? using array_intersect_key does not work - cristal_Littel commented on July 12th 19 at 17:28
you need to get the id of the posts which have all the keywords? - collin.Kuhic commented on July 12th 19 at 17:31
July 12th 19 at 17:27
Solution
Requests in a cycle to perform in any case not necessary, it is possible to generate a query something like this:

$keywords = [
'key1',
'key2'
];

$whereStatement = "";
array_walk($keywords, function($keyword) use (&$whereStatement){
 $whereStatement .= "OR (post.post_title LIKE '%". $keyword ."%' OR post.post_excerpt LIKE '%". $keyword ."%')";
});
$whereStatement = trim($whereStatement, ' OR ');

$sql = "
SELECT
ID
FROM
 ". $this->posts . "AS post
WHERE
post.post_type='attachment'
 AND (
 ". $whereStatement . "
)
";

echo "<pre>", print_r($sql, true), "</pre>";
how is that better? explain how for a newbie, thanks - cristal_Littel commented on July 12th 19 at 17:30
: better queries in a loop? The fact that any DB queries -- this overhead. Two to five simple query in a loop may be it will be faster than a single "thick", but with the increase of number of queries will grow as the total execution time and maybe the memory consumption. There is still such thing as an implicit table lock when reading/writing.

Generally can distribute a small benchmark (queries in a loop and one request) and see the results. - collin.Kuhic commented on July 12th 19 at 17:33

Find more questions by tags PHPMySQL