Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm sure this has been answered, but I have no idea how to research it. This is my string

[value1, value2, value3, ..., valueN]

and I'd like to know if there's a native function that transform it into a simple array

array('value1', 'value2', 'value3', ..., 'valueN');

Let it be clear that I'm looking to know if there's something already written that do it, that is, another approach other than strpos('[') and implode(',').


Edit

Sorry I haven't given much attention to this question because I'm working non-stop on this project that required me to ask this question. Regarding the last comment, I'd like to add here that that is no an issue, but how it was planned. I decided to make the arrays look exactly like JSON arrays because I wanted an easy function to undo it. Here is my INI file.

; Default Pages
user = all
wordpress = all    
home/welcome = visitor
home/customer = visitor
home/new = visitor
home/signup = visitor

; User
user/panel = '["user", "worker", "customer", "company", "staff", "ruler", "gifter", "support", "salesman", "scorer", "recorder", "reporter", "deliverer", "manager", "admin"]'
user/profile = user
user/password = user
user/support = user

; Reporter
user/report = reporter

; User/Customer
user/customer/voucher = customer
user/customer/history = customer
user/customer/ticket = customer
user/customer/profiling = customer

; User/Staff
user/staff/customer = recorder

; User/Manager
user/staff/score = scorer
user/staff/voucher = deliverer

user/manager/shop = '["salesman", "manager"]'

user/manager/staff = manager
user/manager/ticket = manager
share|improve this question
    
What about regex? –  Avinash Raj Feb 20 at 16:12
1  
Regex is a Wizard's feature that I've probably spent more than 100 hours trying to master it and can't :( though I think it would be a good option considering that it could be done in one line of code. –  Marco Aurélio Deleu Feb 20 at 16:13
1  
There's nothing built-in that does what you want. Just use explode() after removing the brackes. –  Barmar Feb 20 at 16:14
1  
json_decode() can decode that to an array. –  Phylogenesis Feb 20 at 16:14
1  
Yes, if the string is [ 'value1', 'value2', 'value3' ... ] then json_decode will work. –  Phylogenesis Feb 20 at 16:22

2 Answers 2

implode() for simple things or preg_split() for regex.

http://php.net/function.implode

http://php.net/manual/en/function.preg-split.php

I used to be weary of learning regex, but this site is imensely helpful, it visually shows what your string will do, and has cheatsheet and examples.

https://www.debuggex.com/

share|improve this answer

You said in comments that the string comes from a parse_ini_file function call.

The first string is NOT what it's returned by a parse_ini_file call, so there must be something else you're doing to get that input, and that isn't necessary at all.

To obtain what you asked in your question, you should do something like this:

$ini_array = parse_ini_file($ini_path); //This is an associative array
//If necessary, get only the key=>value pairs you need
//otherwise, just keep going
$ini_values = array_values($ini_array); //this is now a 0-based array

$ini_values now contains exactly what you asked.

share|improve this answer
    
Although I appreciate the effort in helping me, that was not exactly what I meant. I used to have only strings on my .ini file, but now I'm implementing a new role system which requires a more careful planning and the fact that I'm getting that result from the ini is not a problem, but a planning. See my EDIT for clarification. –  Marco Aurélio Deleu Feb 24 at 14:18
    
@MarcoAurélioDeleu you should use ini file sections to have another level in your resulting array rather than messy json-styled strings. I do think you're going to regret your current approach down the line. It's time for a proper database. –  STT LCU Feb 24 at 14:23
    
I don't want to go for a database because it's a rather limited number of "rows". The number of pages in a system is predefined. If a developer doesn't create the page, it doesn't exist. Before this, I was using bit-wise comparison to allow/deny access, so each row would hold a 2^n number and in order to verify which is the "homepage", I'd move exponentially from 2^1. Now I'm implementing roles and I need another way to define which is the homepage for each "role" and the architecture allows me to choose which system to use at a project, so the existence of the new doesn't exclude the old. –  Marco Aurélio Deleu Feb 24 at 14:30
    
@MarcoAurélioDeleu Please, a database is still easier, cleaner and generally better to use. If you're worried about size, consider something like SQLite. –  STT LCU Feb 24 at 14:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.