Reputation: 5697
How can I convert this Array:
$data = Array
(
[0] => Array
(
[0] => 125
[pcode] => 125
)
[1] => Array
(
[0] => 275
[pcode] => 275
)
[2] => Array
(
[0] => 600
[pcode] => 600
)
[3] => Array
(
[0] => 675
[pcode] => 675
)
[4] => Array
(
[0] => 1031
[pcode] => 1031
)
[5] => Array
(
[0] => C335
[pcode] => C335
)
)
into a string like this:
$new_string=" '125','275','600','675','1031','C335' ";
Upvotes: 0
Views: 65
Reputation: 4241
Really simple:
$tmp=array();
foreach($data as &$element)
{
$tmp[]='\''.addslashes(current($element)).'\'';
}
echo implode(',',$tmp);
You can try it on http://sandbox.onlinephpfunctions.com/code/5c338914de2d627ff39ea7e7dab5a30e060c39f4
Upvotes: 2
Reputation: 272
below code get your output:
<?php
$data = Array
(
1 => Array
(
0 => 125,
'pcode' => 125
),
2 => Array
(
0 => 275,
'pcode' => 275
),
3 => Array
(
0 => 675,
'pcode' => 675
),
4 => Array
(
0 => 675,
'pcode' => 675
),
5 => Array
(
0 => 1031,
'pcode' => 1031
)
);
$i=0;
foreach($data as $k=>$val)
{
if(is_array($val))
{
foreach($val as $k1=>$v1)
{
if(strcmp($k1,'pcode'))
{
if($i==0)
{echo '"'."'".$v1."',";
}
else if($i+1==sizeof($data))
{
echo "'".$v1."'".'"';
}
else
{
echo "'".$v1."',";
}
}
}
}
$i++;
}
?>
Upvotes: 1
Reputation: 695
$data=array(
'1'=> array
(
'0' => 275,
'pcode' => 275
),
'2' => array
(
'0' => 600,
'pcode' => 600
),
'3' => array
(
'0' => 675,
'pcode' => 675
),
'4' => array
(
'0' => 1031,
'pcode' => 1031
),
'5' => array
(
'0' => 'C335',
'pcode' => 'C335'
)
);
$val=implode(',',array_map("FunctionArr",$data));
function FunctionArr($value)
{
return $value[0];
}
echo $val;
Now you can get string according to your need
As var_dump($val)
Upvotes: 0