Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So if having a multidimensional array like:

Got it from here (as a demo): PHP. Loop through an array and get items with attributes in common

$data

Array
(
    [0] => stdClass Object
        (
            [term_id] => 3
            [name] => Comercial
        )

    [1] => stdClass Object
        (
            [term_id] => 4
            [name] => Escolar
        )

    [2] => stdClass Object
        (
            [term_id] => 5
            [name] => Kinder
        )

    [3] => stdClass Object
        (
            [term_id] => 6
            [name] => Primaria
        )

    [4] => stdClass Object
        (
            [term_id] => 7
            [name] => Secundaria
        )

    [5] => stdClass Object
        (
            [term_id] => 1
            [name] => Uncategorized
        )

)

Having 0,1,2,3,4,5 stdClass Objects, how can I create individual arrays for each std Object dynamically.

By that I mean that the function should be able to create $varX array, where X is the array number of the stdObject, automatically...

$var0 = $data[0]; $var1 = $data[1]; and so on, determined by $data first level count of arrays.


Edit: I got carried away and forgot to mention the most important question:

Having $var0, $var1... is very important because for a later use of all or each one individually.

So

  • Needs to create X variables according to the count of first level of the multidimensional array
  • each $varX needs to be accessible in common with the rest of $varX or individually.

    $count = count($data); //6

    foreach ($data as $key => $value) { $var.$key = $value; }

Ok, that function works partially because from there I don't know how to make it automatically add $val1,$val2... to (ex:) array_intersect($val1,$val2,$val3...

share|improve this question
    
Curious as to why you would want to do this? i.e. What do you plan to accomplish with $var0 that you can't with $data[0] ? –  Jordan Arseno Feb 10 '12 at 0:37
    
check the edit to see why, I had forgotten to ask it the first time –  w0rldart Feb 10 '12 at 1:04
    
I think you are confused. I don't think embedding the array key into the lvalue is ever the right approach. –  Jordan Arseno Feb 10 '12 at 1:06
add comment

3 Answers

up vote 2 down vote accepted

The easiest way would be to use extract.

extract($data, EXTR_PREFIX_ALL, 'var');

With that said, it will add an underscore (_) after the prefix (e.g. var_0).

Update:

Regarding your edit, you could simply call array_intersect using call_user_func_array. There's no need for variables.

call_user_func_array('array_intersect', $data);
share|improve this answer
    
check the edit, I added what I forgot to ask –  w0rldart Feb 10 '12 at 1:02
    
+1, wow I really like how the solution you offer looks. I will test it tomorrow, now is time to nap :D. Thank you –  w0rldart Feb 10 '12 at 1:10
add comment
foreach ($data as $key => $obj)
{
  $key = 'var'.$key;
  $$key = $obj;
}
share|improve this answer
    
check the edit... –  w0rldart Feb 10 '12 at 1:03
add comment

You can just cast each object to an array.

foreach ($data AS $key => $datum)
{
    $data[$key] = (array) $datum;
}

For your update:

foreach ($data AS $key => $datum)
{
    $newkey = 'var' . $key; // we want the variable to be called "var" plus the number
    $$newkey = (array) $datum; // Make it an array
    $data[$key] = $$newkey; // And update the $data array to contain the array
}

You now have $var0, $var1, etc and can also access them as a collection in $data, and they're in both as an array.

share|improve this answer
    
check the edit... I added another question –  w0rldart Feb 10 '12 at 1:03
    
I already answered it, I think :) –  Joe Feb 10 '12 at 1:05
    
hmmm, I don't see how (me) I could run the array_intersect with all the values $key have. I didn't try, but I don't think running array_intersect($key) after the foreach will work, neither inside it. –  w0rldart Feb 10 '12 at 1:08
    
Rather than saying "I want to extract these vars as an array", "I want to intersect them", why not just say "I have this data [paste], I need to get it to look like this [paste], how do I do it?" :) Much easier to answer. –  Joe Feb 10 '12 at 1:11
    
Sorry @Joe if my question wasn't clear enough, sometimes its really hard for me to express my questions by writing them, and I'm still improving my Technical English skill to make better questions and answers. But thank you for your help, I appreciate it! –  w0rldart Feb 10 '12 at 1:15
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.