1

I get this data array from form and I need to save this array data to five different variables, so how I do,
this is filled with images data

 +files: Symfony\Component\HttpFoundation\FileBag {#61 ▼
    #parameters: array:1 [▼
      "img" => array:5 [▼
        0 => Symfony\.... {#42 ▶}
        1 => Symfony\....{#43 ▶}
        2 => Symfony\....{#44 ▶}
        3 => Symfony\.... {#45 ▶}
        4 => Symfony\....{#46 ▶}
      ]
    ]

I need to add data to these variables

$img1 = 0
$img2 = 1
$img3 = 2
$img4 = 3
$img5 = 4
2
  • 3
    I think you need to learn how to use arrays or the corresponding class methods rather than extracting to variables. Variables called img1, 2... is usually indication that arrays should be used. Commented May 10, 2020 at 15:12
  • im new to coding Commented May 14, 2020 at 15:51

2 Answers 2

1

You can use ${$variable_name} syntactic sugar from php to assign or get variable from using string as variable name

$img0 = null; $img1 = null; $img2 = null; $img3 = null; $img4 = null; $img5 = null; 

foreach($files['img'] as $key => $file){
    $varname = 'img' . $key; //img1 , img2 , img3 ...

    ${$varname} = $key; // $img1 = 0, $img1 = 1, $img3 = 1;
}

I think this code will be solution for you

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

Comments

0

Maybe using 'list ()' you can achieve what you want.

PHP: Returning values #Example2

list($img1, $img2, $img3, $img4, $img5) = array_values($files['img']);

// $img1 the first image
// $img2 the second image
// etc

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.