I am encountering a small problem and I was just looking for some advice on best practices.
I have a script that fetches some data from an external webpage. Sometimes this data is not complete.
For example I may have
$array=array('name'=>'Bob Smith','address'=>array('0'=>'My address'),'phone'=>'000000');
I then set this data into another array to insert into my database.
E.G.
$data=array('name'=>$array['name'],'address'=>$array['address']['0'],'phone'=>$array['phone']);
If however for example no addresses were found, $array[‘address’][‘0’] is not set, thus creating a second array using it creates an error in CodeIgniter. Although in normal procedural coding no error would be shown, I assume CI has this error showing as resolving it is a best practice.
So.. I assume the best way to resolve this is to use conditionals and check if $array[‘address’][‘0’] is set before adding it to our $data array. Is that correct?
If it is not, null is inserted into the database.
The nest problem is when i get the data from the database to display it.
I want to explode the lines of the address and output them, but if it is not set again codeigniter throws up an error. Again, easily avoided by having a conditional statement.
My question is essentially within the processing, inserting and outputting of data where there are numerous variables which may or may not be set, i need to seemingly have loads and loads of conditional statements.
I suppose in reality this is not a problem, but i like my code to be a short and succinct as possible. Have I missed something?
CHeers
Do you know all the possible keys that could be returned? In that case
You can also add formatting to certain keys in the foreach-loop