Search code examples
phparraysobjectstdclassarray-intersect

array_intersect returns Severity: 4096 Message: Object of class stdClass could not be converted to string CodeIgniter


I got two arrays both have multiple values with std Class objects, I want to use 'array_intersect' but it shows "Severity: 4096 Message: Object of class stdClass could not be converted to string"

Controller Code:

    $AlreadyInsertedList = $this->bill->GetBillsbyDate($Givendate); //array1
    $NotInserted = $this->admin->GetACustomersbyArea($areaid);       //array2
    $finallist = NULL;        //finalarray
    $finallist = array_intersect($AlreadyInsertedList,$allcustomersbyarea); //Line number 88 
    //$array = json_decode(json_encode($finallist)); //already tried not working

Although it is giving the required results but with a lot of errors, I'm attaching a screen shot of it, do have a look at the scoll bar (a long length of errors) enter image description here


Solution

  • This code returns the desired results..

    function ary_diff( $ary_1, $ary_2 ) {   // compare the value of 2 array   // get differences that in ary_1 but not in ary_2   // get difference that in ary_2 but not in ary_1   // return the unique difference between value of 2 array   $diff = array();
    
      // get differences that in ary_1 but not in ary_2   foreach ( $ary_1 as $v1 ) {
        $flag = 0;
        foreach ( $ary_2 as $v2 ) {
          $flag |= ( $v1 == $v2 );
          if ( $flag ) break;
        }
        if ( !$flag ) array_push( $diff, $v1 );   }
    
      // get difference that in ary_2 but not in ary_1   foreach ( $ary_2 as $v2 ) {
        $flag = 0;
        foreach ( $ary_1 as $v1 ) {
          $flag |= ( $v1 == $v2 );
          if ( $flag ) break;
        }
        if ( !$flag && !in_array( $v2, $diff ) ) array_push( $diff, $v2 );   }
    
      return $diff; }