Jeremy Love
Jeremy Love

Reputation: 179

Merge arrays from one string?

I am having trouble with trying to merge mulitple arrays into one array I also cant seem to find a decent solution to this problem since using merge_array doesnt work. I made an array in my foreach loop but it keeps coming out as multiple arrays for one string. How can I convert this.

    Array
(
    [images] => /wp-content/uploads/2015/03/223-HfpWJ4h.jpg
    [userid] => 15
)
Array
(
    [images] => /wp-content/uploads/2015/03/074-XlqzqVn.jpg
    [userid] => 1
)
Array
(
    [images] => /wp-content/uploads/2014/09/cj-spiller.jpg
    [userid] => 1
)
Array
(
    [images] => /wp-content/uploads/2015/02/183-cZRn9rz.jpg
    [userid] => 1
)
Array
(
    [images] => /wp-content/uploads/2015/02/001-gSxGEWD5.jpg
    [userid] => 1
)

To this?

Array
(
    [0] => array(
        [images] => /wp-content/uploads/2015/03/223-HfpWJ4h.jpg
        [userid] => 15
    )
    [1] => array(
        [images] => /wp-content/uploads/2015/03/074-XlqzqVn.jpg
        [userid] => 1
    )
    [2]=> array(
        [images] => /wp-content/uploads/2014/09/cj-spiller.jpg
        [userid] => 1
    )
)

This is my code

global $wpdb, $bp;

$activity_table = bp_core_get_table_prefix() . 'bp_activity';
$activity_meta_table = bp_core_get_table_prefix() . 'bp_activity_meta';

// that have pictures associated with them
$sql = "SELECT a.*, am.meta_value, am2.meta_value as privacy FROM $activity_table a
                    INNER JOIN $activity_meta_table am ON a.id = am.activity_id
                    LEFT JOIN (SELECT activity_id, meta_key, meta_value FROM $activity_meta_table
                    WHERE meta_key = 'activityprivacy') am2 ON a.id = am2.activity_id
                    AND (am.meta_key = 'buddyboss_pics_aid' OR am.meta_key = 'bboss_pics_aid')
                    ORDER BY a.date_recorded DESC";

$pics = $wpdb->get_results($sql,ARRAY_A);

$i = 0;

foreach($pics as $pic){

    $attachment_id = isset($pic['meta_value']) ? (int)$pic['meta_value'] : 0;
    $tn = wp_get_attachment_image_src( $attachment_id, 'buddyboss_pic_tn' );

    $userid = $pic['user_id'];

    if($tn[0] != ''){

        $profile_image  = array('images' => $tn[0], 'userid' => $pic['user_id']);

        print_r($profile_image);

    }
}

Upvotes: 1

Views: 46

Answers (1)

Clarence
Clarence

Reputation: 721

It seems like:

$result = array();

//...
foreach($pics as $pic){

    //...

    $userid = $pic['user_id'];

    if($tn[0] != ''){

        $profile_image  = array('images' => $tn[0], 'userid' => $pic['user_id']);

        $result[] = $profile_image;

    }
}

Isn't it? Or I've mistaken your purpose.

Upvotes: 2

Related Questions