将数组中的每个对象换行,然后将其值返回为

时间:2017-04-30 00:23:04

标签: php loops oop foreach

我有对象数组,我尝试遍历此数组并将此对象中的每个对象包装在<tr></tr>标记中,然后将此特定对象的所有值都返回为<td></td>

请求任何支持,我坚持到那里。

  private function has_the_attribute($key) {
        $object_prop = get_object_vars($this);
        return array_key_exists($key, $object_prop);
      } // has atribute
  public static function instantation($the_record) {
    $the_object = new self;

    foreach ($the_record as $key => $value) {
      if($the_object->has_the_attribute($key)) {
        $the_object->$key = $value;
      }
    }

    return $the_object;

  } // instantation

  public static function find_this_query($sql) {
    global $database;
    $result_set = $database->query($sql);
    $the_object_array = array();

    while($row = mysqli_fetch_array($result_set)) {
      $the_object_array[] = self::instantation($row);
    }

    return $the_object_array;

  } // find_this_query

public static function show_user_tasks($user_id) {
    global $database;
    $the_result_array = self::find_this_query("SELECT * FROM tasks WHERE user_id = '{$user_id}' ");
    !empty($the_result_array) ? array_shift($the_result_array) : false;
    // print_r ($the_result_array);

    for($i = 0; $i < count($the_result_array) - 1; $i++) {
      echo "<tr>";
      foreach($the_result_array[$i] as $key => $value) {
        echo "<td>" . $key->$value . "</td>";
      }
      echo "</tr>";
    }

    }

这里也是var_dump

     array (size=25)
  0 => 
    object(Task)[8]
      public 'id' => string '1' (length=1)
      public 'name' => string '432 Dell Inspirian 500 Windows 7 Sat, 29 Apr 2017 23:43:10 +0200' (length=64)
      public 'pc_type' => string 'Dell Inspirian 500' (length=18)
      public 'os_type' => string 'Windows 7' (length=9)
      public 'description' => string 'Mam duży problem' (length=17)
      public 'status' => string '1' (length=1)
      public 'comments' => null
      public 'start_date' => string '2017-04-29 23:43:10' (length=19)
      public 'end_date' => null
      public 'user_id' => string '432' (length=3)
      public 'agent_id' => string '901' (length=3)
  1 => 
    object(Task)[9]
      public 'id' => string '2' (length=1)
      public 'name' => string '#432 Dell Inspirian 500 Windows 7 Sun, 30 Apr 2017 00:22:48 +0200' (length=65)
      public 'pc_type' => string 'Dell Inspirian 500' (length=18)
      public 'os_type' => string 'Windows 7' (length=9)
      public 'description' => string 'dadasda' (length=7)
      public 'status' => string '1' (length=1)
      public 'comments' => null
      public 'start_date' => string '2017-04-30 00:22:48' (length=19)
      public 'end_date' => null
      public 'user_id' => string '432' (length=3)
      public 'agent_id' => string '901' (length=3)
      ...

0 个答案:

没有答案