gsingh85 Posted September 10, 2014 Share Posted September 10, 2014 I have a variable and it contains a lot of strong data would I want to convert the string data into an array. What's the best way of doing because I know older versions of php will be slightly different? Quote Link to comment https://forums.phpfreaks.com/topic/290980-best-way-to-create-an-array-from-string-data/ Share on other sites More sharing options...
Jacques1 Posted September 10, 2014 Share Posted September 10, 2014 This is rather vague. Can you give an example of such a string? And how is the resulting array supposed to look like? Quote Link to comment https://forums.phpfreaks.com/topic/290980-best-way-to-create-an-array-from-string-data/#findComment-1490631 Share on other sites More sharing options...
CroNiX Posted September 10, 2014 Share Posted September 10, 2014 That really depends on the format/structure of the data. if it was something like: $data = 'one|two|three'; it would be as simple as: $data_array = explode('|', $data); Quote Link to comment https://forums.phpfreaks.com/topic/290980-best-way-to-create-an-array-from-string-data/#findComment-1490632 Share on other sites More sharing options...
gsingh85 Posted September 10, 2014 Author Share Posted September 10, 2014 Sorry. Here is an example I have been working on: $fitters = \DB::select('address')->from('fitters')->execute()->as_array(); foreach ($fitters as $fitter) { $town = explode(',', $fitter['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; A new variable exists $get_available_towns to add some extra on the array but why are the [] brackets required? Quote Link to comment https://forums.phpfreaks.com/topic/290980-best-way-to-create-an-array-from-string-data/#findComment-1490634 Share on other sites More sharing options...
Jacques1 Posted September 10, 2014 Share Posted September 10, 2014 Because this appends the value of $town to the $get_available_towns array. If you remove the brackets, you just overwrite the $get_available_towns variable again and again. This has nothing to do with the PHP version, by the way. Quote Link to comment https://forums.phpfreaks.com/topic/290980-best-way-to-create-an-array-from-string-data/#findComment-1490638 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.