Jump to content

seperating an array at a certain index


Go to solution Solved by Michael_Baxter,

Recommended Posts

hi,  

   On my website I have a form with a single text area and a submit button,

the user will come to my site with a report of points and stats from hosting a tournament online,

see attached picture for details of the report

once they hit submit the form posts to my processing page,

in the processing page I take all the information I need from the beginning of the report this was kind of easy to do,

the part I seem to be struggling with is splitting the array at this point.

I have exploded each line from my report into an array $records,

what I need to do now is,

take everything from the line "START POINTS"

down to the line "STOP POINTS"

from the main array into a new array

so that I can get the new array and use a foreach loop on it to separate the player name from player points

I already have the foreach loop setup ready as I had this all setup once but I was not getting the information from the top of the report now I need that information so my original foreach does not work

post-173124-0-77251600-1485202255_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/303010-seperating-an-array-at-a-certain-index/
Share on other sites

I did think about that to start with apart from $records is an array each line in the report from my screenshot I uploaded here is an individual index inside of records

so $records[0]; would equals to "Nojoks's Tourney Bracket Tool Version 1.2.1.84

$records[1] equals to "Tournament 3/5 Backgammon 1.00pm"

and so on.

so I don't think strops would really work here

every single line from that report has been exploded into an array that's why I need to get access to line 19 then I can run a for each loop on the lines after that

There are several things in this job that can lead to insanity. One of them is accepting free-form text as input. It only takes a small spelling mistake (like your "strops" above) and the whole thing collapses like a house of cards.

I did think about that to start with apart from $records is an array each line in the report from my screenshot I uploaded here is an individual index inside of records...

 

With each line split into an array, you could loop through the array looking for the start point. Then store each of the following lines in a separate variable until the stop point is reached.

 

 

 

There are several things in this job that can lead to insanity. One of them is accepting free-form text as input. It only takes a small spelling mistake (like your "strops" above) and the whole thing collapses like a house of cards.

 

Yep, that would be problematic.

  • Solution

in the end I have used array slice to fix this issue,

I took array slice from index 20 then a foreach loop on the new array from the slice with the final argument as -1

to remove the stop points line

if copy and paste would work on this forum I would show you my final code

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.