-1

I have a problem to get values from single square brackets array. Getting this

//$_POST["values"] == ["val1","val2","val3"]
$val = $_POST["values"]; 

I want something like:

foreach( $val as $value ) {
   $valget = $value;
   //need to get all values like $valget = "val1";
}

Thank you very much!

UPDATE:

What Im trying to get? Values passed as $_POST from filemanager

How I want to proceed? I wnat for each every result of post save into database

And still saving all as array into database to single row, I need for each record new row.

Database

Coxii
  • 132
  • 2
  • 11
  • isn't that what you get ? it's working fine as i tested it .. in each iteration i get val1 val2 and val3 – Demonyowh Jul 31 '18 at 06:46
  • What's the use of that? You're overwriting `$valget` with every iteration of your loop. Can you be a bit more specific of what you want? – brombeer Jul 31 '18 at 06:53
  • 3
    you overwrite every iteration the $valget variable, so at the end of the loop you variable contains the last value of $_POST['values'] array. p.s don't acces directly to $_POST array – Sfili_81 Jul 31 '18 at 06:53
  • I'm trying to get data from multiselect from responsive filemanager. Then each of the value is image, so I need to save each of that image into database as single record, only problem is that I'm still getting whole array saved in db. As ["videoeditor/public/userfiles/output/1/img/frame_1532197324_5b5379cca825a_1-00.jpg","videoeditor/public/userfiles/output/1/img/frame_1532088244_5b51cfb4d99cb_1-00.jpg","videoeditor/public/userfiles/output/1/img/frame_1531930204_5b4f665c1283e_1-00.jpg"] – Coxii Jul 31 '18 at 07:04
  • so update your question. If i understand you must save save your value into db inside your loop. – Sfili_81 Jul 31 '18 at 07:06
  • you need to `explode($string)` with images before but this is another question – Oleksandr Pobuta Jul 31 '18 at 07:06
  • @Coxii why you don't ask the real question in first place? The problem is not about getting values in array but how to save each data from array in database no? – Mickaël Leger Jul 31 '18 at 07:14
  • Possible duplicate of [How does PHP 'foreach' actually work?](https://stackoverflow.com/questions/10057671/how-does-php-foreach-actually-work) – kallosz Jul 31 '18 at 07:19

2 Answers2

2

Ok so this is your var with all $_POST values :

$val = $_POST["values"]; 

This is what you do :

foreach($val as $value) {
    $valget = $value;
}

So each time $valget will be erase by the next $value

// First loop : $valget == "val1"
// Second loop : $valget == "val2"
// Third loop : $valget == "val3"

So at the end if you do echo $valget; you will have the last result : $valget == "val3"

If you want to get each, here is some solution :

1/ Echo each value :

foreach($val as $value) {
    echo $value . "<br>";
}

This way you will output

val1
val2
val3

2/ Doing nothing since $val is already an array with all the value :

$val = $_POST["values"];

$val = array(
    0 => "val1",
    1 => "val2",
    2 => "val3"
);

So you can access each value with :

$val[0] == "val1";
$val[1] == "val2";
$val[2] == "val3";

3/ Change the key if you want to to find them an other way :

// New array
$valget = array();

// Create a new index
$index = 1;

foreach($val as $value) {
    $valget[$index] = $value;
    $index++;
}

This way you will have :

$valget[1] == "val1";
$valget[2] == "val2";
$valget[3] == "val3";
Mickaël Leger
  • 3,345
  • 1
  • 13
  • 32
0

OK got it! Now I want to share answer with world, because there may be someone else who will face same problem as I had.

After everything I tried only working was:

    $numbers = json_decode($_POST['file_img']); //post images from <form> and decode array

    foreach($numbers as $index => &$number){  
        ++$number; # we are incrementing the original value
        echo 'Inside of the array = ', $index, ': ', $number, '<br />'; # this is showing the original value

so now $number = "every single image";
Now save to db or whatever..
}

This answer was found here: https://stackoverflow.com/a/47266239/4928816

So If anyone will need to pass variables from standalone responsivefilemanager to PHP as I needed (http://www.responsivefilemanager.com/demo.php), you may use this code to do so.

Thanks everyone who tried to help me!

Coxii
  • 132
  • 2
  • 11