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)

on
Medium Priority
255 Views
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

## View Solution Only

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
)
}
``````

Commented:
is this what you are looking for

``````<?php
\$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>";
?>
``````

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
\$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>";
?>
``````

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] =>
)
)
``````

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);
``````

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);
``````

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);

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

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] =>
)
``````

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

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
\$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>";

?>
``````

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

##### Thanks for using Experts Exchange.

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