我的打印阵列无法正常工作。它在添加到数组之前回显$ articleID,因此变量不是null或任何东西。
然后我print_r($ magIDArray)它只是回显出'Array()';有人可以帮忙吗?
编辑:我已经调用了该函数,否则它不会回显函数中的变量 - 它是wouldent echo $ articleID,但它已经完成了
$sql = mysql_query("SELECT * FROM master_k2_categories WHERE parent = '$catid' ORDER
BY id DESC");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$name = $row["name"];
getMagArticleID($id);
}
$magIDArray = array();
function getMagArticleID($id){
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
}
print_r($magIDArray);
答案 0 :(得分:1)
您需要使用global
关键字:
$magIDArray = array();
function getMagArticleID($id){
global $magIDArray ;
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
}
getMagArticleID($id);
print_r($magIDArray);
看看变量范围:http://php.net/manual/en/language.variables.scope.php
答案 1 :(得分:1)
您需要在尝试回显值之前运行您的功能
编辑:作为undone said - 您还必须使用全局变量或在函数中返回值
答案 2 :(得分:1)
你错过了两件事:
1)忘记调用该函数。
2)忘记返回功能值。
更新的代码:
<?php
function getMagArticleID($id){
$magIDArray = array();
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
return $magIDArray;
}
$magIDArray = getMagArticleID($id);
print_r($magIDArray);
答案 3 :(得分:0)
你需要先调用这个函数,所以我会这样做:
$magIDArray = array();
function getMagArticleID($id){
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
return $magIDArray;
}
$magIDfunction = getMagArticleID($id);
print_r($magIDfunction);
(UNTESTED)
答案 4 :(得分:0)
试试这个:
$magIDArray = array();
function getMagArticleID($id){
global $magIDArray;
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
return $magIDArray;
}
getMagArticleID(*parameter*);
print_r($magIDArray);
答案 5 :(得分:0)
你的数组在函数范围内被“填充”,因此它的值在全局范围内不可用。
您可以全局化var:
function getMagArticleID($id){
global $magIDArray;
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
}
或者您可以通过函数
返回数组function getMagArticleID($id){
$magIDArray=Array();
$magID = $id;
$sqlID = mysql_query("SELECT * FROM master_k2_items WHERE catid = '$magID' ORDER BY id DESC");
while($row = mysql_fetch_array($sqlID)){
$articleID = $row["id"];
echo '<br>', $articleID;
$magIDArray[] = $articleID;
}
return $magIDArray;
}
$magIDArray=getMagArticleID($id);
也就是说,除了其他替代方法,比如通过引用传递数组或将其全部包装在$ madIDArray是私有类var的类中,或将$ madIDArray声明为对象并让PDO获取绑定到对象的结果。但我相信你会使用最简单的解决方案。