1

I know I can add elemnts to an array like this:

$arr = array("foo" => "bar", 12 => true);

Now, how can I do that in a foreach using dynamic values? I have this code:

foreach ($values as $value) {

    $imagePath = $value->getImagePath();
    $dependsOn = $value->getDependsOn();
    $dependsOn = explode(':', $dependsOn);
    $dependsOnOptionValueTitle = trim($dependsOn[1]);

    array_push($paths, $dependsOnOptionValueTitle => $imagePath); // not working
}

How can I add key/value pairs to my $paths array?

0

2 Answers 2

3

Instead of

array_push($paths, $dependsOnOptionValueTitle => $imagePath); // not working

you should be able to use

$paths[$dependsOnOptionValueTitle] = $imagePath;
Sign up to request clarification or add additional context in comments.

Comments

2

From what I can see, this is what you're trying to do:

$paths[$dependsOnOptionValueTitle] = $imagePath;

Comment if I'm wrong and I'll try to fix it.

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.