I have a two differnt array & i want to remove items in the second one that have the same property value of array1 - arrays

inside the code $response['results'] is for google api. And $da is my databse records I am comparing these two using foreach on the basis of latitude of both data records. please help me out.
Here is my code :
$setarraymy= [];
$setarray = [];
foreach($da as $data1){
foreach ($response['results'] as $res){
switch (true){
case (round($data1['location']['lat'],4)!== round($res['geometry']
['location']['lat'],4) && !in_array($res['geometry']['location']['lat'],$setarray)):
array_push($setarray,$res);
break;
case (!in_array($data1['location']['lat'],$setarraymy)):
array_push($setarraymy,$data1);
break;
default:
break;
}
}
}
echo json_encode(array( 'Results' =>$setarraymy,
'status'=>'OK'),JSON_PRETTY_PRINT);
$new_array = array_unique($setarray, SORT_REGULAR);
echo json_encode( array('Results'=>$new_array,
'status'=>'OK'),JSON_PRETTY_PRINT);
return;

Manipulating array will be difficult, better to use collections and lastly convert collection to array as per your logic.
Avinash

Related

Swift array filter with empty filter value

I am filtering an array based on a set filter supplied to the view (this filter varies) but want to modify the function to allow for an empty filter, i.e return the original array un filtered.
I feel like this should be a simple matter but I'm currently a bit lost - have looked at the .isEmpty modifier and a couple of different IF statements but I'm not "getting it"
var categoryFilter:String
var filteredCategoryTasks: [Task] {
modelData.tasks.filter { task in
(task.category == categoryFilter)
}
}
You could return the original array in the event that the String is empty and the filtered array otherwise:
var filteredCategoryTasks: [Task] {
categoryFilter.isEmpty ? modelData.tasks : modelData.tasks.filter { task in
(task.category == categoryFilter)
}
}

How to call dynamically an array by concatenate a string with a number in Dart

