0

How can I rewrite this please as I tried a few things and I just dont know the correct way to do it. Instead of:

$files_to_zip = array(
    1,
    4,
    8
);

How can that be rewritten so that:

$explode_mods  = "1,4,8";
$mod_nums = explode(",", $explode_mods);

$files_to_zip = $mod_nums;

Basically $files_to_zip still needs to be in array format but I don't know how to only display each value into an array for the $files_to_zip variable. This is part of a larger script and I am almost done so please do not show me a totally different code I just need $files_to_zip to be read as an array still. The $explode_mods will actually be a $_POST value in the real script where I enter numbers separated by commas and they will be inserted into the $files_to_zip array. Thank you.

3
  • What do you really mean? Commented Sep 16, 2011 at 7:30
  • I don't understand your problem. $files_to_zip (and $mod_nums) will be an array, containing the the elements 1, 4, and 8. Commented Sep 16, 2011 at 7:30
  • I took the hint from the title, with "add values".. Let's see if I'm correct :) Commented Sep 16, 2011 at 7:33

1 Answer 1

1

I think I read your question correctly: you already have other entries in $files_to_zip which you want to merge with $explode_mods?

Try this:

$files_to_zip = array('some', 'entries', 'here', 'already');
$explode_mods = "1,4,8";
$files_to_zip = array_merge($files_to_zip, explode(',', $explode_mods));
var_dump($files_to_zip);
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, I edited my original topic. I think you may have me going in the right direction, still a little more tweaking. I basically want $files_to_zip to be an array that I can insert values into from a form. I just tried: $explode_mods = trim($_POST['mods']); $files_to_zip = array_merge(explode(",", $explode_mods)); and it almost worked
You will have to explain "an array that I can insert values into from a form" a bit more, I think.

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.