hatched
hatched

Reputation: 865

PHP - How do I join array elements,adding values before and after using implode()?

I suppose to get a string from my database that looks like that:

6841819,6595747,6597673,6696253,6616167,6616197,6611931,6600475,6760303,6748661

Then I need to separate the string using explode() , and to implode again to build a sql query string.
There's my array like this after explode() :

Array
(
    [0] => 6841819
    [1] => 6595747
    [2] => 6597673
    [3] => 6696253
    [4] => 6616167
    [5] => 6616197
    [6] => 6611931
    [7] => 6600475
    [8] => 6760303
    [9] => 6748661
)

And my codes:

$sql = "select * from table where id like %0001% or id like %";
$explode = explode(",", $array);
$implode = implode(" or id like %", $explode);
$sql.= $implodevno."<br>";
echo $sql;

And the output sql query which is incorrect because it missed a '%' at the end of each value:

select * from table where id like %0001% or id like %6841819 or id like %6595747 or id like %6597673 or id like %6696253 or id like %6616167 or id like %6616197 or id like %6611931 or id like %6600475 or id like %6760303 or id like %6748661

I can't find solutions to add a '%' behind using implode(), any solutions or better way to do this ?
The correct output should be:

select * from table where id like %0001% or id like %6841819% or id like %6595747% or id like %6597673% or id like %6696253% or id like %6616167% or id like %6616197% or id like %6611931% or id like %6600475% or id like %6760303% or id like %6748661%

Upvotes: 0

Views: 1320

Answers (4)

Sagar Bahadur Tamang
Sagar Bahadur Tamang

Reputation: 2709

Try this

$sql = "select * from table where id like %0001% or id like %";

$n = "6841819,6595747,6597673,6696253,6616167,6616197,6611931,6600475,6760303,6748661";
$n_arr = explode(",", $n);
$n_map = array_map(function($v){ return '%'.$v.'%'; }, $n_arr);
$implode = implode(" or id like ", $n_map);
$sql.= $implode."<br>";
echo $sql;

Upvotes: 1

Jonathon
Jonathon

Reputation: 16283

To get your desired result from your current code you could use array_map to add a % before and after each item in your array and then use implode to join them all together:

$sql = "select * from table where id like %0001% or id like %";
$explode = explode(",", $array);
$implode = implode(" or id like ", array_map(function ($item) {
    return '%' . $item . '%';
}, $explode));
$sql.= $implodevno."<br>";
echo $sql;

Take a look a the documentation for array_map here.

It is probably worth mentioning that when doing a LIKE match in MySQL, you should enclose your values within quotes. The array_map function can help you there too. You just need to return '\'%' . $item . '\'%' or "'%$item%'" from the closure.

There are of course more elegant solutions to what you're doing as a whole but that should help you achieve what you're after for now.

Upvotes: 3

Nageen
Nageen

Reputation: 1759

Simple foreach loop :-

$sql = "select * from table where id like %0001% or ";
$array = Array
        (
            '0' => 6841819,
            '1' => 6595747,
            '2' => 6597673,
            '3' => 6696253,
            '4' => 6616167,
            '5' => 6616197,
            '6' => 6611931,
            '7' => 6600475,
            '8' => 6760303,
            '9' => 6748661,
        );
foreach ($array as $key => $value) {

    $sql .= "id like '%".$value."%' or ";
}
$sql = rtrim($sql,'or ');
echo $sql;

output :-

select * from table where id like %0001% or id like '%6841819%' or id like '%6595747%' or id like '%6597673%' or id like '%6696253%' or id like '%6616167%' or id like '%6616197%' or id like '%6611931%' or id like '%6600475%' or id like '%6760303%' or id like '%6748661%'

Upvotes: 3

MrSmile
MrSmile

Reputation: 1233

If you want to do it exactly as you do, try this:

$array = "6841819,6595747,6597673,6696253,6616167,6616197,6611931,6600475,6760303,6748661";

$sql = "select * FROM `table` WHERE `id` LIKE '%0001%' or `id` LIKE ";

$explode_array = explode(",", $array);
$explode_array = array_map(function($v){return "'%{$v}%'";}, $explode_array);

$implode_str = implode(" or `id` LIKE ", $explode_array);

$sql.= $implode_str."<br>";

echo $sql;

output is:

select * from table where id like %0001% or id like '%6841819%' or id like '%6595747%' or id like '%6597673%' or id like '%6696253%' or id like '%6616167%' or id like '%6616197%' or id like '%6611931%' or id like '%6600475%' or id like '%6760303%' or id like '%6748661%'

Upvotes: 2

Related Questions