trasform an array in multidimensional array (second question)

hi EE

i have an array like this

Array
(
    [0] => 1302                      11022009                      132V4
    [1] => 1302            11022009            132V4
}

can i trasform this array into this?

Array
(
    [0] => Array
              (
                 [0] =>1302
                 [1] =>11022009
                 [2] =>132V4
              )
    [1] => Array
              (
                 [0] =>1302
                 [1] =>11022009
                 [2] =>132V4
              )
}

Array
(
    [0] => 1302 11022009 132V4
    [1] => 1302 11022009 132V4
}

[0] => Array


My problem is :
i have some blank space into original array
denny3dAsked:
Who is Participating?
 
agamalCommented:
Try this ... I added a check if the value is "" or " " i mean empty or space


if still appears remove the // from

// unset($testarray[$i]);

<?php
//building your array
$testarray[0]= "1302                      11022009                      132V4";
$testarray[1]= "1302            11022009            132V4                          11022009            132V4";
$testarray[2]= "1302";
$testarray[3]= "";
 
//checking the old value
print_r($testarray);
echo "<br>";
 
//converting it
for ($i=0;$i<count($testarray);$i++) {
$new = preg_split("/[\s,]+/",$testarray[$i]);
$new = array_diff($new, array("", " "));
if (count($new) == 0) {
// unset($testarray[$i]);
 }else {
 $testarray[$i]= $new;
}
}
//checking the old value
print_r($testarray);
echo "<br>";
 
?>

Open in new window

0
 
denny3dAuthor Commented:
sorry i post the code below
hi EE
 
i have an array like this
 
Array
(
    [0] => 1302                      11022009                      132V4
    [1] => 1302            11022009            132V4
}
 
can i trasform this array into this?
 
Array
(
    [0] => Array
              (
                 [0] =>1302
                 [1] =>11022009
                 [2] =>132V4
              )
    [1] => Array
              (
                 [0] =>1302
                 [1] =>11022009
                 [2] =>132V4
              )
}

Open in new window

0
 
agamalCommented:
is this what you are looking for

<?php
//building your array
$testarray[0]= "1302                      11022009                      132V4";
$testarray[1]= "1302          11022009              132V4";
 
//checking the old value
print_r($testarray);
echo "<br>";
 
//converting it
for ($i=0;$i<count($testarray);$i++) {
$testarray[$i] = str_replace("  ", " ", $testarray[$i]);
$new = preg_split("/[\s,]+/",$testarray[$i]);
$testarray[$i] =array ($new[0],$new[1],$new[2]);
}
 
//checking the old value
print_r($testarray);
echo "<br>";
?>

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
denny3dAuthor Commented:
ok agamal
but if my original array is:

Array
(
    [0] => 1302                      11022009                      132V4
    [1] => 1302            11022009            132V4                          11022009            132V4
    [2] => 1302            
}

your code create always 3 node
0
 
agamalCommented:
Now Try This :D
<?php
//building your array
$testarray[0]= "1302                      11022009                      132V4";
$testarray[1]= "1302            11022009            132V4                          11022009            132V4";
$testarray[2]= "1302";
 
//checking the old value
print_r($testarray);
echo "<br>";
 
//converting it
for ($i=0;$i<count($testarray);$i++) {
$testarray[$i] = str_replace("  ", " ", $testarray[$i]);
$new = preg_split("/[\s,]+/",$testarray[$i]);
$testarray[$i]= $new;
 
}
 
//checking the old value
print_r($testarray);
echo "<br>";
?>

Open in new window

0
 
denny3dAuthor Commented:
ok , last problem
for each new array, create a last node blank
like this:

can i delete it?
Array
(
 
    [0] => Array
        (
            [0] => 4/4BE
            [1] => 00001SOTECO
            [2] => SRL
            [3] => 000747
            [4] => Y
            [5] => 1
            [6] => 384.0
            [7] => 1251.015
            [8] => U=1
            [9] => 4
            [10] => W/mqýK
            [11] => POS.20-PZ.1
            [12] => 2009/0348
            [13] => 
        )
   [1] => Array
        (
            [0] => 4/4BE
            [1] => 00001SOTECO
            [2] => SRL
            [3] => 000747
            [4] => Y
            [5] => 1
            [6] => 384.0
            [7] => 1251.015
            [8] => U=1
            [9] => 4
            [10] => W/mqýK
            [11] => POS.20-PZ.1
            [12] => 2009/0348
            [13] => 
        )
)

Open in new window

0
 
agamalCommented:
try this and tell me

replace $array to your array variable name

foreach($array as $key => $value) {
  if($value == "") {
    unset($array[$key]);
  }
}
$new_array = array_values($array); 

Open in new window

0
 
denny3dAuthor Commented:
I dont undertund where i insert this code?
//converting it
for ($i=0;$i<count($new_array);$i++) {
$new_array[$i] = str_replace("  ", " ", $new_array[$i]);
$new = preg_split("/[\s,]+/",$new_array[$i]);
#$new_array[$i] =array ($new[0],$new[1],$new[2]);
$new_array[$i] = $new;
}
 
foreach($array as $key => $value) {
  if($value == "") {
    unset($array[$key]);
  }
}
$new_array = array_values($array); 

Open in new window

0
 
agamalCommented:
like this

//converting it
for ($i=0;$i<count($new_array);$i++) {
$new_array[$i] = str_replace("  ", " ", $new_array[$i]);
$new = preg_split("/[\s,]+/",$new_array[$i]);
#$new_array[$i] =array ($new[0],$new[1],$new[2]);
$new_array[$i] = $new;
}
 
foreach($array as $key => $value) {
  if($value == "") {
    unset($array[$key]);
  }
}
$new_array = array_values($array); 
 
//the new added code
foreach($new_array as $key => $value) {
  if($value == "") {
    unset($new_array[$key]);
  }
}
$new_array = array_values($new_array); 

Open in new window

0
 
denny3dAuthor Commented:
sorry but dont work
I have always the last not blank like the result

//converting it
for ($i=0;$i<count($new_array);$i++) {
$new_array[$i] = str_replace("  ", " ", $new_array[$i]);
$new = preg_split("/[\s,]+/",$new_array[$i]);
#$new_array[$i] =array ($new[0],$new[1],$new[2]);
$new_array[$i] = $new;
}
foreach($new_array as $key => $value) {
  if($value == "") {
    unset($new_array[$key]);
  }
}
$new_array = array_values($new_array); 
 
 
 
 
THE RESULT:
[2] => Array
        (
            [0] => 3+3/3+3BE
            [1] => 00001SERRAMENTI
            [2] => R000640
            [3] => Y
            [4] => 2
            [5] => 818.0
            [6] => 2060.012
            [7] => U=1
            [8] => 6
            [9] => W/mqýK
            [10] => 0058/09
            [11] => 
        )

Open in new window

0
 
agamalCommented:
echo the empty value like this

echo "XX".$array[2][11]."XX";

this will show us the content of the empty value it may be spaces or tab space

try this on many arrays and send my samples
0
 
denny3dAuthor Commented:
the result is:

XXXX
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.