say I have Some List inside a List.
List<List> rules = [
[],
[],
[],
];
Beside, I have 3 list.
They will have some widgets inside, base on another process
List<TextSpan> arrayRule1 = [];
List<TextSpan> arrayRule2 = [];
List<TextSpan> arrayRule3 = [];
Say i want to loop through rules, and loop on each array in it.
How can i can make a dynamic call by concatenate the index of the loop with a string, to use the actual existing array ?
int indice = 0;
for (List rule in rules) {
indice++
for (String statement in rule) {
if (statement == "vrai")
arrayRule+indice.add(// code... ) // Throw an error
else if (statement == "faux" || statement == "fausse")
arrayRule+indice.add(// code... ) // Throw an error
else
arrayRule+indice.add(// code... ) // Throw an error
}
}
I do know how to do this in Javascript, but in Dart, I'm lost
I don't want to make a If...else statement just to select my previous arrayRule1 arrayRule2 arrayRule3 based on the indice variable.
Any idea would be welcome, thanks !
Reflection is not easy in dart, look for dart:mirrors package but I think it does not work in flutter. You have to find another way of doing it.
What you can do it create a map like this
List<TextSpan> arrayRule1 = [];
List<TextSpan> arrayRule2 = [];
List<TextSpan> arrayRule3 = [];
Map<int, List<TextSpan>> arrayRuleMap = {
1: arrayRule1,
2: arrayRule2,
3: arrayRule3,
};
then instead of
arrayRule+indice.add(// code... ) // Throw an error
You can use
arryaRuleMap[indice].add(// code... ) // Throw an error
Edit
For more simplification you can directly initialise array in map instead of creating 3 variables.
Map<int, List<TextSpan>> arrayRuleMap = {
1: [],
2: [],
3: [],
};

Storing single data values in the database

I wanted to know what the best way is to store single data values in a database. Values you only need one field of.
I have a couple of ideas.
Storing all fields in a single_data_table with a id, key and value column.
Storing it as a json object in the database.
Storing it as an array in the database.
Which one is the best way? Or might there even be better ways?
Or is it just easier to keep single data values as static data on the webpage?
At my current job we use a separate settings table for that, with two columns: key and value. And then there's a simple function to retrieve or save a setting:
function setting($key, $value = null){
$setting = DB::table('settings')->where('key', $key)->first();
if(is_null($value)){
return $setting->value ?? null;
}
if(isset($setting)){
$setting->value = $value;
}else{
$setting = $setting ?: new stdClass();
$setting->key = $key;
$setting->value = $value;
}
DB::table('settings')->insertOrReplace((array) $setting);
return true;
}
It is used like so:
$phone = setting('phone'); // Get the phone setting
$url = setting('url', 'http://example.com'); // Set url setting

Google DataStudio row dimension limit

Google's DataStudio seems to have a limit for "row dimensions" to 2 if you use the "pivot table".
Is there a way to increase that limit?
Also there is a limit of 10 "row dimensions" if you use the normal table instead of the pivot table.
First of all, irrespective of the number of rows, you should use a single return statement.
Pass entire data (any number of rows of data) into a single ARRAY. In my case data[] is an Array where I'm passing more than 2 rows into this single array and returning the statement.
dataObj1 is the JSON Object and set1 is the variable name of the dataSet inside the dataObj1
dataObj2 is the another JSON Object and set2 is the variable name of the dataSet inside the dataObj2
var data = []; //this is the single array I'm talking above
dataObj.set1.forEach(function(play) {
var values = [];
dataSchema.forEach(function(field) {
switch (field.name) {
case 'total_impressions':
values.push(play.impr_sns);
break;
default:
values.push('');
}
});
data.push({
values: values
});
});
dataObj2.set2.forEach(function(play) {
var values2 = [];
dataSchema.forEach(function(field) {
switch (field.name) {
case 'total_impressions':
values.push(play.impr_sns);
break;
default:
values.push('');
}
});
data.push({
values: values2 /*this is second array name values2[] but pass into data[] array
itself*/
});
});
return { /* finally return rows:data */
schema: dataSchema,
rows: data
};
}

Accessing array values of a field collection in a node with Drupal?

Please bare with a very recent user of Drupal.
I want to create an array out of all examples of the string "url" on a Drupal site.
I've used the method "field_get_items" previously to do something very similar, but I am now trying to access a field collection that is many levels deep into the node's array and I'm not sure that method would work.
$website_urls = array();
$faculty_members = field_get_items('node', $node, 'field_faculty_member');
for ($i = 0; $i < count($faculty_members); $i++) {
$value = field_view_value('node', $node, 'field_faculty_member', $faculty_members[$i]);
$field_collection = $value['entity']['field_collection_item'][key($value['entity']['field_collection_item'])];
$website_urls[] = render($field_collection['field_link']['#items'][0]['url']);
}
An example of one url's location is...
['field_faculty_program'][0]['entity']['field_collection_item'][1842]['field_faculty_member'][0]['entity']['field_collection_item'][1843]['field_link']['#items'][0]['url']
..and another...
['field_faculty_program'][4]['entity']['field_collection_item'][1854]['field_faculty_member'][0]['entity']['field_collection_item'][1855]['field_link']['#items'][0]['url']
What is the method I should be using to collect al of the 'url' strings for placement in an array?
You can actually still use the field_get_items() function but eventually pass it 'field_collection_item' instead for the node type.
Something like this should work:
if ($items = field_get_items('node', $node, 'field_faculty_member')) {
//loop through to get the ids so we can take
//advantage of field_collection_item_load_multiple for
//greater efficiency
$field_collection_item_ids = array();
foreach ($items as $item) {
$field_collection_item_ids[] = $item['value'];
}
if ($field_collection_items = field_collection_item_load_multiple($field_collection_item_ids)) {
foreach ($field_collection_items as $subitem) {
//now we load the items within the field collection
if ($items = field_get_items('field_collection_item', $subitem, 'field_faculty_member')) {
//And you can then repeat to go deeper and deeper
//e.g. a field collection item within a field collection
//for instance to get the urls within your faculty members
//item. Best to break this into functions or a class
//to keep your code readable and not have so many nested
//if statements and for loops
}
}
}
}
Hope that helps!
Scott

Resources