array_diff数组到错误的字符串转换

时间:2015-06-30 13:17:21

标签: php arrays

我想计算不同的两个数组,但我得到错误;

Notice:  Array to string conversion in x.php on line 255

而不是计算不同。

代码:

            $db->where('lisansID', $_POST['licence']);
            $mActivation = $db->get('moduleactivation', null, 'modulID');
            $aktifler = Array();
            $gelenler = Array();
            foreach($mActivation as $key=>$val)
            {
                $aktifler[] =   $val;
            }

            foreach ($_POST['module'] as $key => $value) {
                $gelenler[] = $val;
            }
            echo '<pre>Aktifler: ';
            print_r($aktifler);
            echo '</pre>';
            echo '<pre> Gelenler:';
            print_r($gelenler);
            echo '</pre> Fark:';
///line 255: 
            var_dump(array_diff($aktifler, $gelenler));

2 个答案:

答案 0 :(得分:4)

<div id="carousel" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div class="item" ng-repeat="item in coll" ng-class="{active: $index==0}" nx-on-finish-render="ngRepeatFinished"> <a href="{{baseUrl}}{{item.slug}}" class="column-post-img"> <img ng-src="{{imgUrl}}{{item.image}}" class="img-responsive post-img" alt="{{item.title}}" ng-if="item.image"> <img src="/imgs/950X350.jpg" class="img-responsive post-img" alt="{{item.title}}" ng-if="!item.image"> <div class="carousel-caption"> <h3>{{item.title | limitTo:100}}</h3> <p>{{item.summary | limitTo: 100}}</p> </div> </a> </div> </div> <ul class="nav carousel nav-pills nav-justified"> <li data-target="#carousel" data-slide-to="{{$index}}" ng-repeat="item in coll" ng-class="{active: $index==0}" nx-on-finish-render="ngRepeatFinished"><a data-target="#">{{item.title | limitTo:100}}</a></li> </ul> </div> 只能比较可以投放到array_diff的字符串或值。但是(string)$aktifler的元素本身就是数组,这就是为什么你得到这个通知(同样,将数组转换为字符串总是会产生字符串&#34;数组&#34 ;,所以所有数组都将被视为相等的。)

请参阅array_diff

  

注意:

     

当且仅当(string)$ elem1 ===(string)$ elem2 时,才认为两个元素相等。用文字表示:当字符串表示相同时。

改为使用array_udiff,您可以在其中定义自己的比较函数。

$gelenler

答案 1 :(得分:0)

Try breaking the final part into steps.

  • Take the row,
  • Save the output,
  • display the output.

thus:

$out = array_diff($aktifler, $gelenler);
var_dump($out);

You have a typo in your code, on the second foreach the $value is not assigned to the array, which is instead given $val. That's your problem.

Also you should get into the habit of unset()ing values from the foreach loop once the loop has completed.

foreach($a as $b => $c){
...
}
unset($b,$c);