PHP - Multidimensional array to CSV

后端 未结 3 1898
情深已故
情深已故 2020-12-12 03:20

I currently have coded a way to turn a multidimensional array to comma separated values (I\'m using pipes instead of commas for ease of debugging). The problem is, I know th

相关标签:
3条回答
  • 2020-12-12 03:36

    Not sure if this will help you, but would it not be easier to flatten the array first and then format in in the way you want? To flatten the array try this:

    $array = "YOUR ARRAY";
    
    $FlatArray = array();
    
    foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $k=>$v)
      {
        $FlatArray[$k] = $v;
      }
    

    Been trying all morning to come up with a recursive function for this. This is as close as I got, Maybe you can improve upon this.

    $data = array('name' =>array('singular' => NULL,'plural' => NULL,'fields' =>array('price' =>array('label' =>'Preis','company_id' =>array('label' => NULL,'placeholder' => NULL)))));
    
    
    function arr_to_csv($data,$csv = '||')
    {
    $list = "";
    foreach($data as $key => &$value)
            {
            $list .= $key . $csv .((!is_array($value))?(is_null($value)?'null':$value): arr_to_csv($value))."<br>";
            }
    return $list;
    }   
    
    
    
    print_r(arr_to_csv($data));
    

    Returns This:

    name||singular||null
    
    plural||null
    
    fields||price||label||Preis
    
    company_id||label||null
    
    placeholder||null
    
    0 讨论(0)
  • 2020-12-12 03:38

    I didn't check it, so in case it doesn't work it should be corrected.

    function readarray($from_array, $addr = array()) {
        global $output;
        foreach ($from_array as $key => $value) {
            if (is_Array($value) && count($value) > 0) {
                $addr[] = $key;
                readarray($value, $addr);
            } else {
                $output[] = implode('||', $addr) . $value;
            }
        }
    
    }
    
    
    $output = array();
    foreach ($my_array as $key=>$value){
    readarray($value); 
    }
    

    // improved to get separate arrays of the root of initial array

    0 讨论(0)
  • 2020-12-12 03:48

    This is some pseudocode, but it is a start:

    $strings = [];
    $flattenArray = function($arr, $level) use (&$strings, &$flattenArray) {
    
        foreach($arr as $key=>$value){
            $s = &$strings[$level];
            if(!isset($s)) { $s = array(); }
            $s[] = $key;
            if(is_array($value)) {
               $flattenArray($value, $level);
            }
            else {
               $s[] = $value;
            }
            $level ++;
        }
    };
    $flattenArray($myArray, 0);
    foreach($strings as &$arr) {
         $arr = implode("||", $arr);
    }
    

    Small demo with your array: http://codepad.viper-7.com/CR2SPY <-- It does not work fully, but it is a start


    Update:

    Here is a demo that I think works the way you want: http://codepad.viper-7.com/shN4pH

    Code:

    $strings = [];
    $flattenArray = function($arr, $level, $k = null) use (&$strings, &$flattenArray) {
    
        foreach($arr as $key=>$value){
            if($k === null) {
                $s = &$strings[$key];
            }
            else {
                $s = &$strings[$k];
            }
            if(!isset($s)) { 
                $s = array();  
            }
            $str = &$s[$level];
            if(!isset($str)) { 
                $str = array(); 
                
                if($k !== null) { $str[] = $k; }
            }
            $str[] = $key;
            if(is_array($value)) {
               $flattenArray($value, $level, ($k === null) ? $key : $k);
            }
            else {
                $str[] = is_null($value) ? "null" : $value;
            }
            $level ++;
        }
    };
    $flattenArray($myArray, 0);
    $all = [];
    foreach($strings as $k => $arr){
        $new = array();
        foreach($arr as $ky => $ar) {
            $all[] = implode("||", $ar);
        }
    }
    
    print_r($all);
    
    0 讨论(0)
提交回复
热议问题