?
Solved

transform an array into multidimensional array php

Posted on 2009-02-17
3
Medium Priority
?
260 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
0
Comment
Question by:denny3d
  • 3
3 Comments
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 1500 total points
ID: 23664860
Yep :)

foreach ($array as $value) {
     $new_array[] = explode(" ", $value);
}
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 23664920
Further explanation.... The explode() function will create an array out of your original value, using the spaces as separation points.  So, you loop through the original array, exploding each value into its own array.  In the process, you're inserting each exploded array into one master array, creating the multi-dimensional array that you wanted.

If you wanted to put everything back together in its original form:

<?php

foreach ($new_array as $value) {
     $restored_array[] = implode(" ", $value);
}

?>

Which would give you back your original array of values separated by spaces.
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 23672368
Glad this worked, but any particular reason why you feel the solution is only worth a B?  I provided exactly what you asked for and even the code to do the reversal.

Just curious if I missed something ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 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