I'm trying to dynamically build an array for the Facebook Messenger SDK and populate it with data from a database.
I can't seem to get a properly structured array no matter what I try.
What I need:
$messages = [
"attachment" => [
"type" => "template",
"payload" => [
"template_type" => "generic",
"elements" => [[
"title" => $row['title'],
"item_url" => $row['item_url'],
"image_url" => $row['image_url'],
"subtitle" => $row['subtitle'],
"buttons" => [[
"type" => "web_url",
"url" => "www.google.com",
"title" => "View Website"],
["type" => "postback",
"title" => "Start Chatting",
"payload" => "start"]]
]
]]
]];
I need to create the data inside buttons based on what I have in the database, tried with array_merge and inserting the array as a string:
// Created arrays
if (!empty($row['button1_type'])) {
$buttons[] = array("type" => $row['button1_type'],"url" => $row['button1_url'],
"title" => $row['button1_title'],
"payload" => $row['button1_payload']);
}
if (!empty($row['button2_type'])) {
$buttons[] = array("type" => $row['button2_type'],"url" => $row['button2_url'],
"title" => $row['button2_title'],
"payload" => $row['button2_payload']);
}
// In the case when I had array_merge - $buttons were actually named as $buttons1 and $buttons2
$buttons = array_merge($buttons1, $buttons2);
// Tried to add it as a string
if (!empty($row['button2_type'])) {
$buttons = "\"type\" => ".$row['button2_type'].",\"url\" => .".$row['button2_url'].",
\"title\" => ".$row['button2_title'].",
\"payload\" => ".$row['button2_payload'];
}
$messages = [
"attachment" => [
"type" => "template",
"payload" => [
"template_type" => "generic",
"elements" => [[
"title" => $row['title'],
"item_url" => $row['item_url'],
"image_url" => $row['image_url'],
"subtitle" => $row['subtitle'],
"buttons" => [
$buttons
]
]
]]
]];
Screenshot showing the differences between correct and incorrect:
So basically $buttons are created inside an extra array, how can I get rid of it? Maybe I should change the whole approach?
With
"buttons" => [$buttons]
//new array ^ ^
a new array is being created with square brackets so to avoid it. use
"buttons" => $buttons
Related
I want to get what I have saved in the Query database and convert it into an array. And I want to pass data to the variables that I have passed. Using php
How can I turn a string below into an array?
"UserID=$api_username&Token=$api_token&Account=$number&Amount=$amount&SPKey=$op&APIRequestID=$ref_id&Optional1={Optional1}&Optional2={Optional2}&Optional3={Optional3}&Optional4={Optional4}&GEOCode=[18.273827, 73.879353]&CustomerNumber=1234567890&Pincode=123456&Format=1"
This is the array I am looking for,
array(
"UserID" => $api_username,
"Token" => $api_token,
"Account" => $number,
"Amount" => $amount,
"SPKey" => $op,
"APIRequestID" => $ref_id,
"Optional1" => "{Optional1}",
"Optional2" => "{Optional2}",
"Optional3" => "{Optional3}",
"Optional4" => "{Optional4}",
"GEOCode" => "[18.273827, 73.879353]",
"CustomerNumber" => "1234567890",
"Pincode" => "123456",
"Format" => "1"
)
I used php parse_url() function but I get such an array.
[
"UserID" => "$api_username"
"Token" => "$api_token"
"Account" => "$number"
"Amount" => "$amount"
"SPKey" => "$op"
"APIRequestID" => "$ref_id"
"Optional1" => "{Optional1}"
"Optional2" => "{Optional2}"
"Optional3" => "{Optional3}"
"Optional4" => "{Optional4}"
"GEOCode" => "[18.273827, 73.879353]"
"CustomerNumber" => "1234567890"
"Pincode" => "123456"
"Format" => "1"
]
But I should be Comma Separated.
I need to access the NAME of the languages and their respective ID from a global array. I have added PHP files under the config directory and added the following code. Is it the right way to do it? How can I access it in my controller file?
return [
'Language' => [
array(
"English" => array (
"name" => 'English',
"value" => 101
),
"Turkish" => array (
"name" => 'Turkish',
"value" => 102
))
]
];
Create a file config/language.php with the contents
///config/language.php
return [
"English" => [
"name" => 'English',
"value" => 101
],
"Turkish" => [
"name" => 100,
"value" => 101
]
];
Then anywhere your need you can access like
$lang = config('language.Turkish');
$lang['name'] //will be 100
$lang['value'] //will be 101
In PHP I have a set of nested arrays of data. I want to flatten these, so that for every node and leaf I get a list of its parents.
Here's an example of the original array - note, each part could be of an unknown depth and length:
$data = array(
"name" => "Thing",
"children" => array(
array(
"name" => "Place",
"rdfs:subClassOf" => "schema:Thing",
"children" => array(
array(
"name" => "Accomodation",
"rdfs:subClassOf" => "schema:Place",
"children" => array(
array(
"name" => "Apartment",
"rdfs:subClassOf" => "schema:Accomodation",
),
array(
"name" => "Hotel",
"rdfs:subClassOf" => "schema:Accomodation",
),
array(
"name" => "House",
"rdfs:subClassOf" => "schema:Accomodation",
)
)
)
)
),
array(
"name" => "CreativeWork",
"rdfs:subClassOf" => "schema:Thing",
"children" => array(
array(
"name" => "Article",
"rdfs:subClassOf" => "schema:CreativeWork",
"children" => array(
array(
"name" => "NewsArticle",
"rdfs:subClassOf" => "schema:Article",
),
array(
"name" => "OpinionArticle",
"rdfs:subClassOf" => "schema:Article",
),
)
)
)
)
)
);
(The data I'm actually parsing is this Schema.org JSON file - the above is a minimal example of it.)
And here's what I'd like to end up with:
$results = array(
"Place" => array("Thing"),
"Accomodation" => array("Thing", "Place"),
"Apartment" => array("Thing", "Place", "Accomodation"),
"Hotel" => array("Thing", "Place", "Accomodation"),
"House" => array("Thing", "Place", "Accomodation"),
"CreativeWork" => array("Thing"),
"Article" => array("Thing", "CreativeWork"),
"NewsArticle" => array("Thing", "CreativeWork", "Article"),
"OpinionArticle" => array("Thing", "CreativeWork", "Article"),
);
I'm assuming I need to recursively call a function to build the array but so far I'm not having much luck. In case this makes it harder, this is happening in a static method.
Something quick to get you started:
class Parser {
public static function parse($input,$prefix = []) {
$return = $prefix ? [$input['name']=>$prefix] : [];
if (isset($input['children'])) {
$prefix[] = $input['name'];
foreach ($input['children'] as $child) {
$return += self::parse($child,$prefix);
}
}
return $return;
}
}
var_dump(Parser::parse($data));
You probably need to add a few checks and comments to make it more readable.
I'm struggling to update a document and add new elements (fields) into it's existing array without losing the array elements on update.
This is my code which inserts a new document into the collection if it doesn't already exist (upsert):
$updateResult = $collection->findOneAndUpdate(
[
'recording-id' => $out['recording']->id
],
['$set' => [
'release' => [
'id' => $out['release']->id,
'title' => $out['release']->title,
'date' => $out['release']->date,
'country' => $out['release']->country
],
'artist' => [
'id' => $out['artist']->id,
'name' => $out['artist']->name,
],
'recording' => [
'id' => $out['recording']->id,
'title' => $out['recording']->title,
'score' => $out['recording']->score,
'length' => $out['recording']->length,
'release-count' => count($out['recording']->releases),
],
'release-group' => [
'id' => $out['release-group']['id'],
'title' => $out['release-group']['title'],
'first-release-date'=>$out['release-group']['first-release-date'],
'primary-type' => $out['release-group']['primary-type'],
'musicbrainz' => $out['release-group']['musicbrainz'],
'url-rels' => $out['release-group']['url-rels'],
'coverart' => $out['release-group']['coverart']
],
'execution' => [
'firstfind' => $out['execution']->time
]
]
],
['upsert' => true,
'projection' =>
[
'_id' => 0,
'release' => 1,
'artist' => 1,
'recording' => 1,
'release-group' => 1,
'execution' => 1
],
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);
So now I have an existing document in a collection:
{
"_id" : ObjectId("5d1a6aaf5ecc8001ee858f6c"),
"recording-id" : "d0d439f9-5324-4728-8706-2da39adb89c5",
"artist" : {
"id" : "9d97b077-b28d-4ba8-a3d9-c71926e3b2b6",
"name" : "Gordon Lightfoot"
},
"recording" : {
"id" : "d0d439f9-5324-4728-8706-2da39adb89c5",
"title" : "Sundown",
"score" : 100,
"length" : 184000,
"release-count" : 2
},
"release" : {
"id" : "0c008d76-2bc9-44a3-854b-0a08cde89337",
"title" : "All Live",
"date" : "2012-04-24",
"country" : "CA"
},
"release-group" : {
"id" : "0a5d5f33-8e9d-4fa4-b622-a95e4218a3c4",
"title" : "All Live",
"first-release-date" : "2012-04-24",
"primary-type" : "Album",
"musicbrainz" : "https://musicbrainz.org/release-group/0a5d5f33-8e9d-4fa4-b622-a95e4218a3c4",
"url-rels" : "https://musicbrainz.org/ws/2/release-group/0a5d5f33-8e9d-4fa4-b622-a95e4218a3c4?inc=url-rels&fmt=json",
"coverart" : null
}
}
Now, I would like to update this document, and add new fields into the arrays. The new fields are to be added to certain fields.
Here is the code doing that:
$collection = (new MongoDB\Client)->stream->musicbrainz;
$updateResult = $collection->updateOne(
[
'recording-id' => $out['recording']['id']
],
['$addToSet' => [
'artist' => [
'wikiQiD' => $out['artist']['qid'],
'wiki-extract' => $out['artist']['wiki-extract'],
'wiki-pageid' => $out['artist']['pageid'],
],
'release-group' => [
'wikiQiD' => $out['release-group']['qid'],
'wiki-extract' => $out['release-group']['wiki-extract']
]
]
],
[
'upsert' => true,
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);
I've noticed there's "$addToSet" and "$push" commands, and could use assistance with what the difference is between these two commands.
If the field is absent in the document to update, $push adds the array
field with the value as its element.
The $addToSet operator adds a value to an array unless the value is
already present, in which case $addToSet does nothing to that array.
I did some googling, and reading of the MongoDB/Client UpdateOne function, but can't seem to find a way to append these fields to the existing arrays.
The error I'm getting is:
Fatal error: Uncaught MongoDB\Driver\Exception\BulkWriteException: The field 'artist' must be an array but is of type object in document {_id: ObjectId('5d1a6aaf5ecc8001ee858f6c')} in ...
I know the following:
It could be my document, as it's not a proper array that Fatal error is complaining about.
It could be my `findOneAndUpdate' formatting, and I'm not doing that correctly.
It could be both and I have it all wrong from the very start.
Any insight or constructive criticism is appreciated, just refrain from flames, pls.
Here's the working code I finally use to get it do what I want it to.
It's a simple matter of just setting your field names and values and mongo will update the found record replacing it with the array sent to it. No need to push or anything.
function firstFindMongoUpdate($out) {
$collection = (new MongoDB\Client)->stream->musicbrainz;
$updateResult = $collection->findOneAndUpdate(
[
'recording-id' => $out['recording']->id
],
['$set' => [
'query' => $out['query'],
'release' => [
'id' => $out['release']->id,
'title' => $out['release']->title,
'date' => $out['release']->date,
'country' => $out['release']->country,
'label' => $out['release']->label,
],
'artist' => [
'id' => $out['artist']->id,
'name' => $out['artist']->name,
'wiki' => $out['artist']->wiki,
],
'recording' => [
'id' => $out['recording']->id,
'title' => $out['recording']->title, // sometimes contains apostophe ie; Bill‘s Love (option ] key)
'score' => $out['recording']->score,
'length' => $out['recording']->length,
'release-count' => count($out['recording']->releases)
],
'release-group' => [
'id' => $out['release-group']['id'],
'title' => $out['release-group']['title'],
'first-release-date'=>$out['release-group']['first-release-date'],
'primary-type' => $out['release-group']['primary-type'],
'musicbrainz' => $out['release-group']['musicbrainz'],
'url-rels' => $out['release-group']['url-rels'],
'coverart' => $out['release-group']['coverart'],
'wiki' => $out['release-group']['wiki']
],
'execution' => [
'artistQuery' => $out['execution']->artistQuery,
'recordingQuery'=> $out['execution']->recordingQuery,
'time' => $out['execution']->time
]
]
],
['upsert' => true,
'projection' =>
[
'_id' => 1,
'query' => 1,
'release' => 1,
'artist' => 1,
'recording' => 1,
'release-group' => 1,
'execution' => 1
],
'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);
In the array below, how can I push the new array into the $options array at the specified location?
$options = array (
array( "name" => "My Options",
"type" => "title"),
array( "type" => "open"),
array("name" => "Test",
"desc" => "Test",
"id" => $shortname."_theme",
"type" => "selectTemplate",
"options" => $mydir ),
//I want the pushed array inserted here.
array("name" => "Test2",
"desc" => "Test",
"id" => "test2",
"type" => "test",
"options" => $mydir ),
array( "type" => "close")
);
if(someCondition=="met")
{
array_push($options, array( "name" => "test",
"desc" => "description goes here",
"id" => "testMet",
"type" => "checkbox",
"std" => "true"));
}
You can use array_splice. For your example:
if($some_condition == 'met') {
// splice new option into array at position 3
array_splice($options, 3, 0, array($new_option));
}
Note: array_splice expects the last parameter to be an array of new elements, so for your example you need to pass an array containing the new option's array.
Simply
array_splice($options, 3, 0, $newArr);
for insert a new array, not to a spesific location(between $r[4] and $r[5]):
$options[]=array ("key" => "val"); //insert new array
$options[]=$v; //insert new variable
to insert a new array after spesific variable:
function array_push(&$array,$after_element_number,$new_var)
{
array_splice($array, $after_element_number, 0, $new_var);
}
if(someCondition=="met")
{
array_push($options, 2, array( "name" => "test",
"desc" => "description goes here",
"id" => "testMet",
"type" => "checkbox",
"std" => "true"));
}
As connec says, you can use array_splice, but without forgetting to wrap your array in another array, like this:
if ('met' === $some_condition)
{
array_splice($options, 3, 0, array(array(
'name' => 'test',
'desc' => 'description goes here',
'id' => 'testMet',
'type' => 'checkbox',
'std' => 'true'
)));
}
Edit: connec has already specified its response.