I have a loop and each time the loop runs I need to add two variables to an array. What I am trying right now is:
$attach_array['outline'] = array();
foreach ($_POST['attachment'] as $key => $value) {
$attachmentName = $value['name'];
$path = "1";
$name = "alsdkjf";
$attach_array['outline']['path']=$path;
$attach_array['outline']['name']=$name;
}
Then later in the script I try to get these values out for PHPMAILER:
foreach ($attach_array['outline'] as $key => $value) {
$mail->AddAttachment($value['path'], $value['name']);
}
This and other attempts are not working so I'm hoping for some help on putting $name and $path into an array in my first loop to use later.
You are overriding the same variables on each loop. You should do something like this:
$attach_array['outline'][] = array('path' => $path, 'name' => $name);
By doing this, now all the path and values will remain on the array as separate items. You dont have to change the code you are using it from.
Related
2 values are collected from the form as variables and rest as array. Then saving the values inside foreach loop in store method.
There is no problem with Array values, but for single variables, the values are showing null for the way I am trying.
I am trying to save $start and $end values inside the foreach loop. The code is below:
$start = $request->start;
$end = $request->end;
foreach ($request->user_id as $key => $val) {
$bill = new UserBill;
if (is_array($request->checked) && in_array($val, $request->checked)) {
$bill->start = $start;
$bill->end = $end;
$bill->amount = $request->amount[$key];
$bill->package = $request->package[$key];
$bill->package_type = $request->package_type[$key];
$bill->user_id = $val;
$bill->save();
}
}
Please help.
You can try sending the information directly to the save method instead populating an object.
foreach ($request->user_id as $key => $val) {
$bill = new UserBill;
if (is_array($request->checked) && in_array($val, $request->checked)) {
$bill->save([
"start" => $request->start,
"end" => $request->end,
"amount" => $request->amount[$key],
"package" => $request->package[$key],
"package_type" => $request->package_type[$key],
"user_id" => $val
]);
}
}
Just make sure all the variables has content. You can check by using the dd() helper
dd($request->toArray()); //toArray() for an easier looking on the output
check your variables are declared in your model in the fillable part https://laravel.com/docs/7.x/eloquent#mass-assignment , also try to put Log to see the values of your variables:
Log::info($start)
Log::info($end)
here is what I'm trying to do. I'm retrieving information from a database via array. What is happening is the information from the previous array is going into the next array.
Here is the code:
$i = 0;
foreach ($array_name as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$array_name[$i]['new_infroamtion'] = $data'
}
}
So right now based on the code data from the table is correctly going into the first array, however, information based from the first array is going into the second array..
Let me know if you need anymore information.
Thank you
You are using $array_name while you are iterating through $array_name. This is valid code if you want to do this, but I don't think you do. You need to change the second $array_name to something else.
$i = 0;
foreach (**$array_name** as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id'] as $key => $test_id {
$data = ModelClass::Information($test_id);
**$array_name**[$i]['new_infroamtion'] = $data
}
}
I did find a solution. What I had to do was add the following
$s = array()
Then in the for loop, I added the following code:
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$s[] = $data
$array_name[$i]['new_infroamtion'] = $s'
}
I was wondering if it was possible to dynamically create variables like so:
$sender = $email = $number = $message = "";
foreach ($_POST as $key => $value) {
if isset & !empty //loosely typed
$+key = $value // can something like this be done?
}
if so how?
PHP does have variable variables, but you don't need that here. I think a simple associative array or object will work just fine for you.
$data = array(
'sender' => 'Someone',
'email' => 'test#example.com',
'number' => 12345,
'message' => 'some message'
);
echo $data['sender']; // Someone
Something like this should work. But I wouldn't do it unless you completely trust the data and if its POST data then access it like $_POST['key']
foreach ($_POST as $key => $value) {
if isset & !empty //loosely typed
${$key} = $value // can something like this be done?
}
There is a PHP function called extract which does what you are looking for.
http://nz2.php.net/extract
I have this loop, where I'm trying to generate an array like this:
array( $file['file_type'] => $file['import_status] )
To do so, I build this foreach loop to access the $file variable, which has the data I wanted to store.
$filesStatusArray = array();
foreach ($filesToImport as $keys => $file) {
$filesStatusArray = $file['import_status'];
}
This way I'm doing, I have only an array with the import_status values.. but I wanted the key to be the file_type... how can I do that?
$filesStatusArray = array();
foreach ($filesToImport as $keys => $file) {
$filesStatusArray[$file['file_type']] = $file['import_status'];
}
i am trying to set the web addresses as the valves and set the keys to be short names for the sites. Not sure where im going wrong. Everytime i try and run it it keeps saying line 11 which is $http://www.yahoo.co.uk/= array( key => value,("yahoo_uk");
$http://www.yahoo.co.uk/= array( key => value,("yahoo_uk");
foreach ($array as $key =>$value) {
echo $value;
}
?>
</body>
wow :P There are so many syntax errors I don't even know where to begin
Here's the correct syntax
$array = array('http://www.yahoo.co.uk' => 'yahoo_uk');
Read this chapter of the manual:
http://php.net/manual/en/language.types.array.php
It seems you intend to do something like:
$urls = array();
$urls['yahoo_uk'] = "http://www.yahoo.co.uk/";
This initializes an array to store URLs, then creates an array member with the short name yahoo_uk as key, and its corresponding URL as the value.
You can then access it with foreach:
foreach ($urls as $name => $url) {
echo "name: $name, url: $url\n";
}
I assume this is what you were going for
<?php
$array = array('http://www.yahoo.co.uk/' => 'yahoo_uk');
foreach ($array as $key =>$value) {
echo $value;
}
?>
You are trying to set constants within your array and you're using incorrect PHP syntax. Try this instead:
$urls = array('yahoo_uk' => 'http://www.yahoo.co.uk/');
foreach ($urls as $key => $value) {
echo $value;
}
Or calling a single value like this:
echo $urls['yahoo_uk']; // http://www.yahoo.co.uk/
Also, your question is very vague and hard to understand.
Try this code:
$yahoo = array_assoc('http://www.yahoo.co.uk/' => 'yahoo_uk');
foreach ($yahoo as $key => $value) {
echo $value;
}
?>