1

EDIT: Here is a portion of $preparedstring:

555555,Jones,Brian,NYC,1000,2011-10-21 00:00:00,Check,1542,0, ,Check, ,0, ,Check, ,; 6666666,Miler,Christopher,Chicago,1000,2011-10-26 00:00:00,Check,6406,0, ,Check, ,0, ,Check, ,;

I am trying to convert a HTML table to a multidimensional array. I have converted the table into a long string, each cell being delimited with a comma and each row being delimited with a semicolon.

I am not exactly sure how to build the multidimensional array from this string. This is what I have tried so far:

<?php    
$outerARR = explode(";", $preparedstring);

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

this gives me a syntax error near the

$arr = array

(

opening parenthesis.

2
  • show a little data to understand what to parse Commented Dec 14, 2011 at 17:37
  • There's a lot of syntax errors, show us the content of your $preparedstring Commented Dec 14, 2011 at 17:39

4 Answers 4

6

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, I'll add a snippet showing how to do it in version < 5.3
2
<?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;
}
?>

Comments

2

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

Comments

0
$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] => 
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.