0

I've come up with this monstrosity :

echo $value;
And the result is this:

"_aaaaaaa","_bbbbbb","_cccccc","_dddddd"

This is i a string....but i want to make it look like this in end.

$value= array("_aaaaaaa","_bbbbbb","_cccccc","_dddddd");

I've tried everything.How can i make this string into an array like the above ?

Any help here ? -Thanks

1
  • i messed up my script some way...give me a sec guys to test this. Commented Feb 23, 2014 at 0:19

4 Answers 4

2

If I understand you correctly, $value = explode(',', $value); should turn this into an array.

Sign up to request clarification or add additional context in comments.

2 Comments

well yeah thats what i was trying to do...but like this its not working...cause i am using this array like this after: foreach ($value as $key => $kv) {....} and its not doing it correctly...but if i write the whole thing as $value= array("_aaaaaaa","_bbbbbb","_cccccc","_dddddd"); Then i get the right results.
@fejese has your answer then :)
0

Try as following:

$value= '"_aaaaaaa","_bbbbbb","_cccccc","_dddddd"';
$array = array_map(function($v) { return trim($v, '"'); }, explode(',', $value));

More simply:

$array = explode('","', trim($value, '"'));

10 Comments

nope none work... :( The thing is i get my result from another function. I parse some files and create that value like this: $value = getHeadersInArray($parentDirectory2); This echoed...is what i showed you....
@user1033882 Nope? Working here -> 3v4l.org/ZN59e If it's not working with your string then your string isn't formatted as you're telling us it is.
This is the function that return that value: 3v4l.org/X23iR If i echo $value i get what i said at the top... Edit: sorry...wrong one.
var_dump() the string being returned from that function and paste the value it gives you in your question. The function you're showing says nothing of the return value. In it you're building an array of $items, and yet it's returning a variable named $value which doesn't appear to have been defined inside the function itself.
yeah i simplified the 1st post...so i didnt get into deep trying to avoid all this...lol, I thought there was a simple way of just converting a string to an array but like i thought...this is harder than it seems. var_dumping the value brings :string(65) ""_aaaaaaa","_bbbbbb","_ccccccc","dddddd"" I have to keep changing the results to _aaaaa _bbbb but you can imagine i just cant post publicly this data...hence these values
|
0

Hope this help

 $str = '"_aaaaaaa","_bbbbbb","_cccccc","_dddddd"';
$value = explode(',', $str);
foreach ($values as $val) {
    $val = trim($val, '"');
}

1 Comment

To give you the whole background of the function...I am parsing a directory and gather (while i am parsing the files) all the headers the files have into an array...At the end of the function i am returning the array...hence the $value = getHeadersInArray($parentDirectory2); That $value echoed...is the result i gave you...I need to do this to like this : print_r($value) to see it...This is how after the conversion to an array it should look like. Array ([0] =>_aaaaaaa [1] => _bbbbbb [2] => _cccccc [3] => _dddddd )
0

Your question would need more specific description I guess what you are trying to achieve but if you have a string that contains values separated by , and surrounded with quotes then you want to do something like this:

$value = explode(',', $value);
foreach ($value as &$val) {
    $val = trim($val, '"');
}

3 Comments

this results: Warning: Invalid argument supplied for foreach()
Change the first line to: $values = explode(',', $value);
that only brings : _aaaaaaa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.