开发者

PHP Array values - I need to know if they are definitely empty

Here is my example array $postcodeSuppliers:

Array
(
    [0] => AB123
    [postcode] => AB123
    [1] => TEST
    [supplier_1] => TEST
    [2] =>  
    [supplier_2] =>  
    [3] =>  
    [supplier_3] =>  
)

I have been trying to confirm whether a supplier is empty. Here is the code I have been using for this:

function generateQuoteSuppliers($postcodeSuppliers) {

    $quoteSupplier = array("supplier_1", "supplier_2", "supplier_3");
    //print("<pre>");
    //print_r($postcodeSuppliers);
    //print("</pre>");

    for ($i = 1; $i < 4; $i++) {
        $supplier = $postcodeSuppliers['supplier_' . $i . ''];
        //if ($supplier == '')
        //if (!isset($supplier))
        if (empty($supplier)) {
            //A fake supplier is added here 'FAKE' if any of the 3 suppliers contain no date
            echo "NO SUPPLIER";
            $quoteSupplier['supplier_' . $i . ''] = array
                (
                'supplier' => 'FAKE',
                'price' => 0
            );
        } else {
            $quoteSupplier['supplier_' . $i . ''] = array
                (
                'supplier' => $postcodeSuppliers['supplier_' . $i . ''],
                'price' => 0
            );
        }
    }

    return $quoteSupplier;
}

None of the methods I have been using to check if the value is empty are working. I get this:

Array
(
    [0] => supplier_1
    [1] => supplier_2
    [2] => supplier_3
    [supplier_1] => Array
        (
            [supplier] => TEST
            [price] => 0
        )

    [supplier_2] => Array
        (
            [supplier] =>  
            [price] => 0
        )

    [supplier_3] => Array
        (
            [supplier] =>  
            [price] => 0
        )

)

When I am expecting this:

Array
(
    [0] => supplier_1
    [1] => supplier_2
    [2] => supplier_3
    [supplier_1] => Array
        (
            [supplier] => TEST
            [price] => 0
        )

    [supplier_2] => Array
        (
            [supplier] =开发者_JAVA百科> FAKE
            [price] => 0
        )

    [supplier_3] => Array
        (
            [supplier] => FAKE 
            [price] => 0
        )

)

Can someone show me where I am going wrong please? I am completely open to the fact I am wrong! or using my array incorrectly.


You can use the language constructor isset()


try using trim() before passing $supplier to empty()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