在PHP中,在某个键值之后插入键值对

在PHP中,在某个键值之后插入键值对,php,Php,我试图在从数据库查询检索到的值数组的末尾插入一个键值对。我尝试了array\u-splice,但这并不是我想要的,所以我尝试了array\u-push,但它没有将其添加到阵列中 这是我的密码: public function listFriendsStatus() { // get the friend statuses of the logged in user $connection = $this->sql->getAdapter()->getDriver

我试图在从数据库查询检索到的值数组的末尾插入一个键值对。我尝试了
array\u-splice
,但这并不是我想要的,所以我尝试了
array\u-push
,但它没有将其添加到阵列中

这是我的密码:

public function listFriendsStatus()
{
    // get the friend statuses of the logged in user
    $connection = $this->sql->getAdapter()->getDriver()->getConnection();

    $query = $connection->execute("SELECT status.id, status, time_status, members.username FROM status
        INNER JOIN friends ON friends.friend_id = status.id 
        INNER JOIN members ON members.id = status.id
        WHERE friends.user_id = " . $this->getUserId()['id']);

    if ($query->count() > 0) {
        $status_holder = array();

        foreach ($query as $key => $value) {
            $status_holder[$key] = $value;

            $status_dir = '/images/profile/' . $status_holder[$key]['username'] . '/status/' . $status_holder[$key]['time_status'] . '/';

            $real_dir = getcwd() . '/public/' . $status_dir;

            if (is_dir($real_dir)) {
                $images = array_diff(scandir($real_dir, 1), array('.', '..'));
                array_push($status_holder, array('images' => $images));
            } else {
                array_push($status_holder, array('images' => ''));
            }
        }

        return $status_holder;
    } else {
        throw new FeedException("No friend statuses were found.");
    }
}
为了更好地解释,这里是我的数组的
var\u导出

array ( 0 => array ( 'id' => '2', 'status' => 'this is jimmy, test', 'time_status' => '0', 'username' => 'jimmy', ), 1 => array ( 'id' => '3', 'status' => 'this is jimmy 2, test', 'time_status' => '0', 'username' => 'timmy', ), 2 => array ( 'images' => '', ), )
我试图做的是在每个
[“username”]
键和值之后插入images数组中的images键和值

任何帮助都将不胜感激

谢谢

(如果需要的话,我可以试着把问题弄清楚)

更新

$status_holder[$key]['images'] = $images;

做了我需要的。

如果您的foreach一次检索一行,则将图像添加到此数据中,然后构建另一个数组

foreach ($query as $key => $value) {
    $status_dir = '/images/profile/' . $value['username'] . '/status/' . $value['time_status'] . '/';

    $real_dir = getcwd() . '/public/' . $status_dir;

    if (is_dir($real_dir)) {
        $images = array_diff(scandir($real_dir, 1), array('.', '..'));
        $value['images'] = $images;
    } else {
        $value['images'] = '';
    }
    $status_holder[$key] = $value;
}

如果foreach一次检索一行,则将图像添加到此数据中,然后构建另一个数组

foreach ($query as $key => $value) {
    $status_dir = '/images/profile/' . $value['username'] . '/status/' . $value['time_status'] . '/';

    $real_dir = getcwd() . '/public/' . $status_dir;

    if (is_dir($real_dir)) {
        $images = array_diff(scandir($real_dir, 1), array('.', '..'));
        $value['images'] = $images;
    } else {
        $value['images'] = '';
    }
    $status_holder[$key] = $value;
}

您应该打开错误显示:您所做的是不可能的。你误解了
end()
的作用;它返回数组的最后一个元素。您不能在某个键值对之后插入键值对吗?可以,但我指的是您的代码:
array\u push()
的第一个参数是通过引用传递的,您不能向
end()
函数添加另一个元素。显示错误或检查错误日志会立即显示出来。只需将键值对添加到数组中,不必担心它的位置。要获得更好的解释,请不要使用
var\u dump()
。使用。你应该打开错误显示:你正在做的是不可能的。你误解了
end()
的作用;它返回数组的最后一个元素。您不能在某个键值对之后插入键值对吗?可以,但我指的是您的代码:
array\u push()
的第一个参数是通过引用传递的,您不能向
end()
函数添加另一个元素。显示错误或检查错误日志会立即显示出来。只需将键值对添加到数组中,不必担心它的位置。要获得更好的解释,请不要使用
var\u dump()
。使用。