0

I have a great couple of variables named $value1, $value2 etc. I want to create an array key for each variable only if the variable is not empty. Something like this:

$array = array(
    If (!empty($value1)) { "bar" => "foo", }
    If (!empty($value2)) { "foo" => "bar", }
);

How do I do this and what would be good practice?

6
  • 1
    Do you want to create an array key as the contents of each of those variables? How do foo/bar relate above? Commented Oct 23, 2014 at 14:28
  • How many variables do you have? If just a few, what you have is fine. If you have several, collect them into an array and loop over them. Commented Oct 23, 2014 at 14:29
  • @MichaelBerkowski That was just an example. To clarify, if $value1 is empty, then there shouldn't be a key created. Commented Oct 23, 2014 at 14:29
  • 1
    Duplicate of: stackoverflow.com/questions/5693754/… Commented Oct 23, 2014 at 14:29
  • @MichaelBerkowski Can you please post an answer detailing exactly how I would collect my variables into an array and loop over them? It seems this is the best practice? Commented Oct 23, 2014 at 14:30

3 Answers 3

3

PHP arrays are dynamic, so you can add stuff to it easily:

$array = array(); // start with empty one

if (!empty($value1)) $array['bar'] = 'foo';
if (!empty($value2)) $array['foo'] = 'bar';

// you don't even have to specify a key, 
// it will just increment accordingly if left out
if (!empty($value3)) $array[] = 'foobar';

this will result in (if all 3 vars are non-empty):

array(3) {
  'a' => 'foo',
  'b' => 'bar',
  0   => 'foobar'
}

http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying

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

4 Comments

I have one final question if you don't mind. How do I use this approach with an indexed array? array(value1,value2,value3,etc.);
How do I add a new value to the array?
@HenrikPetterson Using the [] empty square bracket syntax. if (!empty($value1)) $array[] = $value1; php.net/manual/en/…
I'll add it for completeness
3

You can't do it that way - if you use the array shortcut notation, you WILL create an entry in the array, whether there's a value or not. You'll have to test/set each key individually:

$arr = array();
if (!empty($value)) { $arr['bar'] = 'foo' }

Comments

-2

You can use Variable variables:

$array = array();
$count = 4; //You can have n variables
for($i = 1; $i <= $count; $i++){
    if(isset(${'value' . $i})){
      $array[$i] = ${'value' . $i};
    }
}

4 Comments

That doesn't resemble anything in the question.
Before downvoting, read the question: I have a great couple of variables named $value1, $value2. etc
you aren't even testing if the variables exist... and I'm pretty sure those variable names were just examples.
Fixed the check. You're pretty sure but not enough.

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.