Search code examples
phparrayswordpressbuddypress

how to use foreach with array object


global $wpdb, $bp;
$result = $wpdb->get_results("SELECT user_id FROM wp_bp_xprofile_data WHERE value = '$_POST[language]' OR value = '$_POST[budget]' OR value = '$_POST[style]'LIMIT 0 , 30");
print_r($result);

The output is:

Array ( [0] => stdClass Object ( [user_id] => 2 )
        [1] => stdClass Object ( [user_id] => 2 ) 
        [2] => stdClass Object ( [user_id] => 2 ) 
        [3] => stdClass Object ( [user_id] => 4 ) )

I want the value as comma separated string.


Solution

  • hier is a possible solution;

    <?php 
        global $wpdb, $bp;
        $result = $wpdb->get_results("SELECT user_id FROM wp_bp_xprofile_data WHERE 
                    value = '$_POST[language]' OR value = '$_POST[budget]' 
                    OR value = '$_POST[style]'LIMIT 0 , 30");
        $str_ids = '';
        foreach($result as $r) {
             $str_ids .= ('' == $str_ids) ? '' : ', ';
             $str_ids .= $r->user_id;
        }
        print_r($str_ids);