I have the following call I do to Wiki to retrieve info about places, which users search for via an input called 'location'
// get wiki info about search term
$wiki_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=' . urlencode($_GET['location']);
EXAMPLE: https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=hungary
// get result of Wiki Search
$wiki_json = file_get_contents($wiki_url);
// decode data to use in php
$wiki_array = json_decode($wiki_json, true);
I've tried to get the text the same way I've done for other APIs but its not working because it seems I need the pageId to access the array where the text sits.
Is there a way to bypass this and get the text without knowing the pageID?
There are various possible solutions to this.
You could use reset() / current() against the pages property to get the first / current item in that array, or you could loop around that property with a foreach and ignore the keys. You could also use array_values() on the pages property to get force sequential indicies, or use array_keys() on it to get a list of the page ids and use those to access each item. (There are other ways).
The foreach option is going to be your best bet.
foreach($wiki_array['query']['pages'] as $page)
$page inside the loop will be the array that you're after.
You should then make sure you can deal with multiple results properly.
Related
I have to filter portions of a JSON, there are different nodes/sub-nodes and I really need only a portion, for example this is what it may look like:
"node1":{
"686":{
"value1":"686",
"value2":"M",
"value3":0
}
"687":{
"value1":"687",
"value2":"L",
"value3":1
}
"688":{
"value1":"688",
"value2":"M",
"value3":0
}
For example I need to extract the node1 nodes, specifically ["686","687","688"] what I have tried so far:
$node1values = array_keys((array)$myjson->node1)
The response is weird: * items consider that I never use PHP regularly, no idea how to get this sorted without having to write a procedure to iterate every possible node.
Use array_column() to get a particular property from each array element.
$node1values = array_column((array)$myjson->node1, 'value1');
DEMO
I am currently using PHP to make a chatbot that can do research on wiki,and send the introduction automaitcally, here is the wiki api json(https://zh.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=search_title)
{"batchcomplete":"","query":{"redirects":[{"from":"search_title","to":"search_title"}],"pages":{"541":{"pageid":541,"ns":0,"title":"search_title","extract":"ablablablablablablablablablablabla。"}}}}
I want to get the extract part by
$data=$jsondata['query']['pages']["541"]['extract'];
but it seems like I need to have to know pageid is '541' first, is there method that I can know padeid in first??
You should look for a way to iterate over array values without knowing the keys. I'm not a PHP developer but I think a foreach will do:
foreach ($jsondata['query']['pages'] as $pageid => $pagedata) {
extract = $pagedata['extract']
...
}
There is also a formatversion=2 parameter for the query that, among other things, will make
action=query's "pages" be an array, instead of an object with page ids
as keys that can be difficult to iterate.
So i am using this PHP code to create the json output, and I am having an issue where it’s creating an array of array with the info. I would like to get rid of one array and just display the list of API’s thats been used and number that has been used.
Looks as though the difference is you have...
"apis":[{"item_search":"0\n"},{"item_recommended":"0\n"}]
and want
"apis":{"item_search":"0\n","item_recommended":"0\n"}
If this is the case, you need to change the way you build the data from just adding new objects each time to setting the key values directly in the array...
$zone_1 = [];
foreach($zone_1_apis as $api_name ) {
$zone_1[substr($api_name, 0,-5)] = file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name);
}
You also need to do the same for $zone_2 as well.
It may also be good to use trim() round some of the values as they also seem to contain \n characters, so perhaps...
trim(file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name))
I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!
I am trying to restrict content within a Wordpress template file and am using a plugin called Paid Memberships Pro to do so.
The code below restricts content to members with 'levels' of 1 or 2.
if(pmPro_hasMembershipLevel(array(1,2))){
restricted content goes here
}
The problem comes when I try to use a variable to provide the levels. These levels are held in a custom field group 'restrictions' with field name 'pmpro_id'. I access these levels within the template using...
foreach($restrictions as $restriction){
$temp=get_field('pmpro_id', $restriction->ID );
$temp_array[]=$temp;
}
$levels=implode(',', $temp_array);
If I then pass $levels to pmPro_hasMembershipLevel, this works fine if there is only one level but fails if there are 2 or more. I believe this is because the variable type is then a string rather than integer? I had previously tried to pass the $temp_array directly though I felt this wouldn't work and was correct.
I realise this is probably PHP 101. I have searched but don't really know what I'm looking for to be honest! I am not a developer and this is the last thing holding me back from finishing this project so ANY help anyone could provide would be brilliant. Thanks in advance.
You don't need to implode $temp_array if pmPro_hasMembershipLevel accepts array as its argument. When you implode an array you get string as a return value — that's not what you want here. If you think that the issue might be with the type of values, then you can try to cast them to integers, like this $temp_array[]= (int) $temp;