PHP从First Array获取密钥

时间:2016-02-18 17:03:39

标签: php arrays

我有一个从数据库返回的数据集,键是我需要在表格上打印的字段的名称。

例如,如果我有一个包含5个键(字段)的数组,我需要创建一个HTML表,其中包含每个键的标题。然后我需要将结果打印到表格中。

基本上,我需要从数组创建一个HTML表,第一个结果中的键可以作为标题。

这是我的数据:

<img>

从数组中的第一个结果中获取 SimpleXMLElement Object ( [data] => SimpleXMLElement Object ( [primaryFields] => SimpleXMLElement Object ( [primary] => Array ( [0] => SimpleXMLElement Object ( [Project_Title] => Test [Project_Description_Preview] => Test Desc ) [1] => SimpleXMLElement Object ( [Project_Title] => Blueprint Development Project [Project_Description_Preview] => We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features. ) 的最佳方法是什么?这应该是两个独立的循环;第一个循环从键创建标题然后第二个打印数据?

keys&amp;在这种情况下,Project_Title将是表头。

4 个答案:

答案 0 :(得分:1)

我会留给你解析你得到的数据。从对象的外观到主要。

<?php
//You will need to parse the object to get this.
$primary = array(
    array(
      "Project_Title" => "Test",
      "Project_Description_Preview" => "Test Desc"
    ),
    array(
      "Project_Title" => "Blueprint Development Project",
      "Project_Description_Preview" => "We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features."
    )
);

echo "<table>";
for($i = 0; $i < count($primary); $i++){
//Only create the head on your first object
if($i==0){
  echo "<thead>";
  //build header here
  echo "<tr>";
  foreach($primary[$i] as $key => $value){
      echo "<th>" . $key . "</th>";
  }
  echo "</tr>";
  echo "</thead><tbody>";
}
//Then create the rows and data like all php...
echo "<tr>";

foreach($primary[$i] as $value){
  echo "<td>" . $value . "</td>";
}
echo "</tr>";

}
echo "</tbody></table>";

?>

答案 1 :(得分:0)

我的典型方法是在第一次迭代时首先进入数据时抓住键,假设你正在循环它。

像这样循环(奖励让我们提取密钥以防万一):

public static List<string> ProcessTriangles(List<string> points, List<string> connections)
    {
        List<string> trianglesOut = new List<string>();

        for (int i = 0; i < connections.Count; i++)
        {
            string[] items = connections[i].Split(',');
            int[] indices = new int[6];
            for (int j = 0; j < 3; j++) indices[j] = int.Parse(items[j]);

            Point v0 = Point.FromString(points[indices[0]]);
            Point v1 = Point.FromString(points[indices[1]]);
            Point v2 = Point.FromString(points[indices[2]]);

            Triangle tri = new Triangle(v0, v1, v2);
            trianglesOut.Add(tri.ToString());

            //--i; /** REMOVE THIS **/
        }

        return trianglesOut;
    }

但是,你总能做到:

$primary = $obj->data->primaryFields->primary; //an array
$first = true;
$keys = array();
foreach ($primary as $obj) {
  if ($first) {
    $first = false;
    $keys = get_array_keys((array)$obj));
    make_headers($obj,$keys); //do header things here
  }
  make_table_stuff($obj,$keys); //do table stuff
}

答案 2 :(得分:0)

你必须从某个地方浏览你的阵列,比如说

$arr->data->primaryFields->primary$primary

考虑来自primary的数组。

foreach($primary as $values){
    foreach($values as $key => $ value){
        echo $key." & ".$value;
    }
}

答案 3 :(得分:0)

注意:这适用于任何数量的数据库列,无论它们是否已知。

您应该能够像这样构建整个表:

//where $obj = your output...

//get the column names, convert to an object and store inside an array of one element
$keys = array(json_decode(json_encode(array_keys(get_object_vars($obj[0])))));

//create the output array by merging the keys as the first element with the existing dataset into a new array
$output = array_merge($keys, $obj);

//here is the output step
echo "<table><thead>";
foreach($output as $record_num => $record) {
    echo '<tr>';
    $cells = get_object_vars($record);
    foreach ($cells as $column => $cell) {
        echo '<td class="' . $column . '">' . $cell . '</td>';
    }
    echo '</tr>';

    //if this is the first element of the array, it is the column names. After these are output, the rest should be in the tbody tag
    if ($record_num == 0) { echo '</thead><tbody>';}
}
echo "</tbody></table>";

这也为每个与其数据库列对应的单元格添加了一个类名,可以在UI考虑事项之后用于垂直css样式或javascript选择器。如果不需要,可以删除它。

相关问题