?
Solved

trasform an array in multidimensional array (second question)

Posted on 2009-02-18
12
Medium Priority
?
245 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
0
Comment
Question by:denny3d
  • 6
  • 6
12 Comments
 

Author Comment

by:denny3d
ID: 23668336
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
 
LVL 8

Expert Comment

by:agamal
ID: 23668454
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
 

Author Comment

by:denny3d
ID: 23668483
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:agamal
ID: 23668554
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
 

Author Comment

by:denny3d
ID: 23668581
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
 
LVL 8

Expert Comment

by:agamal
ID: 23668625
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
 

Author Comment

by:denny3d
ID: 23668889
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
 
LVL 8

Expert Comment

by:agamal
ID: 23668936
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
 

Author Comment

by:denny3d
ID: 23668973
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
 
LVL 8

Expert Comment

by:agamal
ID: 23669512
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
 

Author Comment

by:denny3d
ID: 23669645
the result is:

XXXX
0
 
LVL 8

Accepted Solution

by:
agamal earned 2000 total points
ID: 23671111
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question