Runner
Runner

Reputation: 115

Looping through PHP array in foreach and give each $value a new variable name

I want to take an array, loop it with a foreach loop, and have each array value be sent through a class to get data from a database. This is the code I am currently using:

foreach ($unique_category as $key => $value) 
{
    $category = $value;
    $value = new database;
    $value->SetMysqli($mysqli);
    $value->SetCategory($category);
    $value->query_category();
    ${"$value_category"} = $value->multi_dim_array();
    print_r(${"$value_category"});
    echo "<br /><br />";            
}
print_r($unique_category[0]."_category");

I want the variable $unique_category[0]."_category" to be ${"$value_category"}. Currently, the ${"$value_category"} in the foreach loop prints out the correct value/array, while $unique_category[0]."_category" just prints person_category (person being the first value in that array).

How would I go about making $unique_category[0]."_category" print the same thing as ${"$value_category"}?

Thank you

EDIT:

The foreach loop is making a multidimensional array that looks something like this Array ( [0] => Array ( [0] => Home [1] => 9.8 ) [1] => Array ( [0] => Penny [1] => 8.2 )) I want to be able to print out this array outside the foreach loop, with each md array having its own variable name so I can print them out wherever and whenever I want.

Upvotes: 0

Views: 3201

Answers (1)

aretecode
aretecode

Reputation: 334

Testing without the object

<?php

    $unique_category_list = array('foo', 'bar', 'baz');
    foreach ($unique_category_list as $key => $value) 
    {
        $category = $value;
        $value_category = $value."_".$category; 
        $unique_category = $unique_category_list[$key]."_category";
        $unique_category = ${"$value_category"} = $key; 

        print_r($unique_category_list[$key]."_category");
        echo "\n\n";
    }

?>

Outputs:

foo_category

bar_category

baz_category

With the object

<?php 

    // note that $unique_category is now $unique_category_list && $value is now $category
    foreach ($unique_category_list as $key => $category) 
    {
        $database = new Database();
        $database->setMysqli($mysqli);
        $database->setCategory($category);
        $database->query_category();

        // http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
        // this will invoke the `__toString()` of your $database object... 
        // ... unless you meant like this
        // $value_category = $category."_".$category;
        $value_category = $database."_".$category;
        $unique_category = $unique_category_list[$key]."_category";

        // http://stackoverflow.com/questions/2201335/dynamically-create-php-object-based-on-string
        // http://stackoverflow.com/questions/11422661/php-parser-braces-around-variables
        // http://php.net/manual/en/language.expressions.php
        // // http://php.net/manual/en/language.variables.variable.php
        // 'I want the variable $unique_category[0]."_category" to be ${"$value_category"}.'
        $unique_category = ${"$value_category"} = $database->multi_dim_array();          
    }

    print_r($unique_category_list[0]."_category");
    echo "<br><br>\n\n";

?>

Upvotes: 0

Related Questions