0

Ok, am trying to figure out the best way to do this. I have code as follows:

$context['dp_module_headers'] = array();

Than later on down within the function within a while loop I iterate through all foldernames and apply the path to this array. But the paths are relative to the folder names, so I need to include the folder names within the values of the exploded array.

$context['dp_module_headers'] += explode('+', $row[$type . '_header_files']);

This can return an array like so:

$context['dp_module_headers'][0] = 'source/script.js';
$context['dp_module_headers'][1] = 'source/script.css';
$context['dp_module_headers'][2] = 'source/script.js';
$context['dp_module_headers'][3] = 'mydir/style.css';

I need to pre-pend the folder name before all values within the array. I only have access to the folder name that it is in when within the function that does the explode. So within the other function of a different file, I don't have access to the folder name that these paths link to.

So, when doing the explode, I need to pre-pend the folders name before each path within the value of each array.

So, basically, this array needs to return the following instead:

$context['dp_module_headers'][0] = 'sitenews/source/script.js';
$context['dp_module_headers'][1] = 'sitenews/source/script.css';
$context['dp_module_headers'][2] = 'userpanel/source/script.js';
$context['dp_module_headers'][3] = 'userpanel/mydir/style.css';

I have a variable called $folder that gets changed per explode, but how can I add the $folder name string variable to the beginning of each value while exploding it? Or is there a better way to do this?

Thanks guys :)

0

1 Answer 1

3

With PHP 5.3+, you can use something like this:

$context['dp_module_headers'] += array_map(
    function ($path) use ($folder) { return $folder . $path; },
    explode('+', $row[$type . '_header_files'])
);

That's a little more awkward to with with 5.2-, there you should save your exploded paths in a variable, loop over that array, prepend the $folder value to each value, then add the result to your $context array.

1
  • Thanks a million. I actually thought about this some and created another array bound. $context['dp_module_headers'][$folder] = explode('+', $row[$type . '_header_files']); than within the other file, I just perform 2 foreach loops. The first one gives me the folders name, the second loop gives me the path within the values. This works like a charm, especially since the $folder variable will never have spaces and only has 1 folder name. Commented Aug 18, 2011 at 4:49

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.