Suppose I want a hashtag in a twitter-like post to be stored in its own hashtags table. The issue I'm running into is that it appears as though this loop is storing the string "Array" in the hashtag name column, instead of the actual match. This appears to be an issue with the fact that the returned array is multidimensional?
preg_match_all("/\S*#(?:\[[^\]]+\]|\S+)/", $body, $matches);
//for each loop array returned in matches
//create_hashtag()
//ISSUE with dimensionality of array
foreach($matches[0] as $v){
create_hashtag($dbh, $v);
attach_hashtag($dbh, $pid, $v);
}
As you can see, I tried to address this by calling the arrays at the 0th spot, but that didn't work.
Maybe the issue is in the called functions:
function attach_hashtag($dbh, $pid, $tagname) {
//insert into Tagged
try {
$sql = 'INSERT INTO Tagged (hashtag_name, post_id)
VALUES (:name, :pid);';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':name', $tagname);
$stmt->bindParam(':pid', $pid);
$stmt->execute();
$array = [
'status' => 1,
];
return $array;
} catch (Exception $ex){
$array = [
'status' => 0,
];
return $array;
}}
And
function create_hashtag($dbh, $tagname) {
try {
$sql = 'INSERT INTO Hashtags (name)
VALUES (:name);';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':name', $tagname);
$stmt->execute();
$array = [
'status' => 1,
];
return $array;
} catch (Exception $ex){
$array = [
'status' => 0,
];
return $array;
}
//insert into Hashtags}
EDIT: I used var_dump to make sure the query works.
//var_dump($pid); returns proper value
preg_match_all("/\S*#(?:\[[^\]]+\]|\S+)/", $body, $matches);
//for each loop array returned in matches
//echo $matches['Array']; - why does this return "Array"
//ISSUE with dimensionality of array
foreach($matches['Array'] as $v){
create_hashtag($dbh, $v);
attach_hashtag($dbh, $pid, $v); //inserts into tagged,
}