基于键值在PHP中解析数组

时间:2021-04-30 22:09:22

Basic php question here. The following SQL code returns an array containing a key called 'date'. All I want to do is parse the 'date' value based on the key name. Any help?

这里有基本的php问题。以下SQL代码返回一个包含名为“date”的键的数组。我想要做的就是根据键名解析'date'值。有帮助吗?

$result = mysql_query("SELECT * FROM `table` WHERE columnName ='value'") or die(mysql_error());
 $data = array();
while ( $row = mysql_fetch_assoc($result) )
{
  $data[] = $row;
}
echo $data->{'date'}

4 个解决方案

#1


1  

Ok here you go

好的,你去吧

With Foreach

foreach($data as $key => $value)
{
   if($key == 'date')
   {
      // do you parsing stuff
   }
}

Without foreach

$parsing_date = $data['date'];

#2


1  

You're using object syntax on an array which you can't do.

您正在使用不能执行的数组上的对象语法。

 echo $data['date'],

#3


0  

foreach($data as $key => $value)
{
    if ($key == 'date')
    {
        echo "Date Value: '".$value."'";
        // Do your stuffs...
        echo "Date Final Value: '".$value."'";
    } 
}

#4


0  

mysql_fetch_assoc returns an ASSOCiative array. Which means it returns an array with your table name fields as keys. For example if your table has 3 columns 'date', 'name', and 'color'

mysql_fetch_assoc返回一个ASSOCiative数组。这意味着它返回一个数组,其中您的表名字段为键。例如,如果您的表有3列'date','name'和'color'

Then you would access those fields in the following way.

然后,您将以下列方式访问这些字段。

$result = mysql_query("SELECT * FROM `table` WHERE columnName ='value'") or die(mysql_error());
 $data = array();

while ( $row = mysql_fetch_assoc($result) )
{
  echo $row['date']; //Prints the rows date field
  echo $row['name']; //Prints the row name field and so on.
}

#1


1  

Ok here you go

好的,你去吧

With Foreach

foreach($data as $key => $value)
{
   if($key == 'date')
   {
      // do you parsing stuff
   }
}

Without foreach

$parsing_date = $data['date'];

#2


1  

You're using object syntax on an array which you can't do.

您正在使用不能执行的数组上的对象语法。

 echo $data['date'],

#3


0  

foreach($data as $key => $value)
{
    if ($key == 'date')
    {
        echo "Date Value: '".$value."'";
        // Do your stuffs...
        echo "Date Final Value: '".$value."'";
    } 
}

#4


0  

mysql_fetch_assoc returns an ASSOCiative array. Which means it returns an array with your table name fields as keys. For example if your table has 3 columns 'date', 'name', and 'color'

mysql_fetch_assoc返回一个ASSOCiative数组。这意味着它返回一个数组,其中您的表名字段为键。例如,如果您的表有3列'date','name'和'color'

Then you would access those fields in the following way.

然后,您将以下列方式访问这些字段。

$result = mysql_query("SELECT * FROM `table` WHERE columnName ='value'") or die(mysql_error());
 $data = array();

while ( $row = mysql_fetch_assoc($result) )
{
  echo $row['date']; //Prints the rows date field
  echo $row['name']; //Prints the row name field and so on.
}