Reputation: 842
I have a database that I am storing variable locations in. For instance, I have an array of leads, so I have a field called ID in the database, with the location of $lead['@attributes']['Id']
. That is all set in the database.
The issue I am having is retrieving the value from the array I have.
What I have tried currently is something like this:
foreach($t AS $lead) {
$newItem = [];
foreach ($this->crm->fields AS $field) {
$newItem[$field->name] = ${$field->location};
}
}
This isn't working though. It gives me an error saying:
Undefined variable: $lead['@attributes']['Id']
BUT, if I do echo $lead['@attributes']['Id']; die();
, it puts the ID on the page. What am I doing wrong?
TLDR;
I need to create dynamic variables that I store in a database to get information from an array.
Examples
This is the array I am trying to pull from:
array(6) {
["@attributes"]=>
array(10) {
["Id"]=>
string(7) "1086984"
["LeadTitle"]=>
string(7) "1086984"
["CreateDate"]=>
string(19) "03/11/2015 02:00:00"
["ModifyDate"]=>
string(19) "04/13/2017 13:02:57"
["ActionCount"]=>
string(2) "13"
["LogCount"]=>
string(2) "13"
["ReminderCount"]=>
string(1) "1"
["ReadOnly"]=>
string(5) "false"
["Flagged"]=>
string(5) "false"
["LastDistributionDate"]=>
string(19) "01/01/0001 00:00:00"
}
}
The database I have has the following in it:
ID | Name | Location
1 | ID | $lead['@attributes']['Id']
In the end, the array I get should look as follows:
array(10) {
["ID"]=>
string(7) "1086984"
}
I am getting an XML file from a platform, via cURL. I am taking that XML file, running it through simplexml_load_string()
, then json_encode()
and json_decode()
, giving me an array in the end.
I then also have a database, with a list of field names, and the locations in the array I created from the XML file. I am then cycling through the database of fields, and saving the XML file, in a better parsed way, to the database. I do this once every 5 minutes on the updated information. I am not the one generating the XML file, nor do I normally save information in the way that I am having to. I understand this probably isn't the best way to do it, but it is for internal use only, no one outside my company even is going to have access to the website we are making.
I RAN INTO ANOTHER ROADBLOCK. They have a item called "fields", with an array of fields in it.
The problem is, everything is an "attribute" on them. They are, for example, address, email, first name, last name, etc. How would I cycle through that to get the correct data? Any ideas?
This issue also exists with 3 other objects. Actions and Logs, lots of logs...
Upvotes: 1
Views: 140
Reputation: 562563
To avoid use of eval(), you could store your data in your database like this:
ID | Name | Location
1 | ID | @attributes.Id
The data in your database doesn't need to know the name of your PHP variable.
foreach($t AS $lead) {
$newItem = [];
foreach ($this->crm->fields AS $field) {
list($f1, $f2) = explode('.', $field->location);
$newItem[$field->name] = $lead[$f1][$f2];
}
}
Re your update:
I honestly would not use the database to encode your field mappings. I'd define the field mappings in your PHP script.
You'll have to define special-case loops for your Fields, Actions, and Logs.
I don't envy you. I have done apps like this where I had to accept data from another department, formatted in some ill-defined way. In my case, I was sent an Excel spreadsheet, and I wrote code to parse it and get the data out of it that I needed. But the department who send the spreadsheet changed the format every week, so I had to recode my parsing routine. I tried telling them, "decide on the format for the spreadsheet and keep it the same every week." They ignored my request and continued making it differently every time. Then my contract was up and I left, knowing they would break it the week after I left.
Upvotes: 1