Take the 2-minute tour ×
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It's 100% free, no registration required.

I have a strange PHP array issue which I am really confused about... I'm using PHP to take a stash array from within ExpressionEngine, make sure the values are unique, and then using array_intersect to compare to another array - for test purposes the arrays have identical values...

<?php
    $faves = '{exp:stash:get name="skills" scope="user"}';
    $faves = rtrim($faves, "|");
    $allfavourites = explode('|', $faves);      
    $allfavourites = array_unique($allfavourites);

    $this_barristers_fave_ids = array(6,7,8,9,10,11,12,13,14,15,16,17,4,5,3,1,2);

    echo "<br/>There are <strong>".sizeof($allfavourites)."</strong> skills in the faves array after being made unique";

     $num = count(array_intersect($finalfaves, $this_barristers_fave_ids));
     echo "<br/><br/>num is ".$num; // this outputs '13' ?>

The hardcoded '$this_barristers_fave_ids' has exactly the same contents as the $allfavourites array (17 values), so I am expecting the $num variable to output '17'. If I output $faves on about line 3 before exploding it, the output is exactly the contents of the hardcoded '$this_barristers_fave_ids' - I am so confused as to why the number I am getting outputted is 13 instead of 17 - am I missing a stage after the explode and the array_unique that somehow normalises the array and so I will get the number expected?

I'd be so grateful as always for any help...

Many thanks!

share|improve this question
    
What is the content of $faves? Can you do a var_dump for it. Array_intersect creates an array consisting of values that are in BOTH arrays so perhaps they simply do not contain the same values. You are also intersecting something called $finalfaves which you haven't defined anywhere in this code. –  foamcow Oct 15 '14 at 11:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.