简体   繁体   中英

Passing array to a function inside class PHP

I have a question on how to pass an array to a function in PHP. I have a class called "MyClass" and inside it has functions called rankVal($arr1, $arr2) and processResponse($data, $db, $id, $lat, $lng) .

processResponse() will call rankVal() and here is my problem is.

class MyClass{
   private function cmpVal($a, $b){
        /*do sorting stuff*/
   }

   function rankVal($arr1, $arr2){
       $arrIdx=[];
       foreach ($arr1 as $key => $value) {
           $n=array_search($value, $arr2);
           $newPos = ($key+$n)/2;
           $arrNewIdx [$n]=round($newPos,0, PHP_ROUND_HALF_DOWN);
       }

   }

   function processResponse($data, $db, $id, $lat, $lng){
       //Do some stuffs here...
       $someArr1 = [];
       foreach($results as $key => $value){
           $newVal = new stdClass();
           $newVal->key1 = $value->key1;
           $newVal->key2 = $value->key2;
           $newVal->key3 = $value->key3;
           $newVal->key4 = $value->key4;
           $newVal->key5 = $value->key5;
           $someArr1 []= $newVal;
       }
       $someArr2 = $someArr1;

       usort($someArr2, array($this, "cmpVal"));
       $rankedVal = $this->rankVal($someArr1, $someArr2);
   }
}

When I called the processResponse() function I got this error: array_search() expects parameter 2 to be array, object given

So, I var_dump($arr2) in rankVal() , and the output clearly says that $arr2 is an array. Here's the sample output of the var_dump($arr2) :

array(30) {
[0]=>
 object(stdClass)#385 (7) {
   ["key1"]=>
   string(24) "something"
   ["key2"]=>
   string(20) "something"
   ["key3"]=>
   string(41) "something"
   ["key4"]=>
   float(1.23455)
   ["key5"]=>
   float(1.19128371983198)
 }

What did I do wrong? I tried to pass the array by reference by adding "&" in rankVal(&$arr1, &$arr2) , but the error is still there.

To add to the other answer here (which now seems to have gone), if you want your class to behave as an array where appropriate, you need to make it iterable.

All you need to do is implement Iterable . This means that you need to create the necessary methods, but this is all you need in order to have your class behave that way.

Its useful for classes which are designed to hold an array of data, but you want to encapsulate additional tools along with that data.

Heres an example:

class Row implements \Iterator {
    protected $data;
    protected $position = 0;

    public function __construct( array $data = [ ]) {
        $this->data = $data;

    }

    public function addData( $value ) {
        $this->data[] = $value;
    }

    public function replaceData( $index, $value ) {

        $this->data[ $index ] = $value;
    }

    public function getData() {
        return $this->data;
    }

    public function setData( array $data ) {
        $this->data = $data;

        return $this;
    }

    /** Required by Iterator */
    public function current() {
        return $this->data[ $this->position ];
    }

    /** Required by Iterator */
    public function next() {
        ++$this->position;
    }

    public function __toArray() {
        return $this->data;
    }

    /** Required by Iterator */
    public function key() {
        return $this->position;
    }

    /** Required by Iterator */
    public function valid( $index = null ) {
        return isset( $this->data[ $index ? $index : $this->position ] );
    }

    /** Required by Iterator */
    public function rewind() {
        $this->position = 0;
    }

    public function count() {
        return count( $this->data );
    }

}

Once you have this, it can be used anywhere you can use an array.

So after checking my code again, I finally found the problem that causing this weird bug which was not supposed to be there.

The culprit is in the rankVal() function where I called usort() which used rankVal() as the callback function for the sorting process. Then, I changed this callback function to the right one, and voila problem's solved.

Thanks for everyone who had answered and given me some suggestions on how to fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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