Array To String PHP/PDO

Strafi

Erfahrenes Mitglied
Hallo,

Ich Habe folgende Fehlerausgabe:

Notice: Undefined variable: tempArray in request.php on line 30

Da läuft folgende Funktion:

PHP:
function arrayToString($array,$column, $seperator=" ") {
		$string="";

		for($i=0;$i<count($array);$i++) {
			$tempArray[]=$array[$i][$column];

		}

		if(count($tempArray)) {           //Hier ist zeile 30

			$string=implode($seperator,$tempArray);

		}
		
		return $string;
	}

Und so über gebe ich die Werte an die Funktion:

PHP:
$sql = ("
				SELECT
					name
				FROM
					transnamen
				WHERE
					t_id = :t_id
				");
				$stmt = $db->prepare($sql);
				$stmt->bindParam(':t_id', $transaction['id'], PDO::PARAM_STR);
				$stmt->execute();
				
				while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
				$names[] = $row;
				}
				
				$tNames=arrayToString($names,"name");


Im $array ist auch ein Array drin, das sieht dann so aus...

array(3) { [0]=> array(1) { ["vorname"]=> string(8) "Markus" } [1]=> array(1) { ["vorname"]=> string(5) "Sebastian" } [2]=> array(1) { ["vorname"]=> string(10) "Klaus" } }

Warum setzt der das $tempArray[] nicht?
 
Weil es nicht definiert wurde.

PHP:
function arrayToString($array,$column, $seperator=" ") {
        $string="";
        $tempArray = array();
        for($i=0;$i<count($array);$i++) {
            $tempArray[]=$array[$i][$column];

        }

        if(count($tempArray)) {           //Hier ist zeile 30

            $string=implode($seperator,$tempArray);

        }
        
        return $string;
    }
 
Ja das sind die Momente, in den ich mir gerne selbst eine über den Schädel ziehen würde.

Danke dir!
 

Neue Beiträge

Zurück