Asked  7 Months ago    Answers:  5   Viewed   32 times

Right now, I have this code where $obj_arr maybe contain array and an object.

$obj_temp = array ($obj_identity, $arr_user, $obj_locale, $arr_query); 

foreach ($obj_temp as $maybe_arr) {
        if (is_array($maybe_arr)) :
            $name = (string) key($maybe_arr);
        if (is_object($maybe_arr)) :    
            ???? // how to retrieve a class name ?
        endif;  

        $obj_arr[$name] = $maybe_arr;

}



obj_will_be_extract($obj_arr);

function obj_will_be_extract($obj_arr) {
    extract($obj_arr);

    //Do the rest

}

I need to create an array consist of combination of objects and arrays. Cause I need to extract it, then how to get an object name?

 Answers

44

Use get_class to get the class name of an object.

Wednesday, March 31, 2021
 
PeterTheLobster
answered 7 Months ago
92

If you could index the array by gear or some unique value, it would be a lot easier.

$indexed = array();

// create an array using 'gear' as the index
foreach($arrayValue as $value) {
    $indexed[$value['gear']] = $value;
}

// loop over each object
foreach($objectArray as $obj) {
    $value = $indexed[$obj->gear]; // find the corresponding array
    foreach($value as $name => $val) {
        $obj->$name = $val; // assign each array index/value pair to the object
    }
}

If possible to get your code to return the array with the index by default, you can remove the first foreach loop.

Hope that helps.

Wednesday, March 31, 2021
 
wavyGravy
answered 7 Months ago
10

You either iterate the array, searching for the particular record (ok in a one time only search) or build a hashmap using another associative array.

For the former, something like this

$item = null;
foreach($array as $struct) {
    if ($v == $struct->ID) {
        $item = $struct;
        break;
    }
}

See this question and subsequent answers for more information on the latter - Reference PHP array by multiple indexes

Saturday, June 5, 2021
 
TMichel
answered 5 Months ago
83

I got it! Actually , I could do it by this code:

Instead of putting just :

user = db.users.findOne({userName:"And"})

I did just :

  var user = db.users.findOne({userName:"And"})

and

  user._id 

returns the ObjectId("someId") , if I want to keep it in some variable I do:

var Id = user._id. 

About the second question, I dont know.

Thursday, August 12, 2021
 
John Oleynik
answered 3 Months ago
27
extern alias alias1;
using alias1::Namespace;
Sunday, August 15, 2021
 
viky
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :