I'm creating 'Calendar of Event' using Google Calendar API for my project.
To add attendees to my specific event, the API needs the data in this format below.
The format I want (by the Google Calendar API):
array(
array('email' => 'lpage#example.com'),
array('email' => 'sbrin#example.com'),
)
Now, I have my data in just one dimensional array.
Let's say I get them from some form (which works just fine):
$people = $_POST['people'];
How can I convert $people so that it can be readable by the API?
Thank you. Have a nice day
Use array_map() to create an array of arrays from the array.
$data = array_map(function($person) { return ['email' => $person]; }, $people);
or a foreach loop.
$data = [];
foreach ($people as $person) {
$data[] = ['email' => $person];
}
Related
I am using PHP 7.1.33 and Laravel Framework 5.8.36.
I am getting receiving data from a database row-by-row and I am creating a model using updateOrCreate() like the following:
foreach ($arr as $v) {
$product = new Product();
$product->name = $v['name'];
$product->url = $v['url'];
$product->price = $v['price'];
// save
$matchThese = ['name' => $name, 'url' => $url];
$product->updateOrCreate($matchThese);
}
However, nothing gets created.
Any suggestions what I am doing wrong?
I appreciate your replies!
updateOrCreate takes 2 parameters ($attributes, $values).
$attributes is an array containing key-value pairs which will basically be used for where clauses i.e. it will check the database for the attributes passed in this array.
$values is an array containing key-value pairs of what to update in the database.
If it can't find a row in the database matching the first array it will combine the values in the arrays to create a new row.
To achieve what you're after you can do:
Product::updateOrCreate(
['name' => $v['name'], 'url' => $v['url']],
['price' => $v['price']]
);
Try this way
Product::updateOrCreate(
['name' => $name, 'url' => $url],
['price' => v['price']],
)
First parameter is for search the register.
Second parameter set new values.
For more information, you can read Eloquent: Getting Started - Documentation
I have this array:
$datas = array(
array(
'id' => '1',
'country' => 'Canada',
'cities' => array(
array(
'city' => 'Montreal',
'lang' => 'french'
),
array(
'city' => 'Ottawa',
'lang' => 'english'
)
)
)
);
Question 1:
How can I get the the country name when I have the id ?
I tried: $datas['id'][1] => 'country'
Question 2:
How can I loop in the cities when I have the id ?
I tried:
foreach ($datas as $data => $info) {
foreach ($info['cities'] as $item) {
echo '<li>'.$item['city'].'</li>';
}
}
Thanks a lot.
You have the ID of the array you want analyse, but your array is structured as a map, meaning that there are no keys in the outer array. You will therefore have to iterate the array first to find the object you are looking for.
While the first approach would be to search for the object that has the ID you are looking for, i suggest you map your arrays with their IDs. To do that, you can use two PHP array functions: array_column and array_combine.
array_column can extract a specific field of each element in an array. Since you have multiple country objects, we want to extract the ID from it to later use it as a key.
array_combine takes two arrays with the same size to create a new associative array. The values of the first array will then be used as keys, while the ones of the second array will be used as values.
$mappedCountries = array_combine(array_column($datas, 'id'), $datas);
Assuming that the key 1 is stored in the variable $key = 1;, you can afterwards use $mappedCountries[$key]['country'] to get the name of the country and $mappedCountries[$key]['cities'] to get the cities, over which you can then iterate.
if there might be many arrays in $datas and you want to find one by id (or some other key) you can do something like this:
function search($datas, $key, $value) {
foreach($datas as $data) {
if ($data[$key] === $value) {
return $data;
}
}
So if you want to find where id = 1
$result = search($datas, 'id', '1');
and then you can get country echo $result['country'] or whatever you need.
I'm writing a php web application where I have a nested array which looks similar to the following:
$results = array(
array(
array(
'ID' => 1,
'Name' => 'Hi'
)
),
array(
array(
'ID' => 2,
'Name' => 'Hello'
)
),
array(
array(
'ID' => 3,
'Name' => 'Hey'
)
)
);
Currently this means that when I want to use the ID field I have to call $results[0][0]['ID'] which is rather inefficient and with an array of over several hundred records becomes messy quickly. I would like to shrink the array down so that I can call $results[0]['ID'] instead.
My understanding is that a function that uses a foreach loop to iterate through each row in the array and change the format would be the best way to go about changing the format of the $results array but I am struggling to understand what to do after the foreach loop has each initial array.
Here is the code I have so far:
public function filterArray($results) {
$outputArray = array();
foreach ($results as $key => $row) {
}
return $outputArray;
}
Would anyone be able to suggest the most effective way to achieve what I am after?
Thanks :)
Simply use call_user_func_array as
$array = call_user_func_array('array_merge', $results);
print_r($array);
Demo
Friends,
I am creating a drupal form (its my second one yay!) and I am pulling from the database information about Coursework. a very simple coursework_id and a coursework_name.
Now the usual format to populate a drop down menu in Drupal is as follows:
$form['selected'] = array(
'#type' => 'select',
'#title' => t('Selected'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => $category['selected'],
'#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
);
I am trying to pass up my coursework_id and coursework_name in the #options part of the select form described above.
So far I have come up with this line of code:
foreach ($results as $result) {
$courseworks = (array($result->coursework_id => $result->coursework_name));
}
This matches the required format of the Options but however I can only store one result:
Array
(
[2] => Java Programming
)
How could I be able to somehow push the new results. I have tried array_push from PHP but it does seem to work for this case, it can as far as I understand only append a value.
Kind regards,
-D
You should simply try :
foreach ($results as $result) {
$courseworks[$result->coursework_id] = $result->coursework_name;
}
Basically you are redefining $courseworks with every iteration of $results array.
Try this instead;
$courseworks = array();
foreach ($results as $result)
{
$courseworks[] = (array($result->coursework_id => $result->coursework_name));
}
The following should do it for you. You define the array first and then create keys and assign the relative value:
$courseworks = array();
foreach ($results as $result) {
$courseworks[$result->coursework_id] = $result->coursework_name;
}
Here is a sample request which works, but the names are hard coded
$client->batchDeleteAttributes(array(
'DomainName' => $domainName,
'Items' => array(
array('Name' => '5149572a86deb5161fbb22bdab',),
array('Name' => '5149572a86deb5161fbf7487b9',),
)
));
I can get the name values using the following loop
foreach($_POST['d'] as $key => $value)
{
}
I am confused how to integrate the foreach loop with the api request. I assume I need to create an array using the foreach loop and then use that array in the api request but I don't know the syntax.
Thanks!
Without knowing the format of your $_POST data it's hard to give a detailed solution, but something along these lines should get you heading in the right direction:
$names = array();
foreach ($_POST['d'] as $value)
{
$names[] = array('Name' => $value);
}