Solved

Setting up an array for insert

Posted on 2011-02-26
4
219 Views
Last Modified: 2013-11-19
I am trying to separate this information by the "-" symbol so that I can insert it.

This output would be ideal.

105-2-2-3-Cheerleading

$value[0] should = 105
$value[1] should = 2
$value[2] should = 2
$value[3] should = 3
$value[4] should = Cheerleading

Ultimately I need to insert the info into MySql.  Once I get it seperated I should be able to.

The code below is what I have to work with:
101-Home 

102-About Us
102-1-Who We Are
102-2-Profile
102-3-Accreditation

103-Admissions
103-1-Welcome
103-2-Apply Now
103-3-Tuition and Fees
103-4-FAQ's
103-5-My Recommendation
103-6-Contact Us
103-7-Withdrawals / Tuition

104-1-Pre-School
104-1-1-3K Program
104-1-2-4K VPK Program

104-Academics
104-2-Elementary
104-2-1-Kindergarten
104-2-2-First Grade
104-2-3-Second Grade
104-2-4-Third Grade
104-2-5-Fourth Grade
104-2-6-Fifth Grade
104-2-7-The Arts
104-2-8-Clubs
104-2-9-Daily Schedule
104-2-10-After School Care

104-3-Middle School
104-3-1-Welcome
104-3-2-Sixth Grade
104-3-3-Seventh Grade
104-3-4-Eighth Grade
104-3-5-Leadership
104-3-6-Trips
104-3-7-After School Car

104-4-High School
104-4-1-Welcome
104-4-2-Curriculum
104-4-3-English
104-4-4-Math
104-4-5-Science
104-4-6-Social Science / History
104-4-7-Bible
104-4-8-Clubs
104-4-9-Daily Schedule
104-4-10-Trips
104-5-College Counseling
104-6-Library

105-Athletics
105-1-Eagle Sports
105-2-Programs
105-2-1-Boys
105-2-1-1-Football
105-2-1-2-Cross Country
105-2-1-3-Golf
105-2-1-4-Basketball
105-2-1-5-Baseball
105-2-1-6-Track
105-2-1-7-Tennis

105-2-2-Girls
105-2-2-1-Volleyball
105-2-2-2-Golf
105-2-2-3-Cheerleading <<<<--- EXAMPLE USED ABOVE
105-2-2-4-Basketball
105-2-2-5-Softball
105-2-2-6-Track
105-2-2-7-Tennis

105-3-Coaches
105-4-FHSAA
105-5-Game Directions

106-The Arts
106-1-Preforming Art
106-2-Visual Arts
106-3-Eagle Cafe

107-School Life
107-1-Health / Clinic
107-2-RenWeb
107-3-Camp Program
107-4-Business Office
107-5-Parent Resources
107-6-Leadership Team
107-7-Employment
107-8-Calendar

108-Support

Open in new window

I am trying to get this:

105-Athletics
105-1-Eagle Sports
105-2-Programs
105-2-1-Boys
105-2-1-1-Football
105-2-1-2-Cross Country
105-2-1-3-Golf
105-2-1-4-Basketball
105-2-1-5-Baseball
105-2-1-6-Track
105-2-1-7-Tennis

105-2-2-Girls
105-2-2-1-Volleyball
105-2-2-2-Golf
105-2-2-3-Cheerleading
105-2-2-4-Basketball
105-2-2-5-Softball
105-2-2-6-Track
105-2-2-7-Tennis

105-3-Coaches
105-4-FHSAA
105-5-Game Directions

To echo:	    

<li><a  class="active" href="athletics.php#nfc">Athletics</a>
		    <ul class="sub_menu">
			     <li><a href="page.php?pg=55#nfc">Eagle Sports</a></li>
                 <li><a href="page.php?pg=56#nfc">Programs</a>
                       <ul class="sub_menu">
                          <li><a href="page.php?pg=57#nfc">Boys</a>
                               <ul class="sub_menu">
                                  <li><a href="page.php?pg=58#nfc">Football</a></li>
                                  <li><a href="page.php?pg=59#nfc">Cross Country</a></li>
                                  <li><a href="page.php?pg=60#nfc">Golf</a></li>
                                  <li><a href="page.php?pg=61#nfc">Basketball</a></li>
                                  <li><a href="page.php?pg=62#nfc">Baseball</a></li>
                                  <li><a href="page.php?pg=63#nfc">Track</a></li>
                                  <li><a href="page.php?pg=64#nfc">Tennis</a></li>
                                </ul>                           
                          </li>
                          <li><a href="page.php?pg=65#nfc">Girls</a>
                               <ul class="sub_menu">
                                  <li><a href="page.php?pg=66#nfc">Volleyball</a></li>
                                  <li><a href="page.php?pg=67#nfc">Golf</a></li>
                                  <li><a href="page.php?pg=68#nfc">Cheerleading</a></li>
                                  <li><a href="page.php?pg=69#nfc">Basketball</a></li>
                                  <li><a href="page.php?pg=70#nfc">Softball</a></li>
                                  <li><a href="page.php?pg=71#nfc">Track</a></li>
                                  <li><a href="page.php?pg=72#nfc">Tennis</a></li>
                                </ul>                             
                          </li>
                        </ul>                  
                 </li>
                 <li><a href="page.php?pg=73#nfc">Coaches</a></li>
                 <li><a href="page.php?pg=74#nfc">FHSAA</a></li>
                 <li><a href="page.php?pg=75#nfc">Game Directions</a></li>
		    </ul>
	    </li>

