简体   繁体   English

PHP方法和参数数组

[英]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. 方法后面的参数用于THAT方法。 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). 因此,在下面的第一个示例中,method1具有两个参数(param1和param2),method2具有两个参数(param1和param2),而method3具有一个参数(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. 我可以使用method_exists从数组中拉出方法,但是我试图从数组中拉出参数。 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? 当方法的数组值可变时,如何获取与方法匹配的参数的数组值?

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 . 第一个数组是$methods ,第二个数组是$parameters These arrays should match up 1 to 1, meaning $parameters[x] should match up with $methods[x] . 这些数组应该匹配1到1,这意味着$parameters[x]应该与$methods[x]匹配。

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];
    }
}

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. 我将使用array_search() php方法来完成此操作。

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 您可以在此处找到更多信息: http : //php.net/manual/en/function.array-search.php

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. YMMV-此示例在对象的上下文之外,因此get_class_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. 希望这可以帮助。

It is more efficient to build $parameters array when building $methods array. 构建$ methods数组时,构建$ parameters数组更有效。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM