It might be the late hour or user error but I'm having trouble extracting variables from a PDO, where it would normally work. When I print_r my results by doing $array->results(), I get the following line:
Array ( [0] => stdClass Object ( [messageid] => 1 [fromid] => 2 [toid] => 1 [message] => Hello! ) )
However, on other projects when working with other people I have then just referred to the variable like this:
$result['message']
This is obviously after the line
$result = $array->results();
I've been troubleshooting and come to no conclusions, but I'm terrible at managing arrays so it could be something super simple. Any ideas? Thanks!
Since the result seems objects within array, so get your array and access object(s) inside it, to get message object from your $result, just do:
echo $result[0]->message;
Related
I have an array such as:
$var = array('hi','ho',rand(2,5));
What I would like to echo is the entire array, exactly as written.
Normally when you try a print_r, it shows as:
Array (
[0] => hi
[1] => ho
[2] => 3
)
But I want:
Array (
[0] => hi
[1] => ho
[2] => rand(2,5)
)
You can get this with file_get_contents, but is there any way to do so within the actual PHP file?
I don't think it's possible because when array is created, random value is assigned to element with index 2 and you cannot check how this value was created.
I don't think it's possible, since the rand is already evaluated as soon as you set the array to some variable.
A workaround would be the hold the expression as a string and then eval it when you need it. Like this:
$varStr = "array('hi','ho',rand(2,5))";
echo $varStr;
// when you actually need it
$var = eval($varStr);
However, this is almost never a good idea. Providing a use-case where you need this might help come up with a better solution.
I am trying to connect to a linkedin api and this is almost embarassing to ask but I just cant seem to get the syntax correct for accessing a multidimensional array. When I print out the full $information variable I get the following:
Array
(
[person] => Array
(
[id] => vlofsVJ8FM
[first-name] => Tyler
[last-name] => Slater
[interests] => Golf, Motorcycles, Technology
)
)
So when I go to try and reference the id I would normally say $information['person']['id'] but then it doesn't return anything. Sorry for the noob question but I just can't find the answer.
It looks like you have more arrays, so select the first
$information[0]['person']['id']
$information[0]['person']['element_name'];
Another way to get the data you need from this data structure can be done using the current function:
$information = current($information);
Then you can access the elements you need by using $information['id']
I'm probably missing something quite basic, but I'm getting very confused (and frustrated) with the results I get from my SOQL queries to the Salesforce API.
My query:
Select Id, FirstName, LastName FROM contact
The resulting object (as rendered by print_r):
stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPd7uAAC
[1] => 0032000000cPd7uAAC
)
[any] => BuzzAldrin
)
[1] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPt1zABC
[1] => 0032000000cPt1zABC
)
[any] => RonnieVanZant
)
[2] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPb60AA
[1] => 0032000000cPb60AA
)
[any] => PollyJeanHarvey
)
)
[size] => 3
)
The first thing I don't get is why "Id" is an array. A strange quirk, but a workaround is not too hard.
The second thing bothers me endlessly more, though: I select for FirstName and LastName and what happens is they get concatenated and returned as a single string value for a field called "any". To avoid the "split it on uppercase letters" advice I already got from my colleagues, I provided an example with both a two-capital first name and a two-capital last name, and anyhow, in reality I need many more (and more formally unpredictable) fields, and they all get added to this "any" property.
Does anyone see what I'm doing wrong? Assuming it's not such a badly written API, that is?
Edit:
Said developer will now go sit in a corner for a few hours, repenting for not having checked for more recent versions of PHP Toolkit. Seems I was using 11.0, whereas there's already a version 20.0. Shame on me, shame on me indeed. Sorry for wasting your time.
The behavior you are seeing is mostly because of how PHP's SoapClient interprets the results from the API. If you call getLastResponse() on your API connection after you make the query() calls above, you'll see what the actual SOAP messages look like coming back from Salesforce.
As far as the Id array -- its not really an array, but it is listed twice per record (once for the record itself and once as a field), but PHP turns it into an array because it sees it twice. As far as the any, that's happening because PHP is not understanding the namespaced field tags correctly.
As it looks like you found, using the PHP Toolkit can help with these oddities and return sensible objects for you to work with. You might also want to consider looking at Salesforce's REST API, whose results can be directly consumed by json_decode(). For making the HTTP calls to Salesfore, you might be interested in this simple (almost standalone) REST client in a project of mine.
I'm using cakephp and am getting back a "double array" where it is giving me 2 arrays where it should be 1, I have looked into the issue as far as cakephp and can't figure it out and just want to move past this for now so I am wondering if anyone knows how to unset a second array if a variable has 2 arrays.. below is the print_r of the array, its just one variable that has this, which I find odd.. so I want to make it so there is not a 2nd set of duplicate values, if I do an array_push it pushes both values for that index into the resulting new array index so that won't work
one variable is equal to the following:
Array ( [0] => 42 [1] => 62 ) Array ( [0] => 42 [1] => 62 )
EDIT:
This is not an issue of my printing out the array twice accidentally, as I said above, with a foreach array_push of the variable, i end up with this, which is odd:
Array ( [0] => 4242 [1] => 6262 )
EDIT:
This is the cakephp database call that I am using, I know I didn't ask this in regards to cakephp but since some people think this is impossible i am posting this just so you can see what it does if you want
$specificfields_array = $this->Mymodel->find('list', array('fields' =>'Mymodel.id'),
'conditions' => array('emailgroup' => $categorynumber, 'sent' => '0');));
EDIT:
This is what a "foreach" array_push is:
$mynewarray = array();
foreach ($specificfields as $specificfields_current) {
array_push ($mynewarray, $specificfields_current);
}
A variable cannot "have two arrays". It can be one array that has two arrays nested. The scenario you describe is impossible (probably there are two print_r there or there is a < character hiding stuff – check the HTML source).
Can you post the controller, the model and the view file with your print_r calls to the http://bin.cakephp.org/ site and post the links back here so we can see all of your code?
I'm trying to call to a specific part of an array with the key # and it's not working. I can output the array and see it...
Array
(
[6] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[7] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
[8] => Array
(
[0] => user#domain.com
[1] => user#domain.com
)
)
This array is $emailDB. I can call to the array manually with $emailDB[7] and it works, but if my call is dynamic like this it won't work...
<?php
$value = 7;
print_r($emailDB[$value]);
?>
I've never had an issue like this with an array so it's very odd. What really sucks is I'm under deadline with a form not working on a client's site...joy.
We tried this with no luck...
<?php
$value = 7;
print_r($emailDB[intval($value)]);
?>
I thought intval() would assist but it did not.
You're post implies a bug in php itself, which I highly doubt. What's more likely is that what you posted doesn't properly represent the code you're running.
Why don't try this. Make a brand new empty php file. Hardcode the array keys and values and assign them to the $emailDB variable, and then try
$value = 7;
print_r($emailDB[$value]);
You will see you don't have the problem that you claim. You have now started the debugging process, and now you can look at the working, and non working code to compare the difference.
Well, you are echoing an Array, which I assume is printing "Array" onto your screen. If you want to echo the actual contents of the array, you need to use print_r($array) or echo print_r($array, true). You can also try putting the value in quotes, like $emailDB["{$value}"] to see if that works, I sometimes have troubles with integers not going into things properly.
I agree with you all. It had to have been something whacky with how we were pulling in the data somehow. It was a tab-separated file we were exploding. I just re-wrote the whole thing entirely and imported the data into MySQL and all was well.
In hindsight, I have a sneaking suspicion it was a trim() command that was needed and likely nothing more. Dang it...too late, but I learned something about checking over the code for those types of things.