Open in new window

0
Comment
Question by:phpretard
  • 3
4 Comments
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 34988101
<?

$information="101-Home 

102-About Us
102-1-Who We Are
102-2-Profile
102-3-Accreditation

103-Admissions
103-1-Welcome
103-2-Apply Now
103-3-Tuition and Fees
103-4-FAQ's
103-5-My Recommendation
103-6-Contact Us
103-7-Withdrawals / Tuition

104-1-Pre-School
104-1-1-3K Program
104-1-2-4K VPK Program

104-Academics
104-2-Elementary
104-2-1-Kindergarten
104-2-2-First Grade
104-2-3-Second Grade
104-2-4-Third Grade
104-2-5-Fourth Grade
104-2-6-Fifth Grade
104-2-7-The Arts
104-2-8-Clubs
104-2-9-Daily Schedule
104-2-10-After School Care

104-3-Middle School
104-3-1-Welcome
104-3-2-Sixth Grade
104-3-3-Seventh Grade
104-3-4-Eighth Grade
104-3-5-Leadership
104-3-6-Trips
104-3-7-After School Car

104-4-High School
104-4-1-Welcome
104-4-2-Curriculum
104-4-3-English
104-4-4-Math
104-4-5-Science
104-4-6-Social Science / History
104-4-7-Bible
104-4-8-Clubs
104-4-9-Daily Schedule
104-4-10-Trips
104-5-College Counseling
104-6-Library

105-Athletics
105-1-Eagle Sports
105-2-Programs
105-2-1-Boys
105-2-1-1-Football
105-2-1-2-Cross Country
105-2-1-3-Golf
105-2-1-4-Basketball
105-2-1-5-Baseball
105-2-1-6-Track
105-2-1-7-Tennis

105-2-2-Girls
105-2-2-1-Volleyball
105-2-2-2-Golf
105-2-2-3-Cheerleading
105-2-2-4-Basketball
105-2-2-5-Softball
105-2-2-6-Track
105-2-2-7-Tennis

105-3-Coaches
105-4-FHSAA
105-5-Game Directions

106-The Arts
106-1-Preforming Art
106-2-Visual Arts
106-3-Eagle Cafe

107-School Life
107-1-Health / Clinic
107-2-RenWeb
107-3-Camp Program
107-4-Business Office
107-5-Parent Resources
107-6-Leadership Team
107-7-Employment
107-8-Calendar

108-Support";

$infosplit=explode("\n", $information);
$array=array();
for($i=0; $i<count($infosplit); $i++) { $infosplit[$i]=trim($infosplit[$i]);

	if(!empty($infosplit[$i])) {
		$temp=explode("-", $infosplit[$i]);

		$temp_array=array(0=>$temp[0],1=>null,2=>null,3=>null,4=>$temp[(count($temp)-1)]);

		for($a=1; $a<(count($temp)-1); $a++) {
			$temp_array[$a]=$temp[$a];
		}	
		$array[]=$temp_array;
	}
}

print_r($array);

