1

I'm trying to import some data from a CSV file into a WordPress plugin.

I'm having trouble when the value to be imported is '0'. Currently it converts a '0' to NULL when saved in the database. The idea is to import a sport score ie. 2-0, which then gets parsed into an array.

$score = explode( '-', $result );

$home = trim($score[0]);
$away = trim($score[1]);

$goals = array( 'total' => array( 'home' => $home, 'away' => $away) );

update_post_meta( $id, 'total_score', serialize( $goals ) );

Can anybody suggest a solution, knowing me it's probably something blindingly obvious but my brain has hit a brick wall!

Thanks in advance

2
  • are you suer its 0 and not blank Commented Feb 1, 2015 at 20:09
  • Thanks for your comment but I've checked and double checked the input and it is definitely a 0 Commented Feb 1, 2015 at 20:14

2 Answers 2

1

This should do the job:

$home = strval(trim($score[0]));
$away = strval(trim($score[1]));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion @michael but I have fixed the problem. The problem was elsewhere but your suggestion helped me get there - thanks again!
0

I have resolved the issue :)

The original code was correct but a dodgy if/else statement was preventing the value for $score parsing correctly.

The answers below, although not used, did help me slim down the possibilities, thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.