array of comma separated string IN in clause mysql - php

I have a array like that :
$myarray= 'zzz,aaa,bbb' ;
and want use it in mysql IN clause like that :
"... WHERE ids IN ($myarray) " // didnt work
"... WHERE ids IN ('$myarray') " // didnt work
the error im getting is that the first value in that array zzz says that zzz is not a column name . so i understand that i must separate the values with quotes to be like that :
$myarray= ' "zzz","aaa","bbb" ' ;
But i have no clue to do that . any help would be much appreciated.

$myarray = 'zzz,aaa,bbb';
$myarray = implode("','",explode(',',$myarray));
$query = "..... WHERE ids IN ('$myarray')";

You need to explode() your string and then implode() it -
$myarray = 'zzz,aaa,bbb';
$realArray = explode(',', $myarray);
$stringForIn = "'" . implode("','", $realArray) . "'";
echo "WHERE ids IN ($stringForIn)";

Try that :
$myarray= 'zzz,aaa,bbb' ;
echo implode '"' . ('","', explode(',', $myarray)) . '"';

Related

implode and show with key - php

I have an array with just a single element. The key and value looks like this:
Array ( [test] => 12342 )
Result i want is this inside an variable:
test = '12342'
I tried following:
$test = "'" . implode(" ", $metric) . "'";
print_r($test);
But this gives only gives me: '12342', i wanted the '=' after key and '' around the value (this is used for SQL later on). So the result should be test = '12342'. Does not look like implode would work here. I tried looking at http_query_builder but failed.
If this array is only ever going to contain one single item, then you don’t need to loop through the data, but you can use key and current instead:
$data = ['test' => 12342];
echo key($data) . " = '" . current($data) . "'";
key gets you the key of the “current” item, and current gets its value.
By just replying your question as it is using that unique example you would do it this way assuming there is only 1 value inside your array :
$yourArray = array('test' => '12342');
foreach($yourArray as $key => $value) {
$test = $key . " = '" . $value . "'";
}
print_r($test);
If there are multiple you would do this, as each key is unique :
$yourArray = array('test' => '12342', 'testing' => '24321');
$test = array();
foreach($yourArray as $key => $value) {
$test[$key] = $key . " = '" . $value . "'";
}
var_dump($test);

Give double quotes and comma separators in each array

this is my code :
<?php
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
$arr = array();
while ( $tam = mysqli_fetch_array($qqq)) {
$date = $tam['tanggal_awal'];
$reformat_date = date("n-j-Y", strtotime($date));
$arr[] = $reformat_date;
}
$array_date = implode(",", $arr);
?>
> output : 8-9-2018,8-10-2018,8-17-2018
> output i want to is : "8-9-2018", "8-10-2018", "8-17-2018"
could someone help me to get the output i want, thank before.
You can make your code a bit shorter:
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
while ( $tam = mysqli_fetch_array($qqq)) {
$arr[] = date("n-j-Y", strtotime($tam['tanggal_awal']));
}
$array_date = '"' . implode('", ', $arr) . '"';
In php you don't need to declare an array before pushing values to it.
Your $date variable is not needed as it's just a copy of the $tam['tanggal_awal'] value.
$reformat_date is also not needed as you can place it in the array directly.
The imploding is what is placed between the items, by having ", there you just need to add a " at each end of the string.
$arr=['8-9-2018','8-10-2018','8-17-2018'];
$array_date = '"'.implode('", "', $arr) . '"';
echo $array_date;
Output ---> "8-9-2018", "8-10-2018", "8-17-2018"
Try to implode your array like this.

Issue with Array and foreach

Hi I have this array called $allextensionsforque. See print_r below.
Array ( [0] => Local/101#from-queue/n,0 [data] => Local/101#from-queue/n,0 )
I'm trying a foreach like below but its not showing any data on the echo. Can anyone help?
$allextensionsforqueu = mysqli_query($conqueue,"SELECT data FROM queues_details WHERE id = '1' AND keyword ='member'");
$allextensionsforque = mysqli_fetch_array($allextensionsforqueu);
$foo = "";
foreach ($allextensionsforque as $extensionrow)
{
$extensionrowstriped = substr($extensionrow['data'],6,3);
$foo = "' " . $extensionrowstriped . " ' ,";
}
echo $foo;
You don't have an array in $extensionrow, since it's only an one dimensional array. So you just need to replace $extensionrowstriped = substr($extensionrow['data'],6,3); with $extensionrowstriped = substr($extensionrow,6,3);.
Check for errors using mysqli_error(). Here is a simple approach, but you may want to improve it for production use:
$allextensionsforqueu = mysqli_query($conqueue,"...") or die(mysqli_error($conqueue));
Next, append your data to $foo instead of overwritting it in each loop:
$foo .= "' " . $extensionrowstriped . " ' ,";
Finally, verify that $extensionrow contains the data that you expect. substr() will return false if a match is not found.

Ignore the last object in array in PHP

I have this little code here :
$arrayName = array('0','1','2');
foreach ($arrayName as $key) {
echo $key . ',';
}
So now the Output is this :
0,1,2,
If i want to have this what should i do ? :
0,1,2
I mean not inserting "," for last object in an array .
thanks .
You can either use join or implode
echo join(',', $arrayName);
OR
echo implode(',', $arrayName);
Do this way:
echo join(',', $arrayName);
You should not use loop with this (i.e. place this instead of loop, not inside loop)
Try to implode them like
$arrayName = array('0','1','2');
echo implode(',',$arrayName);

Converting a php array to SQL array

I want something like the following:
$arrayOfValues = array(1,2,3,4);
$sqlArray = mysql_convertToSqlArray($arrayOfValues);
which then will return what in SQL would be:
(1,2,3,4)
but in php would be the string "(1,2,3,4)"
There's no builtin function specifically for creating SQL arrays, but you could just join the array and wrap it in parentheses:
$arrayOfValues = array(1,2,3,4);
$sqlArray = '(' . join(',', $arrayOfValues) . ')';
See this in action at http://www.ideone.com/KYApN.
Take a look at http://www.php.net/manual/en/function.implode.php.
This function can be used as following: $sqlArray = "(" . implode(",", $arrayOfValues) . ")";
[Edit]
Ps: join is an alias of implode.

Categories