Im trying to search a array and navigate to the next and previous values
$ids=$res->result_array();
returns
array(3) {
[0]=>
array(1) {
["qid"]=>
string(5) "63697"
}
[1]=>
array(1) {
["qid"]=>
string(5) "63706"
}
[2]=>
array(1) {
["qid"]=>
string(5) "63709"
}
}
but when i try to search for the index it returns false
$curr_index = array_search($this->uri->segment(4), $q);
returns
bool(false)
$this->uri->segment(4) is the qid.
i want to navigate with the array by increasing and decreasing by one so i can get the next and previous values.
can someone please tell what am i doing wrong here?
You have an array of arrays, you could search it like this:
$curr_index = array_search(array('qid' => $this->uri->segment(4)), $q);
Where you are actually searching for an array instead of a string.
Working example: http://codepad.viper-7.com/Ff0sAq
Related
I have a dump of data :
var_dump($steps);
and results are :
object(Drupal\form\Manager\StepManager)#490 (1) {
["steps":protected]=>
array(1) {
[1]=>
object(Drupal\form\Step\StepOne)#494 (2) {
["step":protected]=>
int(4)
["values":protected]=>
array(1) {
["key"]=>
string(3) "123"
}
}
but I tried using :
$steps[1]->values->key but its having an error, its not available directly?
where did I miss?
The index to the first item – and only item in your case – is 0, and not 1. So this should work:
$steps[0]->...
The number 1 in the var_dump shows the size of the array:
["steps":protected]=>
array(1) { // This array contains one item
Your $steps array with in a object of Drupal\form\Manager\StepManager
and you accessed key from values object but your output shows you have a values array not object.
array(1) {
["key"]=>
string(3) "123"
}
So try this in var_dump() I think its worked
var_dump($steps->steps[1]->values["key"]);
I'm currently getting a JSON response from a company's API and converting it into a PHP array like this:
$api_url = file_get_contents('http://example.com');
$api_details = json_decode($api_url, true);
When I run var_dump on $api_details, I am getting this:
array(2) {
["metadata"]=>
array(5) {
["iserror"]=>
string(5) "false"
["responsetime"]=>
string(5) "0.00s"
["start"]=>
int(1)
["count"]=>
int(99999)
}
["results"]=>
array(3) {
["first"]=>
int(1)
["result"]=>
array(2) {
[0]=>
array(4) {
["total_visitors"]=>
string(4) "3346"
["visitors"]=>
string(4) "3249"
["rpm"]=>
string(4) "0.07"
["revenue"]=>
string(6) "0.2381"
}
[1]=>
array(4) {
["total_visitors"]=>
string(6) "861809"
["visitors"]=>
string(6) "470581"
["rpm"]=>
string(4) "0.02"
["revenue"]=>
string(7) "13.8072"
}
}
}
}
I'm trying to do 2 things and can't figure out how to do either with a multidimensional array.
I need to check to see if metadata > iserror is false. If it is not false, I want to show an error message and not continue with the script.
If it is false, then I wants to loop through the results of results > result and echo the total_visitors, visitors, etc for each of them.
I know how to echo data from array, I guess I'm just getting confused when there's multiple levels to the array.
Anyone that can point me in the right direction would be much appreciated :)
You can iterate over arrays using foreach. You can read up on it here: http://php.net/manual/en/control-structures.foreach.php
Since you're using associative arrays, your code will look something like this:
if ($arr['metadata']['iserror']) {
// Display error here
} else {
foreach($arr['results']['result'] as $result) {
echo $result['total_visitors'];
echo $result['visitors'];
}
}
You'll have to tweak the code to fit exactly what you're doing, but this should get you over the line.
Hope that helps!
So I have two arrays which looks like this when I do a var_dump :
array(4) {
["DatabinFieldName_1"]=> string(7) "Heading"
["DatabinFieldType_1"]=> string(13) "VARCHAR (255)"
["DatabinFieldName_3"]=> string(11) "DateCreated"
["DatabinFieldType_3"]=> string(8) "DATETIME"
}
array(8) {
["DatabinFieldName_1"]=> string(7) "Heading"
["DatabinFieldType_1"]=> string(13) "VARCHAR (255)"
["DatabinFieldName_2"]=> string(4) "Copy"
["DatabinFieldType_2"]=> string(4) "TEXT"
["DatabinFieldName_3"]=> string(11) "DateCreated"
["DatabinFieldType_3"]=> string(8) "DATETIME"
["DatabinFieldName_4"]=> string(8) "Comments"
["DatabinFieldType_4"]=> string(4) "TEXT"
}
I need to get the difference in a result. Which I have tried using this code.
// Get POST Array
$databinPostArray = $_POST;
// Get Databin Array
$databinObject =json_decode($nbase->getwhere("Databins","ID='".$databinID."' LIMIT 1;",$_SESSION["UserDB"]));
$databinArray= unserialize($databinObject[0]->DatabinArray);
var_dump($databinPostArray);
var_dump($databinArray);
$result = array_diff($databinPostArray, $databinArray);
print_r($result);
Problem is I keep getting Array() back which means its not finding any differences even though there is.
array_diff() returns the elements of the second array which are not in the first one.So the answer to your question is:
$result = array_diff($databinPostArray, $databinArray);
if (couunt($result) == 0) {
$result = array_diff($databinArray, $databinPostArray);
}
This way, the difference will be returned, whether there is more keys in $databinPostArray or in $databinArray.
If what you want is only to check which elements are in $databinArray, but not in $databinPostArray, please do:
$result = array_diff($databinArray, $databinPostArray);
You need to reverse the arguments:
$result = array_diff($databinArray, $databinPostArray);
array_diff returns an array with everything in the first array that isn't in the second array.
If you want to get all the elements that are unique to either array, you can use:
$result = array_diff(array_unique(array_merge($databinArray, $databinPostArray)),
array_intersect($databinArray, $databinPostArray));
So I need to modify the array in a memcached key-value pair. I need to remove one of the arrays inside the array. An example of what it looks like:
array(2) { [0]=> array(3) { ["username"]=> string(3) "Bob" ["id"]=> string(5) "14537" ["comment"]=> string(4) "cool"} [1]=> array(3) { ["username"]=> string(3) "Tom" ["id"]=> string(5) "14538" ["comment"]=> string(3) "yes"}}
If I know the values of username, id, and comment, how can I delete it? The generic queston: How can I delete array 0?
Considering the answer of doing a foreach loop, I tried
foreach($memcachedarray as $f){
if ($f['id'] == '14537'){
echo key($f);
}
}
But it spits out username
Edit- Ok
I searched some more and found I need to do this:
foreach($memcachedarray as $key => $f){
if ($f['id'] == '14537'){
echo $key;
}
}
That works!
If the Id's are unique across the system then you could use an associative array to store you data then unset the key, otherwise you would want to use a foreach loop to get the array key, then unset that key and recommit your new array back into memcache.
I'm really not sure how to explain this. It's so simple I can't fathom why it's not working.
I have a loop. It puts a bunch of strings into an array. If I fill a single variable with any given string, it will output it perfectly.
But filling an array with the strings will make it give me the dreaded:
Array Array Array Array Array Array Array Array
Note: my strings are not all 'Array'.
The way I loop is:
while(...)
{
$arr[] = $resultFromLoop;
}
Here is my var_dump.
array(1) {
["tagName"]=>
string(5) "magic"
}
array(1) {
["tagName"]=>
string(4) "nunu"
}
array(1) {
["tagName"]=>
string(5) "books"
}
array(1) {
["tagName"]=>
string(0) ""
}
array(1) {
["tagName"]=>
string(3) "zzz"
}
array(1) {
["tagName"]=>
string(4) "grey"
}
array(1) {
["tagName"]=>
string(3) "new"
}
array(1) {
["tagName"]=>
string(6) "flight"
}
This is because you're working with array as with a string.
It puts a bunch of strings into an array.
Nope, there are no strings. I already gave you a magic var_dump($resultFromLoop) function, but you're too lazy to use it for debugging your code (because there is SO, where you can ask any question and don't bother yourself with thinking)