0

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' ";

1
  • I'm assuming this is PHP considering the example: implode(',', array_map(function ($item) { return "'" . $item[0] . "'"; }, $data));. Example here. Commented May 26, 2015 at 10:18

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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++;
}
?>

Comments

0
$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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.