1

When dealing with arrays I am forced to add a bunch of repetitive code to handle arrays with one child versus multiple:

//If more than one step, process each step, elcse processs single
        if(!array_key_exists('command',$pullcase['steps']['step'])) {
            foreach($pullcase['steps']['step'] as $step) {
                $command=$step['command'];
                $parameter=$step['parameter'];
                if(isset($step['value'])){ 
                    $value = $step['value']; 
                    $this->runCommands($command,$parameter,$value); 
                } else { 
                    $this->runCommands($command,$parameter); 
                }
            }
        } else {
            $command = $pullcase['steps']['step']['command'];
            $parameter = $pullcase['steps']['step']['parameter'];
            if(isset($pullcase['steps']['step']['value'])){ 
                $value = $pullcase['steps']['step']['value']; 
                $this->runCommands($command,$parameter,$value); 
            }
            else { $this->runCommands($command,$parameter); }
        }

As you can see, I'm having to duplicate my efforts depending on if there is a single item in an array versus multiple:

$pullcase['steps']['step'][0]['command'] 

vs

$pullcase['steps']['step']['command']

How can I simplify this code so that I can use a single variable for all instances?

4
  • Move the repetitive code into a separate function Commented Mar 12, 2015 at 18:35
  • Also, if you control the creation of the array, make step an array of one even if there is only one so you always have an array. Is that possible? Commented Mar 12, 2015 at 18:40
  • I'm not following Abra - there can be mutliple [step] within [steps], this is theh nature of the array. Why would I want have only one [step] under [steps]? Also, numbering the steps would only serve to over-complicate the array. Commented Mar 12, 2015 at 18:44
  • You either have a step array [step][0][command] or you have a single step [step][command]. What I'm saying is when you create the array instead of [step][command] make it [step][0][command]. Standard way of doing it, problem solved as you only need the foreach. Commented Mar 12, 2015 at 18:57

2 Answers 2

1

If you control the creation of the array, make step an array of one even if there is only one so you always have an array. Is that possible?

You either have a step array [step][0][command] or you have a single step [step][command]. So when you create the array instead of [step][command] make it [step][0][command] etc. Standard way of doing it, problem solved as you only need the foreach.

If you can't do it at array creation then consider doing it before the loop:

if(is_array($pullcase['steps']['step'])) {
    $steps = $pullcase['steps']['step'];
} else {
    $steps[] = $pullcase['steps']['step'];
}
foreach($steps as $step) {
    $value = isset($step['value']) ? $step['value'] : null;
    $this->runCommands($step['command'], $step['parameter'], $value); 
}

Also, if runCommands() can detect a empty argument, then an alternative to the if/else for the function call is used above.

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

2 Comments

I agree that changing the array is the way to go. I'm creating the array from an object so I'll have to work on doing that. Thanks!
BTW: thanks for the $value tip as well. It works like a charm!
0

The following may help. It doesn't do much but call the function "runcommands" on a value if the key is 'command'. I am using it to show how you can use array_walk_recursive to possibly solve your problem.

First, you need the function to use:

function runcommandswhencommand($value, $key)
{
  if($key == 'command') runcommands($value);
}

Now, you can use the recursive walk on your array:

array_walk_recursive($pullcase, 'runcommandswhencommand');

With this, whenever the key is 'command', the value of that index will be used in the parameter of the function runcommands().

3 Comments

I like this technique. I'm unfamiliar with this function. I'll try it out and see how it works. Thanks!
I tried to make it very simple. I hope it is obvious that you can tell it what to do based on the key value. I'm not sure if it will get what you need, but it is where I would start.
Unfortunately due to the nature of the array this won't work. The components of step are dependent on each other. Writing the logic around this function would be overkill. Thanks, though!

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.