0

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.

3
  • 1
    What variable do you actually want to print that? $unique_category[0]."_category" is no variable, it's a concatenation of the value of the first element of that array and the string "_category". Did you mean ${$unique_category[0].'_category'}? Commented Nov 4, 2013 at 2:44
  • In general, dynamically naming variables (as you are doing with ${"$value_category"}, and can be more succinctly done with $$value_category) is a bad idea, and a sign of poorly structured code. Generally, what you should actually be using is an associative array, e.g. $expanded_categories[$key] = $value->multi_dim_array() Commented Nov 4, 2013 at 2:50
  • I have edited the question to show what I want to be printed. Since value is in the array, I want to be able to print out the multidimesional array with either $person_category (person being a value in the array), or $unique_category[0]."_category", being the same thing as $person_category. Commented Nov 4, 2013 at 12:03

1 Answer 1

0

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";

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.