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.
Related
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);
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);
}
I have this multidimensional array which I'll name "original":
$original=
array
0 =>
array
'animal' => 'cats'
'quantity' => 1
1 =>
array
'animal' => 'dogs'
'quantity' => '1'
2 =>
array
'animal' => 'cats'
'quantity' => '3'
However, I want to merge internal arrays with the same animal to produce this new array (with quantities combined):
$new=
array
0 =>
array
'animal' => 'cats'
'quantity' => 4
1 =>
array
'animal' => 'dogs'
'quantity' => '1'
I understand that there are similar questions on stackoverflow, but not similar enough for me to be able to figure out how to use the feedback those questions have gotted to apply to this specific example. Yes, I know I probably look stupid to a lot of you, but please remember that there was a time when you too didn't know crap about working with arrays :)
I've tried the following code, but get Fatal error: Unsupported operand types (Referring to line 11). And if I got that error to go away, I'm not sure if this code would even produce what I'm trying to achieve.
$new = array();
foreach($original as $entity){
if(!isset($new[$entity["animal"]])){
$new[$entity["animal"]] = array(
"animal" => $entity["animal"],
"quantity" => 0,
);
}
$new[$entity["animal"]] += $entity["quantity"];
}
So, I don't know what I'm doing and I could really use some help from the experts.
To try to give a super clear question, here goes... What changes do I need to make to the code so that it will take $original and turn it into $new? If the code I provided is totally wrong, could you provide an alternative example that would do the trick? Also, the only language I am familiar with is PHP, so please provide an example using only PHP.
Thank you
You're very close.
$new[$entity["animal"]] += $entity["quantity"];
needs to be
$new[$entity["animal"]]['quantity'] += $entity["quantity"];
In your if ( !isset [...] ) line, you're setting $new[$entity['animal']] to an array, so you need to access the 'quantity' field of that array before trying to add the new quantity value to it.
One of the reasons why your code is not working is that you're using the animal name as the array index, not the integer index which is used in your desired output.
Try this:
$new = array(); // Desired output
$map = array(); // Map animal names to index in $new
$idx = 0; // What is the next index we can use
foreach ($original as $entity) {
$animal = $entity['animal'];
// If we haven't saved the animal yet, put it in the $map and $new array
if(!isset($map[$animal])) {
$map[$animal] = $idx++;
$new[$map[$animal]] = $entity;
}
else {
$new[$map[$animal]]['quantity'] += $entity['quantity'];
}
}
This works:
$new = array();
$seen = array();
foreach($original as $entity) {
// If this is the first time we're encountering the animal
if (!in_array($entity['animal'], $seen)) {
$new[] = $entity;
$seen[] = $entity['animal'];
// Otherwise, if this animal is already in the new array...
} else {
// Find the index of the animal in the new array...
foreach($new as $index => $new_entity) {
if ($new_entity['animal'] == $entity['animal']) {
// Add to the quantity
$new[$index]['quantity'] += $entity['quantity'];
}
}
}
}
Your example was using the animal name as the index, yet the actual index is just an integer.
However, I think the resulting array would be easier to use and easier to read if it was formatting like this instead:
array('cats' => 4, 'dogs' => 1)
That would require different but simpler code than above... but, it wouldn't be a direct response to your question.