Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] array merge do not work correctly
#1

[eluser]kolxoznik1[/eluser]
I have array from database :

Code:
Array
(
    [0] => Array
        (
            [user] => 6666
            [user_image] => user_6_last_image
            [image] => user_6_last_image
            [date] => 2012-01-25 00:13:08
        )

    [1] => Array
        (
            [user] => 8888
            [user_image] => second.jpg
            [image] => second.jpg
            [date] => 2012-01-24 14:42:27
        )

    [2] => Array
        (
            [user] => 8888
            [user_image] => second.jpg
            [image] => first.jpg
            [date] => 2012-01-24 14:42:27
        )

    [3] => Array
        (
            [user] => 3333
            [user_image] => ax46l7v7vugnesk10whk_339.jpg
            [image] => ax46l7v7vugnesk10whk_339.jpg
            [date] => 2012-01-24 01:54:19
        )

    [4] => Array
        (
            [user] => 3333
            [user_image] => ax46l7v7vugnesk10whk_339.jpg
            [image] => aaaaaaaa.jpg
            [date] => 2012-01-24 01:49:57
        )

    [5] => Array
        (
            [user] => 6666
            [user_image] => user_6_last_image
            [image] => the_last.jpg
            [date] => 2012-01-24 01:49:45
        )

)

I try to add url to [user_image] so :

Code:
function pictures_list($user_id) {
        $data['my'] = $this->ci->follow_model->user_picture_list($user_id);
        
        $newarray = array();
        
        foreach ($data['my'] as $key => $value) {
            $new_image = 'images/'.$value['user_image'];

            $newarray[$key]['user_image'] = $new_image;
        }
        
        $data['my'] = array_merge($data['my'], $newarray);
        
        return $data['my'];
    }

After the finished loop new array values will override the original values but it do not happen ? Where is my mistake?

result :
Code:
Array
(
    [0] => Array
        (
            [user] => 6666
            [user_image] => user_6_last_image
            [image] => user_6_last_image
            [date] => 2012-01-25 00:13:08
        )

    [1] => Array
        (
            [user] => 8888
            [user_image] => second.jpg
            [image] => second.jpg
            [date] => 2012-01-24 14:42:27
        )

    [2] => Array
        (
            [user] => 8888
            [user_image] => second.jpg
            [image] => first.jpg
            [date] => 2012-01-24 14:42:27
        )

    [3] => Array
        (
            [user] => 3333
            [user_image] => ax46l7v7vugnesk10whk_339.jpg
            [image] => ax46l7v7vugnesk10whk_339.jpg
            [date] => 2012-01-24 01:54:19
        )

    [4] => Array
        (
            [user] => 3333
            [user_image] => ax46l7v7vugnesk10whk_339.jpg
            [image] => aaaaaaaa.jpg
            [date] => 2012-01-24 01:49:57
        )

    [5] => Array
        (
            [user] => 6666
            [user_image] => user_6_last_image
            [image] => the_last.jpg
            [date] => 2012-01-24 01:49:45
        )

    [6] => Array
        (
            [user_image] => images/user_6_last_image
        )

    [7] => Array
        (
            [user_image] => images/second.jpg
        )

    [8] => Array
        (
            [user_image] => images/second.jpg
        )

    [9] => Array
        (
            [user_image] => images/ax46l7v7vugnesk10whk_339.jpg
        )

    [10] => Array
        (
            [user_image] => images/ax46l7v7vugnesk10whk_339.jpg
        )

    [11] => Array
        (
            [user_image] => images/user_6_last_image
        )

)

#2

[eluser]Aken[/eluser]
This is not a solution for array_merge. You can do it with much less code.
Code:
function pictures_list($user_id)
{
$picture_list = $this->ci->follow_model->user_picture_list($user_id);

foreach ($picture_list as &$picture)
{
  $picture['user_image'] = 'images/' . $picture['user_image'];
}

return $picture_list;
}

By the way, you could simplify things even more by added the "images/" string to your user_picture_list() model function's database query. If you did that, then you wouldn't even need to loop through your array like this. Post the contents of your follow_model's user_picture_list() function and I'll show you where.
#3

[eluser]CroNiX[/eluser]
Why create a new array and merge? Just add it to your existing array...
Code:
foreach ($data['my'] as $key => $value)
{
  $data['my'][$key]['user_image'] = 'images/'.$value['user_image'];
}
#4

[eluser]kolxoznik1[/eluser]
Aken, here is my model.Do not know is it good query but here it is :

Code:
function user_picture_list($user_id)
    {
        $sql = "SELECT u.username as user, i.image as user_image, p.image, p.date
                FROM users u              
                LEFT JOIN user_follow f ON u.id = f.follow_id
                LEFT JOIN images p ON p.user_id = u.id
                LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
                WHERE f.user_id = 3 OR p.user_id = 3      
                ORDER BY p.date DESC";
                
        $query = $this->db->query($sql);
        
        return $query->result_array();    
    }
#5

[eluser]Aken[/eluser]
Try this:
Code:
function user_picture_list($user_id)
{
    $sql = "SELECT u.username as user, CONCAT('images/', i.image) as user_image, p.image, p.date
            FROM users u              
            LEFT JOIN user_follow f ON u.id = f.follow_id
            LEFT JOIN images p ON p.user_id = u.id
            LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b WHERE p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
            WHERE f.user_id = $user_id OR p.user_id = $user_id
            ORDER BY p.date DESC";
            
    $query = $this->db->query($sql);
    
    return $query->result_array();    
}




Theme © iAndrew 2016 - Forum software by © MyBB