user3386779
user3386779

Reputation: 7215

how to get the value from associative array

I have created an array using array_push in php and I got

[0] => nithya.sreeraman 
[1] => jaivignesh.parthiban 
[2] => aadhil.ahmed 
[3] => aarthe.sathyanaraya 
[4] => abdulamjad.babu 
[5] => khaja.hussain 
[6] => abdulwahab.mohamed 
[7] => abdullasha.pari 
[8] => abinaya.sambandam 
[9] => abinesh.pugazhendhi

I want to collect the value as

('nithya.sreeraman','jaivignesh.parthiban','aadhil.ahmed',etc)

Is it possible in php I wnt to collect this value for where column in QUERY

Upvotes: 0

Views: 99

Answers (5)

sunilwananje
sunilwananje

Reputation: 724

you can directly use implode function to convert comma separated string

$final_output = "('" . implode("','", $your_input_array) . "')";

Upvotes: 0

Jems
Jems

Reputation: 1706

Just use php join

$array = array_map(function($x) { return "'" . $x . "'"; }, $array);
echo "(" . join(',', $array) . ")";

Upvotes: 0

Salvatore
Salvatore

Reputation: 1435

You just need to run a foreach loop to get the values -

$name = array([0] => nithya.sreeraman [1] => jaivignesh.parthiban [2] => aadhil.ahmed 
[3] => aarthe.sathyanaraya [4] => abdulamjad.babu [5] => khaja.hussain 
[6] => abdulwahab.mohamed [7] => abdullasha.pari [8] => abinaya.sambandam 
[9] => abinesh.pugazhendhi);

$i = 0;
// Loop through assoc name array
foreach($name as $key => $value){
    $arrName[$i] = $value;
    $i++;
}

Your names will be stored in $arrName.

I hope this is what you were looking for :)

Upvotes: -1

MrSmile
MrSmile

Reputation: 1233

$array = array_map(function($v) { return "'{$v}'"; }, $array);
echo "(" . implode(",", $array) . ")";

Upvotes: 3

Jagjeet Singh
Jagjeet Singh

Reputation: 1572

Use implode by comma and str_replace

$arr = [0 => 'nithya.sreeraman', 1 => 'jaivignesh.parthiban'];
$string = "('". str_replace(",", "','", implode(',', $arr))."')";

Upvotes: 1

Related Questions