将阵列添加到阵列

时间:2011-09-03 16:38:03

标签: php arrays

我已经能够在我的脚本中添加2个数组。问题是,如下所示,数组中的第二项是名称 [0] 我需要这样说 [user]

    [0] => Array
    (
        [id] => 1
        [0] => Array
            (
                [id] => 1
                [first_name] => Cody
                [last_name] => Robertson
                [email] => codyrob@me.com
                [password] => 
                [age] => 16
                [city] => Commerce Township
                [country] => United States Of America
                [friends] => 
                [avatar] => http://blazebyte.org/community/index.php?action=dlattach;attach=4415;type=avatar
                [register_date] => 2011-09-03
                [laststatus_date] => 0000-00-00
            )

        [status] => This is my first status. Will be pulled from API. :D
        [date] => 2011-09-02 23:26:09
    )

这是我的PHP代码。

    public function all_get()
{
    $feed = array();
    $data = array();

    $query = $this->db->query('SELECT * FROM `statuses`');

    if($query->num_rows() > 0)
    {
        $statuses = $query->result_array();

        foreach($statuses as $status)
        {
            $i = 0;

            $query = $this->db->query('SELECT * FROM `members` WHERE id='. $status['uid']);

            if($query->num_rows() > 0)
            {
                $user = $query->result_array();

                $first = array_slice($status, 0, 1, true);
                $secnd = array_slice($status, 2, null, true);

                foreach($user as $info => $value)
                {
                    $data[$info] = $value;
                }

                $feed[] = array_merge( (array) $first, (array) $data, (array) $secnd);
            }

        }   
    }

    return $feed;
}

1 个答案:

答案 0 :(得分:4)

快速修复将:(在返回前插入此内容)

foreach($feed as &$f) {
  $f['user']=$f[0];
  unset($f[0]);
}