Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:54:10+00:00 2026-06-11T07:54:10+00:00

I am encountering a small problem and I was just looking for some advice

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:54:12+00:00Added an answer on June 11, 2026 at 7:54 am

    Do you know all the possible keys that could be returned? In that case

    $allowed_keys = array('name', 'address');
    $new_data = array();
    foreach ($allowed_keys as $key) {
        $new_data[$key] = isset($data[$key]) ? $data[$key] : null;
    }
    

    You can also add formatting to certain keys in the foreach-loop

    if ($key == 'address') {
        $new_data[$key] = isset($data[$key]) ? implode(', ', $data[$key]) : null;
        continue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am encountering a problem with passing data between pages that require facebook access.
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
I am encountering some weird behavior when calling a shell script from a Java
I'm encountering a stupid problem which I just cannot understand. How come that following
I'm creating a system that turns a small script into a dll. I'm encountering
I've recently started looking at Merb, for use with some small projects around the
I have a small WCF hosting engine that I am writing that will dynamically
I made the following small Javascript script to enable some form elements on my
I am encountering a problem while extracting info from a database using php+mysql and
I'm encountering something I did not expect. I have a C# client that accesses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.