2

I have this form of array

Array
    (
        [0] => 8
        [1] => 2
        [2] => 10
        [3] => 11
        [4] => 19
        [5] => 13
        [6] => 10
    )

I want to implode the value to this form [0,8],[1,2],[2,3],[3,11],[4,19],[5,13],[6,10]. Is there any builtin php function to do this?

3
  • 1
    NO, there is not a built in function to perform this task. Commented Oct 9, 2013 at 2:39
  • The output is in a form of a string, right? Commented Oct 9, 2013 at 3:37
  • Why do you believe you need this output string? Are you trying to produce a json string? Transpose the keys and valuea, then json-encode 3v4l.org/isXYZ Commented Oct 5, 2024 at 11:43

4 Answers 4

3
$out = array();
foreach($array as $k => $v) {array_push($out, array($k, $v)); }
Sign up to request clarification or add additional context in comments.

Comments

3
$new_arr = array_map(null, array_keys($arr), $arr);

Comments

1

Something along these lines :

$array = array ( '0' => 8, '1' => 2, '2' => 10, '3' => 11, '4' => 19, '5' => 13, '6' => 10 );

$tempArray = array();
foreach( $array as $key => $value )
{
    $tempArray[] = '[' .$key .',' .$value .']';
}

$imploded = implode( ',' , $tempArray );

echo $imploded;

Output :

[0,8],[1,2],[2,10],[3,11],[4,19],[5,13],[6,10]

Comments

0

You ask about any built in PHP function for your need, then the answer it doesn't ( ref: http://www.php.net/manual/en/ref.array.php ).

Even if it only needs 3 lines of code ...

$result = array();
while (list($key, $value) = each($your_array)) {
    $result[] = array($key, $value);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.