PHP中的关联数组数组

时间:2021-01-18 21:41:15

I have a function to use but I don't know the content of this function. The only thing I know is that the function returns an array of associative arrays and the keys for the arrays. The data that the function returns come from a database. Can you help in how to read the data from this array? I am confused with the arrays. For now I am doing this:

我有一个功能,但我不知道这个功能的内容。我唯一知道的是该函数返回一个关联数组数组和数组的键。函数返回的数据来自数据库。你能帮忙学习如何从这个数组中读取数据吗?我对数组感到困惑。现在我这样做:

$array = myfunction($var);
if(!empty($array))
{
    while($row = mysql_fetch_array($array))
    {
        print"$row[elem1]
        $row[elem2]";
    }
}

I take the error: Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in... I know that something is missing, but I till now I can't fix it.

我接受错误:警告:mysql_fetch_array()期望参数1是资源,在...中给出的数组我知道有些东西丢失了,但直到现在我无法解决它。

4 个解决方案

#1


1  

Something like -

就像是 -

$array = myfunction($var);
foreach($array as $key => $row) {
    print"{$row['elem1']} {$row['elem2']}";
}

#2


2  

If the function is returning an array then why are you using it in mysql_fetch_array. It is useless. Instead use this

如果函数返回一个数组,那你为什么要在mysql_fetch_array中使用它。这没用。而是使用它

foreach($array as $key => $value){

   echo $key;
   echo '<br>';
   echo $value;
}

This will print the whole array. Or a short method is

这将打印整个阵列。或者简短的方法是

echo '<pre>';
print_r($array);
echo '</pre>';

#3


0  

You have to pass Resource Identifier to mysql_fetch_array() function.

您必须将资源标识符传递给mysql_fetch_array()函数。

Something like:

就像是:

$sql = mysql_query('SELECT * FROM `table`');
if(!empty($array)) {

 while($row = mysql_fetch_array($array)) {
        print"$row[elem1]
        $row[elem2]";
 }
}

#4


0  

The error is correct .. the issue is not associative arrays in php but you are not using a valid mysql resource

错误是正确的..问题不是php中的关联数组,但您没有使用有效的mysql资源

Please see http://php.net/manual/en/function.mysql-fetch-array.php for documentation

有关文档,请参阅http://php.net/manual/en/function.mysql-fetch-array.php

Examples

例子

$mysqli = new mysqli("localhost","root","","test");
$result = $mysqli->query("SELECT * FROM test");
$row = array() ;
echo "<pre>" ;
if($result->num_rows > 0)
{
    while($row = $result->fetch_array(MYSQLI_NUM))
    {
            print implode (",", $row) . PHP_EOL;

    }

}

Or

要么

$array = myfunction($var);
if(!empty($array))
{
    foreach($array as $key => $row)
    {
        if(is_array($row))
        {
            print implode (",", $row) . PHP_EOL;
        }
        else
        {
            print $row . PHP_EOL ;
        }
    }

}

#1


1  

Something like -

就像是 -

$array = myfunction($var);
foreach($array as $key => $row) {
    print"{$row['elem1']} {$row['elem2']}";
}

#2


2  

If the function is returning an array then why are you using it in mysql_fetch_array. It is useless. Instead use this

如果函数返回一个数组,那你为什么要在mysql_fetch_array中使用它。这没用。而是使用它

foreach($array as $key => $value){

   echo $key;
   echo '<br>';
   echo $value;
}

This will print the whole array. Or a short method is

这将打印整个阵列。或者简短的方法是

echo '<pre>';
print_r($array);
echo '</pre>';

#3


0  

You have to pass Resource Identifier to mysql_fetch_array() function.

您必须将资源标识符传递给mysql_fetch_array()函数。

Something like:

就像是:

$sql = mysql_query('SELECT * FROM `table`');
if(!empty($array)) {

 while($row = mysql_fetch_array($array)) {
        print"$row[elem1]
        $row[elem2]";
 }
}

#4


0  

The error is correct .. the issue is not associative arrays in php but you are not using a valid mysql resource

错误是正确的..问题不是php中的关联数组,但您没有使用有效的mysql资源

Please see http://php.net/manual/en/function.mysql-fetch-array.php for documentation

有关文档,请参阅http://php.net/manual/en/function.mysql-fetch-array.php

Examples

例子

$mysqli = new mysqli("localhost","root","","test");
$result = $mysqli->query("SELECT * FROM test");
$row = array() ;
echo "<pre>" ;
if($result->num_rows > 0)
{
    while($row = $result->fetch_array(MYSQLI_NUM))
    {
            print implode (",", $row) . PHP_EOL;

    }

}

Or

要么

$array = myfunction($var);
if(!empty($array))
{
    foreach($array as $key => $row)
    {
        if(is_array($row))
        {
            print implode (",", $row) . PHP_EOL;
        }
        else
        {
            print $row . PHP_EOL ;
        }
    }

}