IDE Displaying Error when [( Is Used in An Array Variable - php

I am using Aptana Studio 3 and it's highlighting the square brackets as a potential error. It inteferes with my coding because I keep thinking that am messing somewhere. The code is running well and no error is displayed on the console. How do I fix it?
$series_data[] = array(
'name' => 'Brands',
'colorByPoint' => true,
'data'=> array(
['name' => 'Nairobi, KE', 'y' => 56.33],
['name'=> 'Kisumu, KE', 'y'=>64.03 ],
['name'=> 'New Yorm, USA', 'y'=>44.03 ]
The error is displayed between ( and [ as below:
data=array([

The [...] array syntax is only valid in PHP 5.4 and higher, so possibly the problem is that your IDE is configured to expect PHP 5.3 syntax? Check the IDE's config.
Also, can I recommend that you be a bit more consistent in which array syntax you use? There's nothing inherently wrong with mixing between array(...) and [...] notations, but it does make the code harder to read.

Check this
you forgot to use )
$series_data[] = array(
'name' => 'Brands',
'colorByPoint' => true,
'data'=> array(
['name' => 'Nairobi, KE', 'y' => 56.33],
['name'=> 'Kisumu, KE', 'y'=>64.03 ],
['name'=> 'New Yorm, USA', 'y'=>44.03 ]
)
);

Related

Swagger-UI GET-parameter serialization for nested arrays

I have this PHP snippet:
<?php
$json = '{"number":[{"value":"5121387","operator":"="},{"value":"5121391","operator":"="}]}';
$array = json_decode($json, true);
echo http_build_query($array);
and it generates this (for better readability I decoded those %XY Brackets):
number[0][value]=5121387&number[0][operator]=%3D&number[1][value]=5121391&number[1][operator]=%3D
This is excellent and works for my case.
When I try to generate this form-URL-encoding by Open-API/Swagger-UI it doesnt work this way:
[
'name' => 'number',
'in' => 'query',
'style' => 'form',
'description' => 'some description',
'explode'=> true,
'collectionFormat' => 'multi',
'schema' => [
[...],
],
];
instead it generates me this GET-query appended to my URL:
number[value]=5121387&number[operator]=%3D&number[value]=5121391&number[operator]=%3D
the Open-API Doc says the same - but it doesnt say how to fix this - the problem is the missing Array-indicators e.g. "[0]":
I have already checked the swagger-ui-bundle.js from the Swagger-ui to alter the form-encoding myself - but it is minified and I dont get it...
Do you have any Idea how I can teach my .yaml or Swagger-UI to add those numbered brackets for Arrays?

PHP::MongoCollection->aggregate() Failures

I have a MongoDB query that is verified as 100% working ( Using MongoHub I have connected to the Replica Set and run the query and received results ), but when converting this query to PHP and attempting to run it through MongoCollection->aggregate(), I fail to get a return/result of any kind whatsoever ... not even an error.
Here is the query, as put into a PHP Array ( as MongoCollection requires ):
$query = array(
'$match' => array(
'$and' => array(
'make' => $props[0],
'model' => $props[1],
'makeYear' => (integer)$props[2],
'status' => 'Active'
)
),
'$group' => array(
'_id' => null,
'marketTotal' => array('$sum' => '$price'),
'count' => array('$sum' => 1)
)
);
The code to run the query is a simple one-liner calling aggregate.
As I don't get errors ... or a log showing any sort of error ... I'm kind of at a total loss here. Is anyone familiar with using PHP w/ MongoDB able to see what I might be doing wrong?
Turns out I was simply missing a layer of arrays ... wrapping each piece of the '$and' array in its own array ... so array('make' => $props[0]), etc ... made it work.
Fun stuff. MongoDB queries are easy. Translating them into PHP-compatible arrays is apparently very difficult and requires a lot of guesswork because it's not 1-to-1

Is there a query language to filter array?

I was wondering if there is a library that implements a SQL-like interface to access data in array, e.g.
Input:
[
['name' => 'Tom', 'age' => 27, 'location' => ['country' => 'GB']],
['name' => 'Jerry', 'age' => 16, 'location' => ['country' => 'LT']],
['name' => 'Stuart', 'age' => 26, 'location' => ['country' => 'GB']]
]
Fictional query:
SELECT name, location.country FROM {input} WHERE age > 18 ORDER BY age DESC
Which would produce a variation of:
[
['name' => 'Tom', 'location.country' => 'GB'],
['name' => 'Tom', 'location.country' => 'GB']
]
Note, I am perfectly aware of array_filter and alike implementations that I could put together on the spot. I am looking for query like interface to access data.
You can use LINQ, with PHP implementations such as phpLinq or LINQ for PHP or plinq
I found the following to be the closest of all available alternatives:
https://github.com/braincrafted/arrayquery
ArrayQuery does not provide query language, though it does provide query API, e.g.
$thorinsCompany = [
[ 'name' => 'Bilbo Baggins', 'race' => 'Hobbit' ],
[ 'name' => 'Gandalf', 'race' => 'Wizard' ],
[ 'name' => 'Thorin Oakenshild', 'race' => 'Dwarf' ],
[ 'name' => 'Balin', 'race' => 'Dwarf'],
[ 'name' => 'Bifur', 'race' => 'Dwarf'],
// ...
];
$query->from($thorinsCompany);
$query->where('race', 'Dwarf')
$query->where('age', 50, '>');
$results = $query->findAll();
On a related note, Mongo PHP extension provides SQL to Mongo API translation, http://www.php.net/manual/en/mongo.sqltomongo.php. The basic logic could be used as a guiding example for someone who set their foot on developing SQL interface to PHP arrays.
I suppose the reason why there aren't any actively used SQL-like query languages for arrays in PHP is performance. Using something like ArrayQuery (which I am the author of) already results in a performance penalty. A query language would result in further decreased performance which makes most of the time no sense.
ArrayQuery was also inspired by Doctrines QueryBuilder, which is, a way to transform SQL into system based on objects that is more natural in a OOP language like PHP.

Class function as array value

i want to place an class function as array value, but it shows parsing error:
Linter error message - Expecting `']"
Php error message -
ErrorException [ Parsing Error ]: syntax error, unexpected '['
Example
'name' => [
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
...
As you see, i want the label to be the value returned from the "lang" class "get" function.
Array short syntax [i.e. $a = ['a', 'b']] is available from version 5.4 on.
Be sure to have the right PHP version to use it, otherwise you should stick to old array('a', 'b') syntax.
You have a syntax error on key name line.
Try syntax like this:
$array = array(
'name' => 'val',
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
'array' => array(
...
)
);
And be sure that Lang::get('site.general.name') return a value.
Change [ to array( like this:
'name' => array(
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
[] for defining arrays isn't supported in older versions.

understanding ElasticSearch routing

I am trying to use the elasticsearch routing mapping to speed up some queries, but I am not getting the expected result set (not worried about the query performance just yet)
I am using Elastic to set up my mapping:
$index->create(array('number_of_shards' => 4,
'number_of_replicas' => 1,
'mappings'=>array("country"=>array("_routing"=>array("path"=>"countrycode"))),
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
),
'searchAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
)
)
) ), true);
If I understand correctly, what should happen is that each result should now have a field called "countrycode" with the value of "country" in it.
The results of _mapping look like this:
{"postcode":
{"postcode":
{"properties":
{
"area1":{"type":"string"},
"area2":{"type":"string"},
"city":{"type":"string",
"include_in_all":true},
"country":{"type":"string"},
"country_iso":{"type":"string"},
"country_name":{"type":"string"},
"id":{"type":"string"},
"lat":{"type":"string"},
"lng":{"type":"string"},
"location":{"type":"geo_point"},
"region1":{"type":"string"},
"region2":{"type":"string"},
"region3":{"type":"string"},
"region4":{"type":"string"},
"state_abr":{"type":"string"},
"zip":{"type":"string","include_in_all":true}}},
"country":{
"_routing":{"path":"countrycode"},
"properties":{}
}
}
}
Once all the data is in the index if I run this command:
http://localhost:9200/postcode/_search?pretty=true&q=country:au
it responds with 15740 total items
what I was expecting is that if I run the query like this:
http://localhost:9200/postcode/_search?routing=au&pretty=true
Then I was expecting it to respond with 15740 results
instead it returns 120617 results, which includes results where country is != au
I did note that the number of shards in the results went from 4 to 1, so something is working.
I was expecting that in the result set there would be an item called "countrycode" (from the rounting mapping) which there isn't
So I thought at this point that my understand of routing was wrong. Perhaps all the routing does is tell it which shard to look in but not what to look for? in other words if other country codes happen to also land in that particular shard, the way those queries are written will just bring back all records in that shard?
So I tried the query again, this time adding some info to it.
http://localhost:9200/postcode/_search?routing=AU&pretty=true&q=country:AU
I thought by doing this it would force the query into giving me just the AU place names, but this time it gave me only 3936 results
So I Am not quite sure what I have done wrong, the examples I have read show the queries changing from needing a filter, to just using match_all{} which I would have thought would only being back ones matching the au country code.
Thanks for your help in getting this to work correctly.
Almost have this working, it now gives me the correct number of results in a single shard, however the create index is not working quite right, it ignores my number_of_shards setting, and possibly other ones too
$index = $client->getIndex($indexname);
$index->create(array('mappings'=>array("$indexname"=>array("_routing"=>array("required"=>true))),'number_of_shards' => 6,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
),
'searchAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
)
)
) ), true);
I can at least help you with more info on where to look:
http://localhost:9200/postcode/_search?routing=au&pretty=true
That query does indeed translate into "give me all documents on the shard where documents for country:AU should be sent."
Routing is just that, routing ... it doesn't filter your results for you.
Also i noticed you're mixing your "au"s and your "AU"s .. that might mix things up too.
You should try setting required on your routing element to true, to make sure that your documents are actually stored with routing information when being indexed.
Actually to make sure your documents are indexed with proper routing explicitly set the route to lowercase(countrycode) when indexing documents. See if that helps any.
For more information try reading this blog post:
http://www.elasticsearch.org/blog/customizing-your-document-routing/
Hope this helps :)

Categories