Change the form of an array (PHP) -


i have huge, complicated issue in php app. here details:

i have array (var dumped):

array (size=6)   0 => string '11/04/15' (length=8)   1 => string '15/04/15' (length=8)   2 => string '19/04/15' (length=8)   3 => string '1' (length=1)   4 => string '1' (length=1)   5 => string '3' (length=1) 

but want displayed as:

array (size=6)   array(size=2)     string '11/04/15' (length=8)     string '1' (length=1)   array(size=2)     string '15/04/15' (length=8)     string '1' (length=1)   array(size=2)     string '19/04/15' (length=8)     string '3' (length=1) 

as can see add sub arrays, reorder structure , remove keys. starter array values change there more dates/less dates , numbers. linked, key 0 should bulked key 3 , key 1 bulked key 4 etc. think that's enough information.

ps: i'm trying arrange data chart php's bar graph (http://www.chartphp.com/).

this should work you:

since pretty obvious i'm going assume, pattern behind expected results should combine numbers dates sorted asc.

so here first dates array preg_grep(). numbers getting array_diff() original array , dates array.

then sort both arrays asc , $dates array sort usort() compare timestampes strtotime() , $numbers array sort sort().

at end loop through both arrays array_map() create expected result.

<?php      $arr = [             "19/04/15",             "11/04/15",             "15/04/15",             "3",             "1",             "1",         ];      $dates = preg_grep("/\d{2}\/\d{2}\/\d{2}/", $arr);     $numbers = array_diff($arr, $dates);      sort($numbers);     usort($dates, function($a, $b){         if(strtotime($a) == strtotime($b))             return 0;         return strtotime($a) < strtotime($b) ? 1 : -1;     });      $result = array_map(function($v1, $v2){         return [$v1, $v2];     }, $dates, $numbers);      print_r($result);  ?> 

output:

array (     [0] => array         (             [0] => 11/04/15             [1] => 1         )      [1] => array         (             [0] => 15/04/15             [1] => 1         )      [2] => array         (             [0] => 19/04/15             [1] => 3         )  ) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -