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"]);
Related
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!
My document where data is to be inserted is:
Data has to be appended to array with key as Solutions
Suppose $data has some numerical value
array(8) {
["_id"]=>
object(MongoId)#9 (1) {
["$id"]=>
string(24) "56d32613097ed36c10000049"
}
["Username"]=>
string(4) "xxx"
["Solutions"]=>
array(0) {
}
}
Suppose $j= "The entire above array"
How to push the value of $data in the array having key as "Solutions"?
I have been trying to figure this out for a while now. Please any advice would be appreciated. I just need to access the array for ["Item"]. How do I gain access to this?
array(1) {
[0]=>
object(SimpleXMLElement)#16 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(10) "AuthorList"
["Type"]=>
string(4) "List"
}
["Item"]=>
array(3) {
[0]=>
string(9) "Smith, Joe"
[1]=>
string(10) "Peter, Ann"
[2]=>
string(18) "Magoo, Mr"
}
}
}
Assuming this structure is in a variable called var1, you should be able to access Item using:
$var1[0]->Item // returns the array
I try to explain it.
Like you see, your first array index is an object.
If you see something like this in your var_dump than you can access it through deferencing the object.
It's the same like you would create an object and would like to access a public variable:
$var1 = new Object();
// when your Object variables are public so you could access them by deference the Object
echo $var1->myVariable; // will echo the public variable "myVariable"
So the answer from adam is the right one :)
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
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)