Ken J
Ken J

Reputation: 4562

PHP Array of Methods and Parameters

I'm passing an array of methods and parameters to my object. The array can be any size with any number of methods and/or parameters. In between the methods are the parameters.

EDIT: Sorry that this was not clear. The parameters following a method are for THAT method. So in the first example below method1 has two parameters (param1 and param2), method2 has two parameters (param1 and param2) and method3 has one parameter (param).

Here are some examples:

array = ["method1","param1","param2","method2","param1","param2","method3","param"];
array = ["method1","param1","param2","method2","param"];
array = ["method","param"];

I can pull the methods from the array using method_exists but I'm trying to pull the parameters from the array. This is what I've got so far:

    // Loop thru $array to find methods
    $methods = array();
    for($i=0; $i < count($array); $i++) {
        if(method_exists($this,$array[$i]) === TRUE) {
               $methods[] = $i;
        }
    }
    // Get parameters for methods

    // If there is only one method take the remaining array values
    // else get the parameters in between the methods
    if(count($methods) == 1) {
        $parameters = array_slice($array,1,count($array));  
    } else {
        ??
    }

How can I grab the array values for parameters that match up to the methods when the array values for methods are variable?

Upvotes: 0

Views: 168

Answers (5)

Simon Hi
Simon Hi

Reputation: 3016

It is more efficient to build $parameters array when building $methods array. But if for some unkown reasons, you want to keep your code as is, here is a way to do it:

$methods = array();
for($i=0; $i < count($array); $i++) {
    if(method_exists($this,$array[$i]) === TRUE) {
           $methods[] = $i;
    }
}

if(count($methods) == 1) {
    $parameters = array_slice($array,1,count($array));  
} else {
    $parameters = array();
    for ($k=0; $k < (count($methods) - 1); $k++)
        $parameters[$k] = array_slice($array,$methods[$k]+1,$methods[$k+1]-$methods[$k]-1);
    $parameters[$k] = array_slice($array,$methods[$k]+1,count($methods)-$methods[$k]-1);
}
print_r($parameters);

Upvotes: 0

Darragh Enright
Darragh Enright

Reputation: 14136

As I understand the question, you have an array of methods and parameters, and you want to separate these - presumably into separate arrays.

If so, you can try the following simplified example:

<?php

// example object with some public methods...
class Foo
{
    function method1() {}
    function method2() {}
    function method3() {}
}

// list of methods and params
$array = [
    'method1', 
    'param1', 
    'param2', 
    'method2', 
    'param3', 
    'method3', 
    'param4', 
    'param5',
];

// get a list of Foo's methods
$methods = get_class_methods('Foo');

// array_diff to get the params only
$params = array_diff($array, $methods);

print_r($params);

Yields:

Array
(
    [1] => param1
    [2] => param2
    [4] => param3
    [6] => param4
    [7] => param5
)

YMMV - this example is outside the context of the object, so get_class_methods() can only see public methods. It can be used inside an object to get protected and private methods too. It depends on your exact use case.

Hope this helps.

Upvotes: 0

Scott
Scott

Reputation: 12376

You are close, but I believe you only want to process the array once. Remember - all of your array values are either "methods" OR "parameters". Therefore, if you are able to find "methods", you can also find "parameters".

$methods = array();
$parameters = array();
$curParameters = null;
for($i=0; $i < count($array); $i++) {
    if(method_exists($this,$array[$i]) === TRUE) {
           //associate last set of paremeters with method
           if ($curParameters != null) {
               $parameters[] = $curParameters;
           }
           //new method, new curParams
           $methods[] = $i;
           $curParameters = array();
    } else {
         //$array[$i] must be a parameter for the last method found
         $curParameters[] = $array[$i];
    }
}
//need to save the last set params founds
$parametres[] = $curParameters

When this is done parsing, you should have two arrays. The first array is $methods and the second is $parameters. These arrays should match up 1 to 1, meaning $parameters[x] should match up with $methods[x].

Upvotes: 1

Danoweb
Danoweb

Reputation: 433

What you could do here is check if the value you are pulling out is in your methods array. If it is, then you know it is a method and not a parameter. if it isn't then you know it to be a parameter.

I would use the array_search() php method to accomplish this.

so you code would look something like:

for($x=0;$x<count($array);$x++)
{
    if( array_search($array[$x], $methods) !== false)
    {
         //This is a Parameter
    }
}

You can find more information here: http://php.net/manual/en/function.array-search.php

Upvotes: 0

Mat
Mat

Reputation: 2154

You could just turn

for($i=0; $i < count($array); $i++) {
    if(method_exists($this,$array[$i]) === TRUE) {
           $methods[] = $i;
    }
}

To

$j = 0;
for($i=0; $i < count($array); $i++) {
    if(method_exists($this,$array[$i]) === TRUE) {
           $methods[] = $i;
           $j++;
    }
    else {
        $parameters[$j][] = $array[$i];
    }
}

Upvotes: 0

Related Questions