I am using this PHP Code:
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!empty($_FILES['ticket_files']))
{
}
}
But if the file input is blank, it still thinks that there is a file there and runs the code.
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if (count($_FILES['ticket_files']) > 0)
{
}
}
Try this instead of what you have now.
try this
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!$_FILES['ticket_files']['error'][$key])
{
//Do Stuff
}
}
You can use this:
if (file_get_contents( $file_path ) == '')
{
// file is empty
}
You need check the contents of the file to see if it's empty.
foreach ($_FILES['ticket_files']['name'] as $key => $value)
{
if(!empty(file_get_contents($path . $_FILES['ticket_files']['name']))
{
//Do Stuff
}
}
If nothing is uploaded $_FILES array looks like:
Array ( [image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
The error code 4 [error] => 4 indicates no file was uploaded and error code 0 indicates no error and file was uploaded. You can add below check instead of empty check.
if($_FILES['ticket_files']['error']==0) {
// file uploaded, process code here
}
Related
Thanks in advance for your help!
I need to change the values from the variable $v_file recursively. For that I created a recursive function that loops through my array $arr and substitutes the placeholders with the values from my array.
function replace_file($arr, $v_file){
foreach ($arr as $key => $value) {
if (is_array($value)) {
replace_file($value, $v_file);
}else{
if($value == "check"){echo("$key ||| $value <br/>");
$v_file = str_replace('{{'.$key.'}}', 'check-square.svg', $v_file);
}elseif ($value == "uncheck") {
$v_file = str_replace('{{'.$key.'}}', 'square.svg', $v_file);
}else{
$v_file = str_replace('{{'.$key.'}}', $value, $v_file);
}
}
}
return $v_file;
}
Here is the array I'm trying to loop through
Array (
[nomec] => john doe
[pessoac] => 1234
[datanascc] => 01/01/0001
[cpfcnpjc] => 999.999.999-99
[rgc] => 99.999.999-9
[proposta] => Array (
[inclusao] => check
[exclusao] => uncheck
[alteracaodados] => uncheck
[segviacartao] => uncheck
)
[beneficiario] => Array (
[titular] => check
[dependente] => uncheck
[agregado] => check
)
)
And this is the info I'm trying to substitute with the values from the array:
$file = "{{nomec}},
{{pessoac}},
{{datanascc}},
{{cpfcnpjc}},
{{rgc}},
{{inclusao}},
{{exclusao}},
{{alteracaodados}},
{{segviacartao}},
{{titular}},
{{dependente}},
{{agregado}}";
When i call the function with replace_file($obj, $file); and print the result, i get:
john doe, 1234, 01/01/0001, 999.999.999-99, 99.999.999-9, {{inclusao}}, {{exclusao}}, {{alteracaodados}}, {{segviacartao}}, {{titular}}, {{dependente}}, {{agregado}}
Can someone help me with this function?
Thanks!
I'm trying to solve an issue where I have an multilevel array that needs to be filtered with user controlled filters.
Example of the array
[1] => Array
(
[objectID] => 5038
[Data] => Array
(
[originalId] => 6
)
[titles] => InfoType Object
(
[_] => string
[language] => eng
)
)
The filters would be then language and objectID, for example.
Anything that doesn't meet the criteria will have to be excluded. Sounds perfectly find if that would be a SQL query, but it's not. The API returns a string that cannot be controlled and it's in a form of an array. Have to work with what you have.
The idea came up to write a function that would prepare an if-statement. Problem is that you can't do just that
foreach ($cache as $listing) {
foreach ($filters as $filter_param => $filter_value) {
if ($query) $output[] = $listing;
}
}
In this case the $query would be equal to something like this:
$listing["titles"]["language"] =="eng" && $listing["objectID"] =="5038"
I'm pretty sure there's an easier way that wouldn't actually be bad. Really stuck with this one.
function isGood($what, $filters) {
foreach ($filters as $key => $value) {
if (is_array($value)) {
if (isGood($what[$key], $value) === false) {
return false;
}
} else {
if ($what[$key] != $value) {
return false;
}
}
}
return true;
}
foreach($cache as $listing) {
if (isGood($listing, $filters)) {
$output[] = $listing;
}
}
I have a multidimensional array below. And I am trying to get certain part of the array based on a value that is passed into the function. But for some reason it returns false even though the path matches, it only return something if /test is used but if I type /hello the if fails and it returns false.
here is the array:
Array
(
[0] => Array
(
[name] => test_route
[path] => /test
[controller] => TestController
[action] => indexAction
)
[1] => Array
(
[name] => hello_route
[path] => /hello
[controller] => HelloController
[action] => helloAction
)
)
and here is the method:
public function getRoute($path = "", $name = "")
{
foreach($this->routes as $key => $val)
{
if($val['path'] === $path || $val['name'] === $name)
{
return $this->routes[$key];
}
else
{
return false;
}
}
}
Just modifying on the code you provided , may be you should try something like this :
public function getRoute($path = "", $name = "")
{
foreach($this->routes as $key => $val)
{
if($val['path'] === $path || $val['name'] === $name)
{
return $this->routes[$key];
}
}
return false;
}
Your method exists after examining the first element. Remove the else block and put the return false outside the loop.
foreach($this->routes as $key => $val)
{
if($val['path'] === $path || $val['name'] === $name)
{
return $this->routes[$key];
}
}
return false;
I'm not sure why /test even works. You're dealing with a multidimensional array. foreach does not do deep searches. You're going to have to modify your code to this:
public function getRoute($path = "", $name = "")
{
foreach($this->routes as $route) {
foreach($route as $key => $val)
{
if($val['path'] === $path || $val['name'] === $name)
{
return $route[$key];
}
}
}
return false;
}
How can i add element to my array under a specific key?
This is my array output before i use foreach. As you can see, the error field is empty. I want to fill it out.
Array (
[0] => Array (
[transactionid] => 2223
[created] => 26-02-13 14:07:00
[cardid] => 10102609
[pricebefordiscount] => 68900
[error] =>
)
This is my foreach. As you can see i already tried to make this work by implementing $arrayname['index'] = $value;. But this does not work, nothing comes out when i spit out in a print_r. Why is this happening?
foreach ($samlet as $key)
{
if ($key['pricebefordiscount'] > '200000')
{
$samlet['error'] = "O/2000";
}
if ($key['cardid'] === '88888888')
{
$samlet['error'] = "Testscan";
}
}
This is the desired output:
Array (
[0] => Array (
[transactionid] => 2223
[created] => 26-02-13 14:07:00
[cardid] => 10102609
[pricebefordiscount] => 68900
[error] => "Testscan"
)
Change your foreach, so you have the indexes used in the "main" $samlet array:
foreach($samlet as $key => $array)
{
if ($array['cardid'] === '88888888')
{
$samlet[$key]['error'] = '0/2000';
}
}
And so on...
Try this :
foreach ($samlet as &$key){
if ($key['pricebefordiscount'] > '200000'){
$key['error'] = "O/2000";
}
if ($key['cardid'] === '88888888'){
$key['error'] = "Testscan";
}
}
According to PHP manual:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
So your code should looke like this:
<?php
foreach ($samlet as &$key)
{
if ($key['pricebefordiscount'] > '200000')
{
$key['error'] = "O/2000";
}
if ($key['cardid'] === '88888888')
{
$key['error'] = "Testscan";
}
}
TRY THIS
foreach ($samlet as $key=>$value)
{
if ($value['pricebefordiscount'] > '200000')
{
$samlet[$key]['error'] = "O/2000";
}
if ($value['cardid'] === '88888888')
{
$samlet[$key]['error'] = "Testscan";
}
}
This question already has answers here:
How do you remove an array element in a foreach loop?
(6 answers)
Closed 6 years ago.
I have a foreach loop set up to go through my array, check for a certain link, and if it finds it removes that link from the array.
My code:
foreach($images as $image)
{
if($image == 'http://i27.tinypic.com/29yk345.gif' ||
$image == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[$image]);
}
}
But it doesn't remove the array entires. It's probably something to do with $images[$image], as that's not the key of the array entry, only the content? Is there a way to do this without incorporating a counter?
Thanks.
EDIT: Thanks guys, but now I have another problem where the array entries don't actually get deleted.
My new code:
foreach($images[1] as $key => $image)
{
if($image == 'http://i27.tinypic.com/29yk345.gif')
$image == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[$key]);
}
}
$images is actuallty a two-dimensional array now hence why I need $images[1]. I have checked and it successfully goes around the array elements, and some elements do actually have some of those URLs in that I wish to delete, but they're not getting deleted. This is my $images array:
Array
(
[0] => Array
(
[0] => useless
[1] => useless
[2] => useless
[3] => useless
[4] => useless
)
[1] => Array
(
[0] => http://i27.tinypic.com/29yk345.gif
[1] => http://img3.abload.de/img/10nx2340fhco.gif
[2] => http://img3.abload.de/img/10nx2340fhco.gif
[3] => http://i42.tinypic.com/9pp2456x.gif
)
)
Thanks!
foreach($images as $key => $image)
{
if(in_array($image, array(
'http://i27.tinypic.com/29ykt1f.gif',
'http://img3.abload.de/img/10nxjl0fhco.gif',
'http://i42.tinypic.com/9pp2tx.gif',
))
{
unset($images[$key]);
}
}
Try that:
foreach ($images[1] as $key => &$image) {
if (yourConditionGoesHere) {
unset($images[1][$key])
}
}
unset($image); // detach reference after loop
Normally, foreach operates on a copy of your array so any changes you make, are made to that copy and don't affect the actual array.
So you need to unset the values via $images[$key];
The reference on &$image prevents the loop from creating a copy of the array which would waste memory.
To answer the initial question (after your edit), you need to unset($images[1][$key]);
Now some more infos how PHP works:
You can safely unset elements of the array in foreach loop, and it doesn't matter if you have & or not for the array item. See this code:
$a=[1,2,3,4,5];
foreach($a as $key=>$val)
{
if ($key==3) unset($a[$key]);
}
print_r($a);
This prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
[4] => 5
)
So as you can see, if you unset correct thing within the foreach loop, everything works fine.
You can use the index of the array element to remove it from the array, the next time you use the $list variable, you will see that the array is changed.
Try something like this
foreach($list as $itemIndex => &$item) {
if($item['status'] === false) {
unset($list[$itemIndex]);
}
}
$image is in your case the value of the item and not the key. Use the following syntax to get the key too:
foreach ($images as $key => $value) {
/* … */
}
Now you can delete the item with unset($images[$key]).
One solution would be to use the key of your items to remove them -- you can both the keys and the values, when looping using foreach.
For instance :
$arr = array(
'a' => 123,
'b' => 456,
'c' => 789,
);
foreach ($arr as $key => $item) {
if ($item == 456) {
unset($arr[$key]);
}
}
var_dump($arr);
Will give you this array, in the end :
array
'a' => int 123
'c' => int 789
Which means that, in your case, something like this should do the trick :
foreach($images as $key => $image)
{
if($image == 'http://i27.tinypic.com/29yk345.gif' ||
$image == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[$key]);
}
}
foreach($images as $key=>$image)
{
if($image == 'http://i27.tinypic.com/29ykt1f.gif' ||
$image == 'http://img3.abload.de/img/10nxjl0fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2tx.gif')
{ unset($images[$key]); }
}
!!foreach($images as $key=>$image
cause $image is the value, so $images[$image] make no sense.
You would also need a
$i--;
after each unset to not skip an element/
Because when you unset $item[45], the next element in the for-loop should be $item[45] - which was [46] before unsetting. If you would not do this, you'd always skip an element after unsetting.
Sorry for the late response, I recently had the same problem with PHP and found out that when working with arrays that do not use $key => $value structure, when using the foreach loop you actual copy the value of the position on the loop variable, in this case $image. Try using this code and it will fix your problem.
for ($i=0; $i < count($images[1]); $i++)
{
if($images[1][$i] == 'http://i27.tinypic.com/29yk345.gif' ||
$images[1][$i] == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$images[1][$i] == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[1][$i]);
}
}
var_dump($images);die();