etrader Posted April 29, 2011 Share Posted April 29, 2011 I want to create an array from a file with this structure TITLE1, HEADER1, key11, key12, key13, key14 TITLE2, HEADER2, key21, key22, key23, key24 . . . How I can get a foreach with these elements: $title="TITLE" $header="HEADER" $key="(one random key)" If needed, I can change the file structure too. Quote Link to comment https://forums.phpfreaks.com/topic/235063-foreach-for-a-multi-level-array/ Share on other sites More sharing options...
salathe Posted April 29, 2011 Share Posted April 29, 2011 Show us how you are looping over the file (if you are), to give us some context to work within. Quote Link to comment https://forums.phpfreaks.com/topic/235063-foreach-for-a-multi-level-array/#findComment-1208045 Share on other sites More sharing options...
Undrium Posted April 29, 2011 Share Posted April 29, 2011 Are you familiar with objects? If so, create an object and insert it into an array, if you want a key for this object just make use of associative arrays. The object could have your variables Title, Header, and an array containing the keys. Then to loop through the array you just do: foreach($objectArray as $object){ //Extract the data from the object here } Did I understand you right? Quote Link to comment https://forums.phpfreaks.com/topic/235063-foreach-for-a-multi-level-array/#findComment-1208046 Share on other sites More sharing options...
etrader Posted April 29, 2011 Author Share Posted April 29, 2011 The file contains lines with TITLE, and HEADER, and some keywords I am thinking of $array=array('file.txt'); foreach ($array as $line) { $parse=explode(",", $parse); $title=$parse[0]; $header=$parse[1]; $key=???? } The problem is that I cannot take a random from $parse[2] to $parse['last'] Quote Link to comment https://forums.phpfreaks.com/topic/235063-foreach-for-a-multi-level-array/#findComment-1208053 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 You're on the right path. Change these lines $title=$parse[0]; $header=$parse[1]; $key=???? to $title = array_shift($parse); $header = array_shift($parse); // $parse will now only contain the keys. // grab a random key $key = $parse[ array_rand($parse) ]; array_shift removes the first item from the array. So when it is first called it'll remove the title from the $parse array and assign it to the $title variable, the same applies for the header. You're now left with an array of keys. To grab a random key you can use array_rand. Quote Link to comment https://forums.phpfreaks.com/topic/235063-foreach-for-a-multi-level-array/#findComment-1208066 Share on other sites More sharing options...
etrader Posted April 29, 2011 Author Share Posted April 29, 2011 Thanks for your technical advice Quote Link to comment https://forums.phpfreaks.com/topic/235063-foreach-for-a-multi-level-array/#findComment-1208074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.