?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
Vampireofdarkness earned 500 total points
ID: 34988126
The output for the above is:
Array
(
    [0] => Array
        (
            [0] => 101
            [1] => 
            [2] => 
            [3] => 
            [4] => Home
        )

    [1] => Array
        (
            [0] => 102
            [1] => 
            [2] => 
            [3] => 
            [4] => About Us
        )

    [2] => Array
        (
            [0] => 102
            [1] => 1
            [2] => 
            [3] => 
            [4] => Who We Are
        )

    [3] => Array
        (
            [0] => 102
            [1] => 2
            [2] => 
            [3] => 
            [4] => Profile
        )

    [4] => Array
        (
            [0] => 102
            [1] => 3
            [2] => 
            [3] => 
            [4] => Accreditation
        )

    [5] => Array
        (
            [0] => 103
            [1] => 
            [2] => 
            [3] => 
            [4] => Admissions
        )

    [6] => Array
        (
            [0] => 103
            [1] => 1
            [2] => 
            [3] => 
            [4] => Welcome
        )

    [7] => Array
        (
            [0] => 103
            [1] => 2
            [2] => 
            [3] => 
            [4] => Apply Now
        )

    [8] => Array
        (
            [0] => 103
            [1] => 3
            [2] => 
            [3] => 
            [4] => Tuition and Fees
        )

    [9] => Array
        (
            [0] => 103
            [1] => 4
            [2] => 
            [3] => 
            [4] => FAQ's
        )

    [10] => Array
        (
            [0] => 103
            [1] => 5
            [2] => 
            [3] => 
            [4] => My Recommendation
        )

    [11] => Array
        (
            [0] => 103
            [1] => 6
            [2] => 
            [3] => 
            [4] => Contact Us
        )

    [12] => Array
        (
            [0] => 103
            [1] => 7
            [2] => 
            [3] => 
            [4] => Withdrawals / Tuition
        )

    [13] => Array
        (
            [0] => 104
            [1] => 1
            [2] => Pre
            [3] => 
            [4] => School
        )

    [14] => Array
        (
            [0] => 104
            [1] => 1
            [2] => 1
            [3] => 
            [4] => 3K Program
        )

    [15] => Array
        (
            [0] => 104
            [1] => 1
            [2] => 2
            [3] => 
            [4] => 4K VPK Program
        )

    [16] => Array
        (
            [0] => 104
            [1] => 
            [2] => 
            [3] => 
            [4] => Academics
        )

    [17] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 
            [3] => 
            [4] => Elementary
        )

    [18] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 1
            [3] => 
            [4] => Kindergarten
        )

    [19] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 2
            [3] => 
            [4] => First Grade
        )

    [20] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 3
            [3] => 
            [4] => Second Grade
        )

    [21] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 4
            [3] => 
            [4] => Third Grade
        )

    [22] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 5
            [3] => 
            [4] => Fourth Grade
        )

    [23] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 6
            [3] => 
            [4] => Fifth Grade
        )

    [24] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 7
            [3] => 
            [4] => The Arts
        )

    [25] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 8
            [3] => 
            [4] => Clubs
        )

    [26] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 9
            [3] => 
            [4] => Daily Schedule
        )

    [27] => Array
        (
            [0] => 104
            [1] => 2
            [2] => 10
            [3] => 
            [4] => After School Care
        )

    [28] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 
            [3] => 
            [4] => Middle School
        )

    [29] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 1
            [3] => 
            [4] => Welcome
        )

    [30] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 2
            [3] => 
            [4] => Sixth Grade
        )

    [31] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 3
            [3] => 
            [4] => Seventh Grade
        )

    [32] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 4
            [3] => 
            [4] => Eighth Grade
        )

    [33] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 5
            [3] => 
            [4] => Leadership
        )

    [34] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 6
            [3] => 
            [4] => Trips
        )

    [35] => Array
        (
            [0] => 104
            [1] => 3
            [2] => 7
            [3] => 
            [4] => After School Car
        )

    [36] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 
            [3] => 
            [4] => High School
        )

    [37] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 1
            [3] => 
            [4] => Welcome
        )

    [38] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 2
            [3] => 
            [4] => Curriculum
        )

    [39] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 3
            [3] => 
            [4] => English
        )

    [40] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 4
            [3] => 
            [4] => Math
        )

    [41] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 5
            [3] => 
            [4] => Science
        )

    [42] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 6
            [3] => 
            [4] => Social Science / History
        )

    [43] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 7
            [3] => 
            [4] => Bible
        )

    [44] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 8
            [3] => 
            [4] => Clubs
        )

    [45] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 9
            [3] => 
            [4] => Daily Schedule
        )

    [46] => Array
        (
            [0] => 104
            [1] => 4
            [2] => 10
            [3] => 
            [4] => Trips
        )

    [47] => Array
        (
            [0] => 104
            [1] => 5
            [2] => 
            [3] => 
            [4] => College Counseling
        )

    [48] => Array
        (
            [0] => 104
            [1] => 6
            [2] => 
            [3] => 
            [4] => Library
        )

    [49] => Array
        (
            [0] => 105
            [1] => 
            [2] => 
            [3] => 
            [4] => Athletics
        )

    [50] => Array
        (
            [0] => 105
            [1] => 1
            [2] => 
            [3] => 
            [4] => Eagle Sports
        )

    [51] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 
            [3] => 
            [4] => Programs
        )

    [52] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 
            [4] => Boys
        )

    [53] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 1
            [4] => Football
        )

    [54] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 2
            [4] => Cross Country
        )

    [55] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 3
            [4] => Golf
        )

    [56] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 4
            [4] => Basketball
        )

    [57] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 5
            [4] => Baseball
        )

    [58] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 6
            [4] => Track
        )

    [59] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 1
            [3] => 7
            [4] => Tennis
        )

    [60] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 
            [4] => Girls
        )

    [61] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 1
            [4] => Volleyball
        )

    [62] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => Golf
        )

    [63] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 3
            [4] => Cheerleading
        )

    [64] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 4
            [4] => Basketball
        )

    [65] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 5
            [4] => Softball
        )

    [66] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 6
            [4] => Track
        )

    [67] => Array
        (
            [0] => 105
            [1] => 2
            [2] => 2
            [3] => 7
            [4] => Tennis
        )

    [68] => Array
        (
            [0] => 105
            [1] => 3
            [2] => 
            [3] => 
            [4] => Coaches
        )

    [69] => Array
        (
            [0] => 105
            [1] => 4
            [2] => 
            [3] => 
            [4] => FHSAA
        )

    [70] => Array
        (
            [0] => 105
            [1] => 5
            [2] => 
            [3] => 
            [4] => Game Directions
        )

    [71] => Array
        (
            [0] => 106
            [1] => 
            [2] => 
            [3] => 
            [4] => The Arts
        )

    [72] => Array
        (
            [0] => 106
            [1] => 1
            [2] => 
            [3] => 
            [4] => Preforming Art
        )

    [73] => Array
        (
            [0] => 106
            [1] => 2
            [2] => 
            [3] => 
            [4] => Visual Arts
        )

    [74] => Array
        (
            [0] => 106
            [1] => 3
            [2] => 
            [3] => 
            [4] => Eagle Cafe
        )

    [75] => Array
        (
            [0] => 107
            [1] => 
            [2] => 
            [3] => 
            [4] => School Life
        )

    [76] => Array
        (
            [0] => 107
            [1] => 1
            [2] => 
            [3] => 
            [4] => Health / Clinic
        )

    [77] => Array
        (
            [0] => 107
            [1] => 2
            [2] => 
            [3] => 
            [4] => RenWeb
        )

    [78] => Array
        (
            [0] => 107
            [1] => 3
            [2] => 
            [3] => 
            [4] => Camp Program
        )

    [79] => Array
        (
            [0] => 107
            [1] => 4
            [2] => 
            [3] => 
            [4] => Business Office
        )

    [80] => Array
        (
            [0] => 107
            [1] => 5
            [2] => 
            [3] => 
            [4] => Parent Resources
        )

    [81] => Array
        (
            [0] => 107
            [1] => 6
            [2] => 
            [3] => 
            [4] => Leadership Team
        )

    [82] => Array
        (
            [0] => 107
            [1] => 7
            [2] => 
            [3] => 
            [4] => Employment
        )

    [83] => Array
        (
            [0] => 107
            [1] => 8
            [2] => 
            [3] => 
            [4] => Calendar
        )

    [84] => Array
        (
            [0] => 108
            [1] => 
            [2] => 
            [3] => 
            [4] => Support
        )

)

Open in new window


A small explanation is:

Split $information by \n (new line), then cycle through each line (the for loop).

For each cycle, remove any trailing whitespace (trim()) and check if !empty (any value).

Create $temp, which is an explode (array) separated by the -
Create $temp_array which has values 0 - 4, with 0 being the 101/102/103 number and index 4 being the text at the end. NOTE: This does not check that your input is valid. If someone forgets the text and inserts a number, that will be in index 4.

It then cycles through the other values in the $temp variable (the array, exploded by -) to populate values for index 1, 2 and 3.

At the end of each cycle it adds the new array to the main array ($array)
0
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 34988142
Oh, in addition, you will note that Pre-School is broken because of the - in the text. In order to avoid that you will need additional checks (ie- if the penultimate value in $temp is a string, prepend that string to value 4 instead of inserting to 3). I would suggest splitting by another character for simplicity -- perhaps a caret, tilde or hash.
0
 

Author Closing Comment

by:phpretard
ID: 34988144
WOW
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now