2

i have array chunk,

Array(
[0] => Array
    (
        [0] => "0"
        [1] => "1"
        [2] => "2"
    )
[1] => Array
    (
        [3] => "3"
        [4] => "4"
        [5] => "5"
    )
[2] => Array
    (
        [5] => "5"
        [6] => "6"
        [7] => "7"
    )

)

i want to implode automatic this array with looping, and get format

012|345|567
1
  • 1
    You want to get the string "012|345|567" from this array? Commented Apr 5, 2014 at 1:01

3 Answers 3

3

Try using implode + array_map:

$array = array ( array(1,2,3), array(4,5,6), array(7,8,9));
$array = implode('|', array_map('implode', $array));
print_r($array);
Sign up to request clarification or add additional context in comments.

2 Comments

@Chielarck You are welcome. Always help future visitors by selecting the answer you like best as the accepted answer.
I needed a variant of this where I wanted to implode but also include a separator; I don't think you can use array_map because you can't pass the separator parameter. I used array_walk($arr, 'apply_implode', ','); with function apply_implode(&$element, $key, $separator){ $element = implode($separator, $element);} and note that array_walk does NOT RETURN an array like array_map does
0

You can try it.

function implode_array($param = array()) {
  $result = '';
  foreach ($param as $p1_key => $p1_value) {
    $result .= implode("", $p1_value);
    if ($p1_key < count($param))
      $result .= '|';
  }
  return $result;
}

Comments

0

Try following code,

$array = Array(
[0] => Array
(
    [0] => "0"
    [1] => "1"
    [2] => "2"
)
[1] => Array
(
    [3] => "3"
    [4] => "4"
    [5] => "5"
)
[2] => Array
(
    [5] => "5"
    [6] => "6"
    [7] => "7"
)

);

foreach($array as $key => $value) {
     $keys[] = implode("",array_keys($value));
}
$result = implode('|', $keys);

Thanks!

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.