I'm working with a two-dimensional array, and as I'm iterating through each index of this array, I'm trying to check if the current key is equal to a string.
Here's how I'm trying to do it:
foreach($_POST['items'] as $index => $item){
$key = key($item);
if($key == 'image'){
echo 'hello';
}
}
This throws the error Invalid argument supplied for foreach()
How do you check if the current key is equal to a particular string?
var_dump
array(2) {
[0]=> array(1) { ["paragraph"]=> string(4) "paragraph 1" }
[1]=> array(1) { ["paragraph"]=> string(4) "paragraph 2" }
}
array(2) {
[0]=> array(1) { ["paragraph"]=> string(4) "paragraph 3" }
[1]=> array(1) { ["paragraph"]=> string(4) "paragraph 4" }
}
Here is the element within the form with name image:
<div><input type="file" name="items[][image]" id="uploadImage" multiple></div>
var_dump of dynamic form with 1 image input followed by 1 paragraph input
array(1) {
[0]=> array(1) { ["paragraph"]=> string(11) "paragraph 1" }
}
var_dump is not seeing the input with name image???
Here is the form Its contents are dynamically added from <script>
<form method="post" action="insert.php" enctype="multipart/form-data">
<textarea name="title"></textarea>
<input type="submit" name="upload" value="Upload" id="upload">
</form>
One of the dynamic adding functions (this one is for image inputs)
function addImage() {
$("form").append('<div><input type="file" name="items[][image]" id="uploadImage" multiple></div>');
}
$_POST['items']is not something you can loop through withforeach. Your first step should be to make sure it's actually an array.$_POST['items']variable? -- If you have not performed any processing on it then it is likely a String, and not an array.ifwould have any bearing on the "Invalid argument supplied for foreach".foreachloop at all. Are you sure the error is coming from this code? The error message should have a line number on it.