0

I'm running into an "Cannot redeclare" error and I can't figure out how to fix it. So I have a few functions in a php file located below. Now these functions iterate over an array of data.

I think I've surmised that the problem is that I'm looping the function over and over again in the foreach loop, and its the foreach loop thats been the problem. It seems like its already writing one the function to memory the first time and then for some reason it doesn't like being evoked again.

Your help appreciated.

P.S I've seen a number of similar posts on the issue such as Fatal error: Cannot redeclare but that doesn't seem to work.

<?php
// *****Code Omitted from Stack**** 


function postHelper($data, $field1, $field2)
{ //TODO Abstract and make sure post Helper  and modify Post can be the same thing.
    $result = array();
    for ($j = 0; $j < count($data); ++$j) { //iterator over array
        if ($field2 == "") {
            $result[$j] = $data[$j][$field1];
        } else {
            return $result[$j] = $data[$j][$field1][$field2];
        }
    }
    return $result;


}

//returns an array with only @ and # values 

function modifyPost($data)
{

//puts symbol @ before read data

    function addSymbol($data, $field1, $field2)
    {
        $info = postHelper($data, $field1, $field2);
        foreach ($info as &$n) {
            $n = '@' . $n;
        }
        print_r($info);
    }


    /*
     Parse texts and returns an array with only @ or # signs used
    */


    function parseText($data)
    {

        $newarr = array();
        $text = postHelper($data, "text", "");

        foreach ($text as &$s) { //separates into words
            $ex = explode(" ", $s);
            foreach ($ex as &$n) { //if text doesnt' begin with '@' or '#' then throw it out.
                if (substr($n, 0, 1) === '@' || strpos($n, '#') !== false) {
                    array_push($newarr, $n . ',');
                }

            }

        }

        return $newarr;
    }
}


foreach ($posts as $entry) {
    if (!function_exists('modifyPost')) {
        $nval = "hello";
        modifyPost($entry);
        $entry['mod_post'] = $nval;
    }
}

?>

EDIT: I've solved the error. Turns out that the original posts did actually work. I messed in naming. I will give points to anyone who can explain to me why this is necessary for a call. Moreover, I will update post if there is an additional questions that I have.

Community
  • 1
  • 1
andor kesselman
  • 1,089
  • 2
  • 15
  • 26

2 Answers2

1

The error says it all. You have duplicate modifyData() & parseText functions.

Remove the top half of the php file so only one of each occurs.

  • Sorry I just caught on, that was a mistake in copying from the original file onto stackoverflow. Not present in the acutal file. I will fix the post. My apologies. – andor kesselman Aug 30 '14 at 10:16
1

Php doesn't support nested functions. Although you technically can declare a function within a function:

function modifyPost($data)
{


    function addSymbol($data, $field1, $field2)

the inner function becomes global, and the second attempt to declare it (by calling the outer function once again) will fail.

This behaviour seems counter-intuitive, but this is how it works at the moment. There's RFC about real nested functions, which also lists several workarounds for the problem.

georg
  • 211,518
  • 52
  • 313
  • 390