从一个字符串合并数组?

时间:2015-03-25 05:06:36

标签: php arrays

我在尝试将多个数组合并到一个数组时遇到了麻烦我似乎无法找到解决此问题的合适解决方案,因为使用merge_array不起作用。我在foreach循环中创建了一个数组,但它仍然以一个字符串的多个数组形式出现。我该如何转换呢。

    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
)

对此?

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
    )
)

这是我的代码

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);

    }
}

1 个答案:

答案 0 :(得分:2)

似乎:

$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;

    }
}

不是吗?或者我误解了你的目的。