Codeigniter:如何更改结果数组

时间:2019-03-21 06:18:05

标签: php arrays api codeigniter

如何像这样更改结果数组

enter image description here

喜欢这样

enter image description here

这是我的控制器

public function index(){

    $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
    $responseData = $response->body; 
    $arrData = json_decode($responseData,true); 

    echo "<pre>";
    print_r($arrData);
    echo "</pre>";
    $this->load->view('welcome_message');

}

4 个答案:

答案 0 :(得分:0)

在HTML表中这样做

$data['arrData'] = json_decode($responseData,true);

在视图$this->load->view('welcome_message', $data);

中传递数据
<?php foreach($arrData as $data): ?>
 <tr>
    <td><?= $data['user_firstname'] ?></td>
 </tr>
<?php endforeach; ?>

答案 1 :(得分:0)

控制器

public function index(){

    $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
    $responseData = $response->body; 
    $arrData = json_decode($responseData,true);
    $data = array(); 
    $data['arrData'] = $arrData;

    $this->load->view('welcome_message',$data);

}

查看代码
首先,您需要设置标题,然后设置

<?php foreach($arrData as $data){ ?>
 <tr>
    <td><?php echo $data['user_firstname'] ?></td>
 </tr>
<?php } ?>

答案 2 :(得分:0)

控制器:首先将数据从控制器传递到视图:

public function index(){

    $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
    $responseData = $response->body; 
    $arrData = json_decode($responseData,true); 

    // Create an Data Array to store all the data
    // that you need to pass to your view
    $data['user'] = $arrData; 

    $this->load->view('welcome_message', $data);

}

视图:然后在您的视图文件中创建一个表并使它动态使用需要显示的数据:

<?php foreach($user as $key => $value) : ?>
<tr>
    <td><?= $value['user_firstname'] ?></td>
    <td><?= $value['user_lastname'] ?></td>
    ...
    ..
    .
</tr>
<?php endforeach; ?>

答案 3 :(得分:0)

更改控制器以传递$arrData数据:

public function index(){

    $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
    $responseData = $response->body; 
    $data['arrData'] = json_decode($responseData,true); 

    $this->load->view('welcome_message', $data);

}

并尝试在welcome_message.php视图文件中进行以下操作:

<table class="table table-hover">
    <thead>
        <tr>
            <th>The Title</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($arrData as $data): ?>
        <tr>
            <td><?= $data['user_firstname'] ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>