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
Related
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!
Say you have a method that receives an array as a parameter. That method does some manipulation with that array and then outputs a different array.
public function createArrayForX(array $myArray){
return $modifiedArray;
}
In order for me to test that the output has some values I need to create some sample cases for my input. It is a tedious work to create arrays manually, especially big ones.
public function testCreateArrayForX(){
$myArray = []; // **how do I easily create myArray here**
$this->assertContains("String that I want", $this->sampleClass->createArrayForX($myArray));
}
I tried something like var_dumping my array in the program, copying the result from var_dump and transforming it back to array (Convert var_dump of array back to array variable) and then edit it to make different tests. Is there a simple way or am I doing things wrong if I need this?
So my question is how do you easily create a skeleton for you sample input that you can then change to test different scenarios?
print(var_export($myArray, true));
in the program gave me what I need.
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.
I am converting to a PHP array from a Facebook API response in JSON format. The result is a multilevel array with many elements named id on every level.
I would like to retrieve all the elements with the name id and I was wondering if there is any direct way to get all those elements without having to parse each level of the array to grab the element.
Hope this question makes sense,
Any tip will be much appreciated.
Use Array Column function
$output = array_column($input, 'id');
If you do not have php 5.5 (i.e. no array_column for you), you may use this:
$ids = array_map(function($element) {return $element['id'];}, $input);