Php - How do I use a for loop inside API request - php

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);
}

Related

Use Dynamic Array inside Array PHP

I am new in PHP. I am working for send data using PHP API. Its method is like below
$createContact->setCustomFieldValues(
[
array(
'customFieldId' => 'pcVFD6',
'value' => array('35')
),
array(
'customFieldId' => 'pcVnzW',
'value' => array('37')
)
]
);
I have data in array like this
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
I want pass this values to above function but I am not getting idea about proper way to do it.
I have tried something like this
foreach ($aa as $key => $value){
$createContact->setCustomFieldValues([
array(
'customFieldId' => $key,
'value' => array($value)
)
]
);
}
its working but passing only last one array to API. Anyone here can please help me for achieve my goal?
Thanks!
You need to transform the array before you pass it to setCustomFieldValues. You don't want to be calling setCustomFieldValues multiple times as your current attempt does, as that's not equivalent to the original. You just need to change the structure of the array ahead of time.
For example:
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
$transformedArr = array();
foreach ($aa as $key => $value){
$transformedArr[] = array(
'customFieldId' => $key,
'value' => array($value)
);
}
$createContact->setCustomFieldValues($transformedArr);

Why can't arrays be used in multiple foreach loops?

This is a simple question yet I'm unable to find an answer to this. I'm not looking for corrections to my code, simply education regarding this issue.
The array is defined before the first foreach begins so I can use it outside of the loop.
$arrayVar = array();
foreach ($variables as $key => $variable){
$arrayVar = array(
'name' => $squad['full_name'],
'position' => $squad['position']
);
}
This populates the array with data. However, when used in another loop, the array resets instead of appending to the end.
EDIT: John's answer resolves the issue. A simple inclusion of square brackets has saved me around 1000 lines.
You keep overwriting your array in every iteration instead of appending to it.
$arrayVar = array();
foreach ($variables as $key => $variable){
$arrayVar[] = array( // <= Add to array instead of overwriting it
'name' => $squad['full_name'],
'position' => $squad['position']
);
}

How to add data to multidimensional array

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];
}

create json array with php

I find it difficult to create JSON data.
I was wanna make out JSON like the picture below
but I'm having trouble.
I am only able to make like the picture below
The following code I created. please help me, I am really very confused
foreach ($q->result() as $row){
$djson[] = array(
'id' => $row->id_custome,
'name' => $row->nama_custome
);
}
return $djson;
You should use the key/index
foreach ($q->result() as $key=>$row){
$djson[ ( $key + 1 ) ] = array(
'id' => $row->id_custome,
'name' => $row->nama_custome
);
}
return $djson;
Note: $key + 1 because you want to start the json from 1.

Pushing into a 2 dimensional PHP array

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;
}

Categories