0

My problem is so twisted that I don't even know how to start to explain it.

Lets say that I have several assosiative arrays (not always the same arrays: sometimes I have the products array, sometimes I have the markets array, sometimes I have the segment array, etc...). $values is the only array I always get!

$values = array ("0" => "1", "4" => "2", "5" => "3");
$products = array ("0" => "1", "1" => "1", "2" => "2", "3" => "1", "4" => "2", "5" => "3");
$markets =  array ("0" => "1",  "3" => "1", "4" => "2", "5" => "3");
...

I want to build an array with the values of each of the arrays I get, with the values matching the keys. Something like

$myArray = array ("0" => array ( "values" => "1", "products" => "1", "markets" => "1"),
                  "1" => array ( "products" => "1"),
                  "2" => array ( "products" => "2"),
                  "3" => array ( "products" => "1", "markets" => "1"),
                  "4" => array ( "values" => "2", "products" => "2", "markets" => 2),
                ...);

I've tried something like this:

        switch ($_POST["cpv_type"]) {
            case "pClass":
                $keyValue = $_POST["cpv_type"];
                $objKey   = "this->productClasses";
                break;

            case "pMarket":
                $keyValue = $_POST["cpv_type"];
                $objKey   = "this->markets";
                break;

            case "pSegment":
                $keyValue = $_POST["cpv_type"];
                $objKey   = "this->productSegments";
                break;

            case "pType":
                $keyValue = $_POST["cpv_type"];
                $objKey   = "this->productTypes";
                break;

            default:
                $keyValue = "products";
                $objKey = "this->products";
                break;
        }

And then I do a foreach cicle:

    // all values must be floats
    if(!empty($this->value)){
        foreach ($this->value as $key => &$curVal){
            // if no value has been entered, exclude it and also associated product from validation
            if (strlen(trim($curVal)) == 0) {
                unset($this->value[$key]);
                unset($this->products[$key]);
            } else {
                                        // This validates my variable 
                $curVal =   TMS::checkVar($curVal, "dec", $_SESSION["dico"]->_VALUE_, 100, false);


                                        // Store the value on existing array, associating "hoppValue" to the right key entry!
                $logDetail[$keyValue][${$objKey}[$key]]["hoppValue"] = $curVal;
            }
        }
    }

My problem is in the variable variable: How do I access, for example $this->productTypes[5] using variable variable syntax? I get "null" for all var_dumps of $$objKey, ${$objKey}, ${$objKey}[$key], ${$objKey[$key]}, $$objKey[$key]

Thank you for your help!

2
  • 1
    you can simple get array in case ` $objKey = $this->productClasses` and used as $objKey[$key] Commented Jun 8, 2016 at 16:52
  • You are totally right! The answer was so simple and right in front of my eyes. Workdays shouldn't be so long! (Please put your comment into answer so I mark it right!) Commented Jun 8, 2016 at 16:57

1 Answer 1

1

you can simple get array in case $objKey = $this->productClasses and used as $objKey[$key]. And you can replace $objKey to $arrayClasses or similary for good understending code.

p.s. sorry for my English.

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.