We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

trasform an array in multidimensional array (second question)

denny3d
denny3d asked
on
Medium Priority
255 Views
Last Modified: 2012-05-06
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
Comment
Watch Question

Author

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

Commented:
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

Author

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

Commented:
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

Author

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

Commented:
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

Author

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

Commented:
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

Author

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

Commented:
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

Author

Commented:
the result is:

XXXX
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.