I have an object with a property:
protected $recipients = array();
In one of my methods I set the contents of the $recipients property with an associative array of data using this method:
public function setRecipientData($recipientData){
foreach($recipientData as $key => $value){
$this->recipients[$key] = $value;
}
}
When I dump the contents of $this->recipients, the array ends up looking like this:
array (size=2)
0 =>
array (size=6)
'email' => string 'info#mycompany.com' (length=29)
'userEmail' => string 'xxx#yyy.com' (length=11)
'first_name' => string 'Test' (length=4)
'last_name' => string 'User' (length=4)
'jobTitle' => string 'Customer User' (length=13)
'phoneNumber' => string '123.456.7890' (length=12)
I also have a property that will extract only the email addresses from the property like this:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
$emailPair = array('email' => $recipient['userEmail']);
$emails[] = $emailPair;
}
return $emails;
}
The problem that I'm running into is that when I run the method getRecipientsEmails() I get the error:
Undefined index: userEmail
I don't understand why it's not seeing the index 'userEmail'. If I dump the nested array it shows a value:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
dd($emailPair = array('email' => $recipient['userEmail']));
$emails[] = $emailPair;
}
return $emails;
}
Result:
array (size=1)
'email' => string 'xxx#yyy.com' (length=20)
I've tried dumping the autoload with composer but it makes no difference.
Can anyone help point me in the right direction??
EDIT: Sorry, I realized that I wrote the wrong method into my original post. I've added the original getRecipientEmails() method and the dump that shows the data.
Related
I've got told many times, if there is a new question even on the same code to just create a new thread so here I am. Thanks to the guys for helping me with the previous question.
I have the following code:
/* Return an array of _octopus_ids */
$offices = array_map(
function($post) {
return array(
'id' => get_post_meta($post->ID, '_octopus_id', true),
);
},
$query->posts
);
/* Dump out all the multi-dimensional arrays */
var_dump($offices);
$test = array_column($offices, 'id');
var_dump($test);
var_dump($offices) dumps the following:
array (size=10)
0 =>
array (size=1)
'id' => string '1382' (length=4)
1 =>
array (size=1)
'id' => string '1330' (length=4)
var_dump($test) dumps the following:
array (size=10)
0 => string '1382' (length=4)
1 => string '1330' (length=4)
Problem:
How can I use the following code:
$results = $octopus->get_all('employees/' . $test; which results in an Notice: Array to string conversion error.
I want to be able to make a results call such as this $results = $octopus->get_all('employees/1382'); - So I want just the numeric string of $test to be appended to the end of employees/
If I hardcode the 1382 after employees/, I get the following result:
object(stdClass)[1325]
public 'id' => int 1382
What's the proper way to array of strings into just strings?
I am fetching json data from a table called properties. Column name is attr and it has size,bedrooms and prop-type in it.
$q= mysqli_query($connect,"SELECT * FROM properties");
$savemyval = array();
while($row= mysqli_fetch_assoc($q)){
$data = json_decode($row['attr']);
//var_dump($data);
if($proptpe == $data->proptype){
$savemyval[] = $row['id'];
}
}
Querying data like above if i var_dump this is what i get
object(stdClass)[3]
public 'bedrooms' => string '5' (length=1)
public 'proptype' => string 'residential' (length=11)
object(stdClass)[4]
public 'bedrooms' => string '4' (length=1)
public 'proptype' => string 'commercial' (length=10)
object(stdClass)[3]
public 'size' => string '16000' (length=5)
public 'prop-type' => string 'commercial' (length=10)
in var_dump i get proper data but when i try to get proprtype, if its more than 1 it gives me the error
PHP : Notice: Undefined property: stdClass::
if I use isset then there is no error but still it prints one result while dumping gives me more than 1 results
Because in your array you doesn't have proptype on second index
try like this
if(isset( $data['proptype']) && $proptpe == $data->proptype){
$savemyval[] = $row['id'];
}
Hope you guys can help me... Still new at PHP and I am struggling to display parts of this Object/Array set of Results.
I am getting the following result $results back from a SOAP webservice:
`object(stdClass)[9]
public 'Summary' =>
object(stdClass)[2]
public 'ID' => string '1096408402' (length=10)
public 'IKey' => string '1440010962' (length=10)
public 'Address' =>
object(stdClass)[4]
public 'Forename' => string 'TEST' (length=4)
public 'Surname' => string 'TESTER' (length=6)
public 'DOB' => string '0000-00-00' (length=10)
public 'Telephone' => string 'Unavailable' (length=11)
public 'Occupants' =>
array (size=3)
0 =>
object(stdClass)[12]
...
1 =>
object(stdClass)[13]
...
2 =>
object(stdClass)[14]
...
3 =>
object(stdClass)[15]
...
Now I am attempting to put the data into a table format.
I have been successful in creating the table using a foreach on the section marked Occupants. I do this by calling Occupants as follows:
$occupants = ($results->Address->Occupants); and the data is extracted and populated into my table using my code (not relevent for this question).
My problem now is that when I try and do the same for Summary or Address it doesnt work: I get the error "Trying to get property of non-object"
I have tried $summary = $results->Summary and $summary = $results['Summary'] and neither works.
What I then want to do is run
<?php $summary = ($results->Summary);foreach($summary as $person):?>
and then I insert it into my table as follows:
<td><?=$person->ID?></td>
So any idea why I get this error? I dont think it is in the foreach aspect...?
Normally, you should get the "Summary" object with:
$summary = $results->Summary
in this case $summary is an object with 2 properties: "ID" and "IKey".
If you iterate over $summary with foreach, the value of $person would have the value of $summary->ID in the first loop iteration and the value of $summary->IKey in the second loop iteration. Both $summary->ID and $summary->IKey are strings and therefore non-objects, so I think that is why you get the error.
I suppose that you want do do this:
$summary = $results->Summary;
foreach ($summary as $value)
echo "<td>$value</td>";
This should output (for the given example):
<td>1096408402</td><td>1440010962</td>
For more information about Object Iteration, I recommend: http://php.net/manual/en/language.oop5.iterations.php
I am trying to parse out certain things within the JSON code, but the problem is that the two groups of arrays that have the information in it I need have random names, here is from the var_dump:
array (size=2)
'results' =>
array (size=1)
0 => string 'Phone.5d5b6fef-a2e0-4b08-cfe3-bc7128b776c3.Durable' (length=50)
'dictionary' =>
array (size=3)
'Person.51f28c76-2993-42d3-8d65-4ea0a66c5e16.Ephemeral' =>
array (size=8)
'id' =>
array (size=5)
...
'type' => null
'names' =>
array (size=1)
...
'age_range' => null
'locations' => null
'phones' =>
array (size=1)
...
'best_name' => string 'John Smith' (length=15)
'best_location' => null
'Location.28dc9041-a0ee-4613-a3b0-65839aa461da.Durable' =>
array (size=30)
'id' =>
array (size=5)
...
'type' => string 'ZipPlus4' (length=8)
'valid_for' => null
'legal_entities_at' => null
'city' => string 'City' (length=8)
'postal_code' => string '12345' (length=5)
'zip4' => string '4812' (length=4)
'state_code' => string 'MO' (length=2)
'country_code' => string 'US' (length=2)
'address' => string 'Main St, City, MO 12345-4812' (length=33)
'house' => null
No I am trying to get best_name from under the part that starts with Person and address under Location. But when I do:
$string = file_get_contents($url);
$json=json_decode($string,true);
var_dump($json);
echo $json['dictionary']['Person']['best_name'];
I get Undefined index: Person error, because the actual object name for Person is:
Person.51f28c76-2993-42d3-8d65-4ea0a66c5e16.Ephemeral which is different every time I do a search. Is there a way to do this without putting the random generated line in?
Hopefully this makes sense, thanks for the help in advance!
If the Person key always starts with the string "Person", than simply do a foreach and check the key which contains this string.
Like:
foreach ($json['dictionary'] as $key => $value) {
if (preg_match('/^Person/', $key)) {
echo $json['dictionary'][$key]['best_name'];
}
}
Of course this get complicated, if you have multiple keys which start with "Person".
You can do the same with "Location" or any other string you need.
How about something like this ... loop through $json['dictionary']'s index keys to find something that starts with "Person".
$foundIt = false;
foreach (array_keys($json['dictionary']) as $key) {
if (substr($key,0,6) == 'Person') {
$foundIt = $key;
break;
}
}
if ($foundIt) { echo $json['dictionary'][$foundIt]['best_name']; }
Ok, the idea here is to have code that will append SF_ to all key names in an array.
I took my array (which is part of an object), flipped it, added the SF_, and flipped it back.
Somewhere in the process I lost some fields...
here's what I started with:
object(stdClass)[12]
public 'Affiliate_Code__c' => string 'XX-TXUJC3' (length=9)
public 'AltEmail__c' => string 'benny#oxpublishing.com' (length=22)
public 'City' => string 'Mobile' (length=6)
public 'Email' => string 'benny#oxpublishing.com' (length=22)
public 'Fax__c' => string '251-300-1234' (length=12)
public 'FirstName' => string 'Benny' (length=5)
public 'LastName' => string 'Butler' (length=6)
public 'Phone' => string '251-300-3530' (length=12)
public 'PostalCode' => string '36606' (length=5)
public 'State' => string 'AL' (length=2)
public 'Street' => string '851 E I-65 Service Rd' (length=21)
public 'test1__c' => float 1
array
'SF_Affiliate_Code__c' => string 'XX-TXUJC3' (length=9)
'SF_Email' => string 'benny#oxpublishing.com' (length=22)
'SF_City' => string 'Mobile' (length=6)
'SF_Fax__c' => string '251-300-1234' (length=12)
'SF_FirstName' => string 'Benny' (length=5)
'SF_LastName' => string 'Butler' (length=6)
'SF_Phone' => string '251-300-3530' (length=12)
'SF_PostalCode' => int 36606
'SF_State' => string 'AL' (length=2)
'SF_Street' => string '851 E I-65 Service Rd' (length=21)
And here's my code:
$response = $mySforceConnection->query(($query));
foreach ($response->records as $SF) {
}
var_dump($SF);
$SF = array_flip($SF);
foreach ($SF as $key => $value){
$SF[$key] = 'SF_'.$value;
}
$SF = array_flip($SF);
echo "<pre>";
var_dump($SF);
echo "</pre>";
extract($SF);
Any idea?
I'm new to OO programming of any sort, and I'm sure this has something to do with it.
I'm so stupid I have to do:
foreach ($response->records as $SF) { }
because I don't know how to get to that array any other way.
Help!
Thanks!
When you do the flip, you end up with duplicate keys - the values become the keys, and your values are not unique (e.g. Email and AltEmail__c both have the same value).
Rather than doing a flip then flipping back, just create a new array and copy the values in with the new keys:
$SF_new = array();
foreach($SF as $key => $value ) {
$SF_new['SF_' . $key] = $value;
}
// And if you want to continue using the $SF name...
$SF = $SF_new;
array_flip will flip the values and keys, as you said. A PHP array cannot have multiple keys with the same name. Try something like this to avoid flipping:
<?php
$SF = array();
foreach($response->records as $key => $value)
{
$SF['SF_' . $key] = $value;
}
About the way you get to the array in the object, this is the proper way to do it.
$SF = get_object_vars($response);
Will transform your object into an array.
flip swaps keys and values. because you have values that share the same value, you lose them in the flip.