-4

I'm trying to use 'yield' in my algorithm and my PHP version is 5.4.

I'm looking for any guidance on using 'yield'.

this an FP-algorithim it works fine with php 5.5 but the rest of the project use php 5.4

    public function find_frequent_itemsets($transactions, $minimum_support){
    $processedTransactions = array();
    $items = array();
    //var_dump($transactions);
    foreach($transactions as $transaction){
      // if(!is_array($transaction)) 
      //continue;
        $processed = array();

        foreach($transaction as $item){

            // check if this item exists ?
            if(array_key_exists($item, $items)){
                $items[$item] += 1;
            }else{
                // not found
                $items[$item] = 1;
            }
            array_push($processed, $item);

        }// every transaction.

        array_push($processedTransactions, $processed);
    }// end first for each (transaction).


    // Remove infrequent items from the item support dictionary.
    foreach(array_keys($items) as $index){
        if($items[$index] < $minimum_support){
            unset($items[$index]);
        }
    }// end loop

    //var_dump($items);
    // sort Transaction in $processedTransactions.
    $sortedTransactions = array();
    //var_dump($processedTransactions);
    foreach($processedTransactions as $currentTransactions){
        $tmp = array();

        foreach($currentTransactions as $item){
            if(array_key_exists($item, $items)){
                // key exits = frequent item.
                //array_push($tmp, array($item, $items[$item]));
                $tmp[$item] = $items[$item];
            }
        }

        // sort based on most frequent item.
        arsort($tmp);
        //var_dump($tmp);
        array_push($sortedTransactions, array_keys($tmp));
    }


    $tree = new FPTree();

    // Add all Transactions.
    foreach($sortedTransactions as $currentTransactions){
        $tree->add($currentTransactions);
    }foreach(find_with_suffix($tree, array(), $minimum_support) as $itemset){
        yield $itemset;
    }

}}
   function find_with_suffix($tree, $suffix, $minimum_support){
    foreach($tree->getItems() as $element){
    $item = $element[0];
    $nodes = $element[1];
    $support = 0;
    foreach($nodes as $n){
        $support += $n->getCount();
    }
      if($support >= $minimum_support && !array_key_exists($item, $suffix)){
        $found_set = array_merge(array($item), $suffix);
        $wanted=array();
        $foo= implode(", ", $found_set) ;
        $wanted[$foo]=$support;
        yield $wanted;                          $condTree=conditional_tree_from_paths($tree->prefixPaths($item),$minimum_support);
      foreach(find_with_suffix($condTree, $found_set, $minimum_support) as s){
            yield $s;
        }
    }
}

1 Answers1

0

As this post on stackoverflow tells us, it is rather "costly" to change your algorithm not to use yield and thus make it compatible with a version of php <= 5.4.

On the other side an upgrade to php 5.5 or even better 5.6 is relatively easy. Unfortunately you didn't provide details why you are still with 5.4 and if there are reasons beyond your control that keep you from updating.

I recommend you update your php version because being up to date with software is a matter of security too. Unless you are limited to Windows XP and Windows Server 2003 (which themselves are out of support) you should go for the update.

Community
  • 1
  • 1
Marged
  • 10,577
  • 10
  • 57
  • 99
  • I only do this task i can't change the version now the whole project runs on it please any help i don't know what to do and the deed line is Monday – Maiada Wagdy Jun 12 '15 at 06:06
  • @MaiadaWagdy then you have to rewrite your code to not use yield – Marged Jun 12 '15 at 06:11
  • I tried but it doesn't work when i tried and i don't know what to do – Maiada Wagdy Jun 12 '15 at 06:39
  • please can you help me – Maiada Wagdy Jun 12 '15 at 06:40
  • @MaiadaWagdy perhaps it is best if you create a new question or change this one. The focus of what you need is "how to remove yield-function from php algorithm to make it compatible with php < 5.5". Then either show your code with yield or (if it only has small problems) the code where you already tried to remove the yield. And removing the typos will help to attract people because it shows your diligence – Marged Jun 12 '15 at 11:09