LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 611 users online 221665 members 803 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
WhoreosandMilk
Favs: anything compelling or entertaing
Mood: Scheming
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
6 online / 21 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

Array transformations in PHP
Replies: 3Last Post May 27, 2008 3:04pm by britishguy
Welcome to LiveWire!
We're Stronger Together.
Join the Community
Single page for this topic Email Print Favorite
( pleaseremove )


meh

Patron
Tech Support Leader
Reply
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

-------
Anyone who isn't confused really doesn't understand the situation
http://craigk.org/pictures/
Can you work out the code?


6:33 pm on April 17, 2008 | Joined: Feb. 2005 | Days Active: 1,299
Join to learn more about pleaseremove England, United Kingdom | Straight Male | Posts: 5,945 | Points: 37,468
LiveWire Humor
Post from this position was omitted due to content violations
i who have nothing


Executive

Patron
Reply
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.)

Post edited at 11:28 am on April 25, 2008 by i who have nothing


11:23 am on April 25, 2008 | Joined: Mar. 2008 | Days Active: 119
Join to learn more about i who have nothing Ontario, Canada | Straight Male | Posts: 2,559 | Points: 3,473
britishguy


WORSHIP THE SPICE

Patron
Support Leader
Reply
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

Post edited at 3:06 pm on May 27, 2008 by britishguy

-------
mysql_query("DELETE stupidity FROM earth") or die("Stupidity");


3:04 pm on May 27, 2008 | Joined: April 2004 | Days Active: 640
Join to learn more about britishguy England, United Kingdom | Label Free Male | Posts: 6,925 | Points: 32,147
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic