Home >Backend Development >PHP Tutorial >php中如何在数据集中增加一个字段?

php中如何在数据集中增加一个字段?

WBOY
WBOYOriginal
2016-06-06 20:46:331630browse

原先是从表里返回一个数据集list,
在前端显示:

<code><?php foreach ($this->list AS $key => $val) { ?>
        <input value="<?php echo $val['title']; ?>">
<?php } ?>
</code>

现在想在数据集里临时增加一个字段的数据,供前端显示,这个数据来自别的表。

回复内容:

原先是从表里返回一个数据集list,
在前端显示:

<code><?php foreach ($this->list AS $key => $val) { ?>
        <input value="<?php echo $val['title']; ?>">
<?php } ?>
</code>

现在想在数据集里临时增加一个字段的数据,供前端显示,这个数据来自别的表。

如果我没有理解错误的话,可以这样做

<code>$list = array(
    'fruit' => array(
        'you' => 'pear',
        'I' => 'apple'
    ),
    'size' => array(
        'you' => 'big',
        'I' => 'small'
    )
);//origin list

$title = array(
    'you' => 'king',
    'I' => 'general'
);//new data

$list['title'] = $title;//add new data to list
</code>

用 array_map 来做很简单:

<code class="lang-php">$this->list = array_map(function($item) {
  $item['tmp'] = 'Some value';
  return $item;
}, $this->list)
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn