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.

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 :)

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  SoLoGHoST Aug 18 '11 at 4:49

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.