php- convert two dimentional array to in single dimentional array -
i reading css folder , received below array.
array ( [formvalidation] => array ( [0] => formvalidation.css ) [0] => bootstrap-theme.css [1] => bootstrap-theme.min.css [2] => bootstrap.css [3] => bootstrap.min.css [4] => component.css [5] => custom.css [6] => custom_11_1_backup.css [7] => datepicker.css [8] => dropkick.css [9] => easy-responsive-tabs.css [10] => jquery.bootstrap-touchspin.css [11] => jquery.fileupload.css [12] => jquery.mcustomscrollbar.css [13] => jquery.noty.css [14] => noty_theme_default.css [15] => owl.carousel.css [16] => owl.theme.css [17] => owl.theme.default.min.css [18] => print_invoice.css [slider] => array ( [0] => ajaxloader.gif [1] => owl.theme.css ) [19] => validationengine.jquery.css )
now want these in below format.
array ( [0] => array ( [path] => css/formvalidation/formvalidation.css [name] => formvalidation.css ) [1] => array ( [path] => css/bootstrap-theme.css [name] => bootstrap-theme.css ) )
i dont know how handle recursive array folders.
ok, here's solution recursive call of function.
code
<?php $data = array( 'formvalidation' => array ( 0 => 'formvalidation.css' ), 0 => 'bootstrap-theme.css', 1 => 'bootstrap-theme.min.css', 2 => 'bootstrap.css', 3 => 'bootstrap.min.css', 4 => 'component.css', 5 => 'custom.css', 'slider' => array ( 0 => 'ajaxloader.gif', 1 => 'owl.theme.css', ), 19 => 'validationengine.jquery.css', ); $initpath = 'css'; echo '<b>input</b><pre>'; var_dump($data); echo '</pre>'; // testing purpose $result = extractstylesheets($data, $initpath); echo '<b>result</b><pre>'; var_dump($result); echo '</pre>'; // testing purpose function extractstylesheets($input,$path='') { $result = array(); $success = true; foreach($input $key => $item) { if (is_array($item)) { // it's array, have sub-level // recursive call of function , merging arrays $result = array_merge( $result, extractstylesheets($item,$path . (empty($path) ? '' : '/') . $key) ); } else { // style sheets if (preg_match('/(.*?\.css)/i', $item, $regs)) { // push sub-array result array $result[] = array( 'path' => $path . '/' . $item, 'name' => $item ); } } } return $result; } ?>
result
array(9) { [0]=> array(2) { ["path"]=> string(37) "css/formvalidation/formvalidation.css" ["name"]=> string(18) "formvalidation.css" } [1]=> array(2) { ["path"]=> string(23) "css/bootstrap-theme.css" ["name"]=> string(19) "bootstrap-theme.css" } [2]=> array(2) { ["path"]=> string(27) "css/bootstrap-theme.min.css" ["name"]=> string(23) "bootstrap-theme.min.css" } [3]=> array(2) { ["path"]=> string(17) "css/bootstrap.css" ["name"]=> string(13) "bootstrap.css" } [4]=> array(2) { ["path"]=> string(21) "css/bootstrap.min.css" ["name"]=> string(17) "bootstrap.min.css" } [5]=> array(2) { ["path"]=> string(17) "css/component.css" ["name"]=> string(13) "component.css" } [6]=> array(2) { ["path"]=> string(14) "css/custom.css" ["name"]=> string(10) "custom.css" } [7]=> array(2) { ["path"]=> string(24) "css/slider/owl.theme.css" ["name"]=> string(13) "owl.theme.css" } [8]=> array(2) { ["path"]=> string(31) "css/validationengine.jquery.css" ["name"]=> string(27) "validationengine.jquery.css" } }
Comments
Post a Comment