CBeTJlu4ok
CBeTJlu4ok

Reputation: 1112

Search for value in object returned from mySQL

This is an object returned from mySQL request:

array(6) {
  [0]=> object(stdClass)#48 (2) {
    ["meta_key"]=> string(16) "bp_activity_tagz"
    ["meta_value"]=> string(1) "1"
  }
  [1]=> object(stdClass)#30 (2) {
    ["meta_key"]=> string(14) "favorite_count"
    ["meta_value"]=> string(1) "3"
  }
  [2]=> object(stdClass)#30 (2) {
    ["meta_key"]=> string(14) "bp_activity_tagz"
    ["meta_value"]=> string(1) "4"
  }
  [3]=> object(stdClass)#30 (2) {
    ["meta_key"]=> string(14) "bp_activity_tagz"
    ["meta_value"]=> string(1) "2"
  }
  [4]=> object(stdClass)#30 (2) {
    ["meta_key"]=> string(14) "bp_activity_spam"
    ["meta_value"]=> string(1) "0"
  }
  [5]=> object(stdClass)#30 (2) {
    ["meta_key"]=> string(14) "bp_activity_universal"
    ["meta_value"]=> string(0) ""
  }
}

Depending on meta_key I need to place each value in its own place (one of those meta_key might not even exist)

Of course foreach can do the trick but I want to execute each function step by step, switching case is not a solution here.

I more searching for array-search like function but for object like this.

function would look like this (imaginary language) If my array is $array

in $array get meta_value for meta_key 'bp_activity_tags'
   if exist do this with meta_value
   else do this
in $array get meta_value for (every) meta_key 'bp_activity_tagz'
   if exist foreach meta_value
   else do this
in $array get meta_value for meta_key 'bp_activity_spam'
   if exist do this
   else do this

This is an function for requesting this array:

    $metas = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );

    if ( !empty( $metas ) ) {
        $metas = array_map( 'maybe_unserialize', (array) $metas );

        foreach( $metas as $mkey => $mvalue ) {
            wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $mkey, $mvalue, 'bp' );
        }
    }
// No result so return false
if ( empty( $metas ) )
    return false;

// Maybe, just maybe... unserialize
$metas = array_map( 'maybe_unserialize', (array) $metas );

// Return first item in array if only 1, else return all metas found
$retval = ( 1 == count( $metas ) ? $metas[0] : $metas );

Upvotes: 0

Views: 88

Answers (2)

Matthias S
Matthias S

Reputation: 3563

If I understand you correctly you want to have a function that allows you to search within an object's variable and if there is a match, you need that object (or its 'value').

Maybe something like this works for you?

public function findInObjects($array, $objVariable, $needle) {
  foreach($array as $object) {
    if(strpos($object->$objVariable, $needle) !== false) {
        return $object;
    }
  }
  return null;
}

$result = findInObjects($array, 'meta_key', 'favorite_count');
echo $result->meta_value; //should result in "3"

Upvotes: 1

deceze
deceze

Reputation: 522372

I would simply first reformat the array:

$data = array();
foreach ($array as $values) {
    $data[$values->meta_key][] = $values->meta_value;
}

Then simply write that code:

if (isset($data['bp_activity_tags'])) {
    foo($data['bp_activity_tags']);
}
...

Upvotes: 1

Related Questions