I have been trying to figure this out but keep getting confused with code examples.
How can I add a single element to the array below.
$datafields = array(
"helpdesk_ticket" => array(
"subject" => $subject,
"description" => $notes,
"email" => "joe#email.net",
"priority" => $priority2,
"ticket_type" => $type,
"viewed" => $viewed
)
);
So for instance I want to add:
"status" => $status
This is just a simple array assignment.
$datafields['helpdesk_ticket']['status'] = $status;
For more info read the manual about arrays
Related
I am creating a response from my own webhook.
Now i wanted to send the suggestions array, but am struggling with creating an array in an array. How does these need to be set up?
$jsonResponse = json_encode(array(
"session" => array(
"params" => array(
"antA" => "Hello Answer A",
"antB" => "Hello Answer B",
"extrainfo" => "This is some extra information"
)
),
"prompt" => array(
"override" => false,
"firstSimple" => array(
"speech" => "<speak>".$speech."</speak>"
),
"suggestions" => array(
"title" => "aa",
"title" => "bb",
"title" => "cc"
),
)
));
The issue is that prompt.suggestions is specified to take an indexed array of Suggesion objects, that is, an array that maps from numbers to the objects (or just a list, where the numbering is assumed). But you are providing an associative array - that is, mapping a property name to something. Furthermore, your associative array is naming everything the same. Php uses similar syntax for indexed arrays and associative arrays, so it can sometimes be unclear what you actually need.
In this case, that part of your code should probably look something more like this:
"suggestions" => array(
array("title" => "aa"),
array("title" => "bb"),
array("title" => "cc")
)
I have this multidimensional array in php:
$products = array(array(
"name" => "Hannah",
"id" => "eg01",
"price" => 120
),
array(
"name" => "Natasha",
"id" => "eg02",
"price" => 125
));
How do I push new data in the array and save it?
I tried this code:
array_push($products, $name, $id, $price);
but it only replaces the new one every time I click the push button.
I wanted it to be saved every time i push.
Either you can use array_push() method for inserting records in an array or we can append data like this
$products[] = ['key'=>'value'];
Your array is made of sub arrays with named keys, you need to construct your inner array first.
array_push($products,array('name'=>$name,'id'=>$id,'price'=>$price));
or for verbosity
$item = array(
'name' => $name,
'id' => $id,
'price' => $price
);
array_push( $products, $item );
you may also see this syntax
$products[] = array('name'=>$name,'id'=>$id,'price'=>$price);
in which you can optionally specify a key if needed between the [] notation
I am doing Jira REST API calls and I am wondering how I can dynamically add more than one component to the components field using REST API in PHP. I have the following code, works when I set it static, but not sure how to do it dynamically.
Example of static component set:
$data = array(
'fields' => array(
'project' => array(
'key' => $rowAnswers["Key"]
),
'summary' => $rowAnswers["Summary"],
'description' => $rowAnswers["Description"],
'issuetype' => array(
'name' => $rowAnswers["IssueType"]
),
'components' => array(
array(
"name" => "component1"
),
array(
"name" => "component2"
)
)
),
);
My array that I want to replace the static content with:
$components = explode(",", $rowAnswers["Components"]);
$arr = array();
foreach($components as $value){
$array = array("name"=>$value);
array_push($arr,$array);
}
Replacing
'components' => array(
array(
"name" => "component1"
),
array(
"name" => "component2"
)
)
with
'components' => [
$arr
]
doesn't work, I get:
"{"error":false,"error_msg":"","data":"{\"errorMessages\":[],\"errors\":{\"components\":\"expected Object\"}}"}"
I see on an api call to get a request it looks like this:
[components] => Array
(
[0] => stdClass Object
(
[name] => component1
)
[1] => stdClass Object
(
[name] => component2
)
)
But I am unsure how to transform an array into this type of object or request in PHP. Calling with PHP-cURL and json_encoding the data it sends.
Thanks in advance!
you need to decode your json as associative array by setting the second parameter to true
check out the json_decode
assoc
When TRUE, returned objects will be converted into associative arrays.
To fix this I had to do the following:
When creating the array from the DB:
$components = explode(",", $rowAnswers["Components"]);
$arr = array();
foreach($components as $value){
$array = json_decode(json_encode(array("name"=>$value)), FALSE);
array_push($arr,$array);
}
Then to set the component in the request:
'components' => $arr
Thanks
I have the following array from an API
$arrs = array(
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
);
The API require this information to work unfortunately I can only have one array per request, so I send this question to the developers:
How can I list multiple item such as:
$arrs= array(
"items1" => array (
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
)
"items2" => array (
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
)
"items3" => array (
"id" => $ids,
"names" => $names,
"description" => $desc,
"picture_url" => $p_img;
)
));
And they told me that at the moment is not possible, so, the "important" part of this array is the "names", when it is use with a single item there is no problem I get a single name, done, no problem, but what if I have multiple names? I can send multiple request but that will be seen as a flood or something like... just imaging 300 names = 300 request in one second or so... sure I can put a pause per request but is not efficient...
the API will read something like this...
"id" => 654,
"names" => "John", // <-- Lets look at this...
"description" => "Fancy desc...",
"picture_url" => "http"//domain.com/assets/user/654/av_654_dd.jpg";
So before I output the array I have an SQL Query with a while to display the information...
while ($names = $listnames->fetch_assoc()) {echo $names['names']. ', ';}
This will display... John, Karl, Lisa, Mark... so this same structure I'd love to put it into my array... the thing is I can't put a while after the => ... that would be silly and it wont work...
"id" => 654,
"names" => "John, Karl, Lisa, Mark", // <-- Lets look at this...
"description" => "Fancy desc...",
"picture_url" => "http"//domain.com/assets/user/654/av_654_dd.jpg";
if I need only one name then there is not problem... but in this case I need to put all of the name as a value, so, how can get the result from a WHILE loop.... so that I can use that result elsewhere...
Thank you for taking the time..
while ($names = $listnames->fetch_assoc()) {
$name_array[] = $names['names'];
}
$arrs=array(
"items1" => array (
"id" => $ids,
"names" => implode(', ', $name_array),
"description" => $desc,
"picture_url" => $p_img;
)
);
We don't know how you access to the API.
If this is a REST API, you are able to do only what the developers planned.
If this is a framework or another library, you may edit it.
And you should receive your results like theses:
$arrs = array(
array(
"id" => 1,
"names" => 'MyName',
"description" => 'MyDesc',
"picture_url" => 'MyPic',
),
array(
"id" => 2,
"names" => 'MyName',
"description" => 'MyDesc',
"picture_url" => 'MyPic',
),
);
So if you have full access to the SQL results, to get all results' data, you could do:
$results = array();
while( $row = $listnames->fetch_assoc() ) {
$results[] = $row;
}
If you want to set several possible name in input, the developers should develop that is possible with array of values.
For names, they just have to code: "LIKE '".implode("', LIKE '", $names)."'"
They could also add '%' to allow more values, e.g. 'John' for 'Johnny'.
I think we need more informations to really help you.
I want to output users via a json object but when I try to output their songs list it only outputs the last one. I want to get this list into an array.
this is my array push while looping through users,
array_push($arrayUsers, array(
'username' => $user['username'],
'id' => $user['_id'],
'favSongs' => array(
'title' =>'song1',
'title' =>'song2'
)
)
);
but this is what I get back (missing song title),
[{"username":"asdfasdfasd","id":{"$id":"4f58d7227edae19c02000000"},"songs":{"title":"song2"}}]
I want it to output the songs like this, but am confused how to get it to do this using PHP:
"songs":[{"title": "song1"}, {"title": "song2"}]
'favSongs' => array(
'title' => 'song1',
'title' => 'song2'
)
PHP will replace the 'title' key with the last one declared.
"songs":[{"title": "song1"}, {"title": "song2"}]
This is an array of objects, so in PHP it needs to be an array of arrays.
'favSongs' => array(
array('title' => 'song1'),
array('title' => 'song2')
)