build multidimensional array from string php

前端 未结 4 1474
我在风中等你
我在风中等你 2021-01-03 13:20

EDIT: Here is a portion of $preparedstring:

555555,Jones,Brian,NYC,1000,2011-10-21 00:00:00,Check,1542,0, ,Check, ,0, ,Check, ,; 666666

相关标签:
4条回答
  • 2021-01-03 13:53

    Your approach to solving the problem is sadly very wrong, though there are many solutions to your problem, I would use something like the below.


    How does the code work?

    First we use explode to split our string up in smaller chunks, ; is our delimiter.

    We pass this newly created array to array_map as it's second parameter.

    array_map takes two parameters, the first one is a function that will be called for every member of the second paramater (which should be an array).

    Inside our callback to array_map we use explode to once again split out the values, now with , as our delimiter.


    $data = "1,2,3;4,5,6;7,8,9";
    
    $ret = array_map (
      function ($_) {return explode (',', $_);},
      explode (';', $data)
    );
    
    print_r ($ret);
    

    output

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

    It doesn't work, why?

    Probably because you are using a version of PHP prior to 5.3, if so you can use this snippet instead:

    function explode_by_comma ($_) {
      return explode (',', $_);
    }
    
    $ret = array_map ('explode_by_comma', explode (';', $data));
    
    0 讨论(0)
  • 2021-01-03 13:54
    <?php
    
    //explode first dimension of the array to create an array of rows
    $outerARR = explode(";", $preparedstring);
    
    $arr = array();
    
    //iterate through the newly created array
    foreach ($outerARR as $arrvalue) {
    
        //explode this row into columns
        $innerarr = explode(",", $arrvalue);
    
        //add the newly created array of columns to the output array as a new index
        $arr[] = $innerarr;
    }
    ?>
    
    0 讨论(0)
  • 2021-01-03 13:59

    You're close, but arrays don't work that way. You can't put a foreach inside an array constructor like that. It should look like this:

    $outerARR = explode(";", $preparedstring);
    $arr = array();
    foreach ($outerARR as $arrvalue){
       $innerarr = explode(",", $arrvalue);
       $arr[] = $innerarr;
    }
    

    Demo: http://codepad.org/I5wFFczR

    0 讨论(0)
  • 2021-01-03 14:04
    $outerARR = explode(";", $preparedstring);
    $a = array();
    $y=0;
    foreach ($outerARR as $arrvalue){
        $x=0;
        $innerarr = explode(",", $arrvalue);
        foreach($innerarr as $v){
            $a[$y][$x++] = $v;
        }
        $y++;
    }
    print_r($a);
    
    
    
    Array
    (
        [0] => Array
            (
                [0] => 555555
                [1] => Jones
                [2] => Brian
                [3] => NYC
                [4] => 1000
                [5] => 2011-10-21 00:00:00
                [6] => Check
                [7] => 1542
                [8] => 0
                [9] =>  
                [10] => Check
                [11] =>  
                [12] => 0
                [13] =>  
                [14] => Check
                [15] =>  
                [16] => 
            )
    
        [1] => Array
            (
                [0] =>  6666666
                [1] => Miler
                [2] => Christopher
                [3] => Chicago
                [4] => 1000
                [5] => 2011-10-26 00:00:00
                [6] => Check
                [7] => 6406
                [8] => 0
                [9] =>  
                [10] => Check
                [11] =>  
                [12] => 0
                [13] =>  
                [14] => Check
                [15] =>  
                [16] => 
            )
    
    )
    
    0 讨论(0)
提交回复
热议问题