Combine strings in multidimensional array and keep key - PHP -


i have array:

array(1) {   ["comments"]=>   array(244) {     [113]=>       array(2) {       [0]=>       string(40) "2016-07-15 09:27 | on track done"       [1]=>       string(40) "2016-07-15 09:56 | done on track"      }   } } 

and want make array:

array(1) {   ["comments"]=>     array(244) {     [113]=>       array(1) {       [0]=>       string(80) "2016-07-15 09:27 | on track done        2016-07-15 09:56 | done on track"       }    } } 

i need keep key [113] , merge values. need comments in 1 array. in example there 2 comments, more.

i tried array_merge, array_value , loop. couldnt it.

a more general algorithm :

<?php // sample data. $arr = array( "comments" => array( 0 => array( "2016-07-15 09:27 | on track done",                                                "2016-07-15 09:56 | done on track"                                              ),                                    1 => array( "111 | aaa",                                                "222 | bbb",                                                "333 | ccc"                                              )                                  ),               "posts"    => array( 0 => array( "2016-07-13 08:00 | bla bla bla",                                                "2016-07-13 08:30 | more bla bla bla"                                              ),                                    1 => array( "444 | xxx",                                                "555 | yyy",                                                "666 | zzz"                                              )                                  )             ); var_dump( $arr ); // original array. $new = array(); foreach ( $arr $key => $category ) // "comments", "posts", ... { ( $i = 0; $i < count( $category ); $i++ ) // 0, 1, ...   { $values = implode( $category[ $i ]," " ); // merge strings.     $new[ $key ][ $i ] = array( $values );//we can remove "array" , leave "$values".   } } var_dump( $new ); // new array. ?> 

result :

original array

array (size=2)   'comments' =>      array (size=2)       0 =>          array (size=2)           0 => string '2016-07-15 09:27 | on track done' (length=40)           1 => string '2016-07-15 09:56 | done on track' (length=40)       1 =>          array (size=3)           0 => string '111 | aaa' (length=9)           1 => string '222 | bbb' (length=9)           2 => string '333 | ccc' (length=9)   'posts' =>      array (size=2)       0 =>          array (size=2)           0 => string '2016-07-13 08:00 | bla bla bla' (length=30)           1 => string '2016-07-13 08:30 | more bla bla bla' (length=35)       1 =>          array (size=3)           0 => string '444 | xxx' (length=9)           1 => string '555 | yyy' (length=9)           2 => string '666 | zzz' (length=9) 

new array

array (size=2)   'comments' =>      array (size=2)       0 =>          array (size=1)           0 => string '2016-07-15 09:27 | on track done 2016-07-15 09:56 | done on track' (length=81)       1 =>          array (size=1)           0 => string '111 | aaa 222 | bbb 333 | ccc' (length=29)   'posts' =>      array (size=2)       0 =>          array (size=1)           0 => string '2016-07-13 08:00 | bla bla bla 2016-07-13 08:30 | more bla bla bla' (length=66)       1 =>          array (size=1)           0 => string '444 | xxx 555 | yyy 666 | zzz' (length=29) 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -