LiveWire Peer Support Network

Printable Version of Topic "Array transformations in PHP"

- LiveWire Teen Forums & College Forums (http://www.golivewire.com)
-- (http://www.golivewire.com/forums/support-technical.html)
--- Programming & Application Development (http://www.golivewire.com/forums/forum-211-s-0.html)
---- Array transformations in PHP (http://www.golivewire.com/forums/peer-iaiypi-support-a.html)


-- Posted by pleaseremove at 6:33 pm on April 17, 2008

OK, I'm trying to transform the following arrays in PHP into one multi-dimensional array. Given its 2 in the morning this is beyond my poor brain.

The arrays are:
Code:

Array
(
   [3] => title1
   [4] => title2
   [5] => title3
   [6] => title4
   [7] => title5
   [8] => title6
   [9] => title7
)
Array
(
   [3] => description1
   [4] => description2
   [5] => description3
   [6] => description4
   [7] => description5
   [8] => description6
   [9] => description7
)
Array
(
   [3] => tags1
   [4] => tags2
   [5] => tags3
   [6] => tags4
   [7] => tags5
   [8] => tags6
   [9] => tags7
)

The index for each array is an ID which i would like o be part of the new array, so i would like it to look something like this:

Code:

Array
(
   [0] => Array
             (
                  [id] => 3
                  [title] => title1
                  [description] => description1
                  [tags] => tags1
             )
   [1] => Array
             (
                  [id] => 4
                  [title] => title2
                  [description] => description2
                  [tags] => tags2
             )
   [2] => Array
             (
                  [id] => 5
                  [title] => title3
                  [description] => description3
                  [tags] => tags3
             )
   [3] => Array
             (
                  [id] => 6
                  [title] => title4
                  [description] => description4
                  [tags] => tags4
             )
   [4] => Array
             (
                  [id] => 7
                  [title] => title5
                  [description] => description5
                  [tags] => tags5
             )
   [5] => Array
             (
                  [id] => 8
                  [title] => title6
                  [description] => description6
                  [tags] => tags6
             )
   [6] => Array
             (
                  [id] => 9
                  [title] => title7
                  [description] => description7
                  [tags] => tags7
             )
)

PHP would be nice, but I'm stuck on logic, not syntax, so anything that can help is great.

Thanks


-- Posted by i who have nothing at 11:23 am on April 25, 2008

You can use classes.  i.e.

Code:

class newClass {
public $id;
public $title;
public $descrip;

function __construct() {
//constructor code here
}
}

then create the array and populating it with instances of the class.

(i've never actually done this other than in C++ and Java but I'm assuming that using your sais of PHP syntax you could get it popping.)


-- Posted by britishguy at 3:04 pm on May 27, 2008

Maybe I'm about to say something really dumb, but why don't you just write a function to iterate through the arrays and build a new one?

Something like:
Code:

//Feed this function an array consisting of the single-depth arrays you want merged  
//e.g. mergeArrays(array('titles'=>$arrTitles, 'tags'=>$arrTags))
//returns an array, with "index" form your example as the index,
//with all the fields from your example as values for each index

function merge_arrays($arrArrays)
{
   $arrNew = array();
   foreach ($arrArrays as $arrName=>$arrData){
        foreach ($arrData as $key=>$value){
             $arrNew[$key][$arrName]=$arrData;
        }
   }//end foreach

   return $arrNew;
}//end function


www.golivewire.com