0

I have an array from a feed with following print_r output:

Array
(
[0] => Array
    (

        [name] => Cell Phone Case

        [options] => Array
            (
                [Colors] => Array
                    (
                        [0] => Clear
                        [1] => Red
                        [2] => Blue        
                    )
            )

Array
(
[1] => Array
    (

        [name] => Iphone
            )

Array
(
[2] => Array
    (

        [name] => Tablet

        [options] => Array
            (
                [Color] => Array
                    (
                        [0] => White
                        [1] => Black
                        [2] => Red 
                    )

            )            

The feed unfortunately is providing colors in two values, for some products it is:

options['Colors'] 

and for some other

options['Color'].

My code to insert the values into DB is as follows:

$stmt = $conn->prepare("INSERT INTO product_colors(product_id, colors) VALUES(:pid, :colors)");


if (!is_array($value['options']['Colors'])) {
$value['options']['Colors'] = array($value['options']['Colors']);
}
if (!is_array($value['options']['Color'])) {
$value['options']['Color'] = array($value['options']['Color']);
}


//Colors or Color
$colorArr = array();
if(isset($value['options']['Colors'])) {
 $colorArr = $value['options']['Colors'];
}
if(isset($value['options']['Color'])) {
$colorArr = array_merge($colorArr, $value['options']['Color']);
}

$PID = ($conn->lastInsertId());

foreach ($colorArr as $colors) {
$stmt->execute(array(':pid' => $PID, ':colors' => $colors));
}  

The data is getting inserted except that a null value is also inserted at the beginning, except for product 1 (at end ) for each product color values.

print_r($colorArr); gives the following array output:

Array
(
[0] => Clear
[1] => Red
[2] => Blue
[3] =>
)

An extra null value after the 3 colors for product 1...

Please note that some products do not have color value like the iphone in array 2.

How can i correct this issue ??? I need the product colors inserted into DB for the corresponding product id, even if options are both Colors or Color.

Help requested....

Update:

I have corrected the issue myself. The default php array_merge function doesn't merge values if any subsequent array's field is empty. This function rectifies it as follows:

function array_merge_corrected($array_a=array(),$array_b=array()){
$array_merge= array();
if(!empty($array_a)&& !empty($array_b)){
foreach($array_a as $field=>$value){
$array_merge[$field]= $value;
}
foreach($array_b as $field=>$value){
if(!empty($value)){
$array_merge[$field]= $value;
} elseif(!array_key_exists($field,$array_a)){
$array_merge[$field]= $value;
}
}
}
return $array_merge;
}


$colorArr = array_merge_corrected($colorArr, $value['options']['Color']);

3 Answers 3

1

I believe the culprit is this bit of the code:

if (!is_array($value['options']['Colors'])) {
    $value['options']['Colors'] = array($value['options']['Colors']);
}
if (!is_array($value['options']['Color'])) {
    $value['options']['Color'] = array($value['options']['Color']);
}

This creates an array if it's not already an array, including in the case when there is actually nothing. So you get an array containing a null value.

Sign up to request clarification or add additional context in comments.

1 Comment

@user3790186, definitely not the most efficient way of doing it. The good solution is to test if the value is also non-null before creating the array: if (!is_empty($value['options']['Colors']) && !is_array($value['options']['Colors']).
0

Try by changing the lines

if(isset($value['options']['Colors'])) {
    $colorArr = $value['options']['Colors'];
}

To

if(isset($value['options']['Colors']) and $value['options']['Colors'] != null and $value['options']['Colors'] !='') {
     $colorArr = $value['options']['Colors'];
}

Comments

0

it seems that the problem is in array_merge, but i guess if you check before you add the color in database it will be easier to sort. So just replace the foreach iteration to insert for that:

foreach ($colorArr as $colors) {
    if(trim(chop($colors))!='') $stmt->execute(array(':pid' => $PID, ':colors' => $colors));
} 

All in all I would change your code to be more clear:

$option=$value['options'];
// Check if values are set.
if(!isset($option['Colors'])) $option['Colors']=array();
if(!isset($option['Color'])) $option['Color']=array();

// Convert to array if dont
if(!is_array($option['Colors'])) $option['Colors']=array($option['Colors']);
if(!is_array($option['Color'])) $option['Color']=array($option['Color']);

// Finally merge
$colorArr = array_merge($option['Colors'], $option['Color']);

and so on...

Hope it helps!

2 Comments

Have you put if(trim(chop($colors))!='') before execute?
I have corrected the issue myself and posted resolution below my question.

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.