如何检查数组对象和数组?

时间:2022-08-01 07:01:17

First array:

第一个数组:

[VariationSpecificsSet] => SimpleXMLElement Object
            (
                [NameValueList] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [Name] => Size
                                [Value] => Array
                                    (
                                        [0] => 5FT King Size
                                        [1] => 4FT6 Double
                                    )

                            )

                        [1] => SimpleXMLElement Object
                            (
                                [Name] => Main Colour
                                [Value] => Array
                                    (
                                        [0] => Brown
                                        [1] => Black
                                    )
                            )
                     )
          )

Second Array:

第二个数组:

[Variation] => SimpleXMLElement Object
            (
                [StartPrice] => 14.99
                [Quantity] => 12
                [VariationSpecifics] => SimpleXMLElement Object
                    (
                        [NameValueList] => SimpleXMLElement Object
                            (
                                [Name] => Size
                                [Value] => No.10-1M
                            )
                    )
        )

examine above two arrays

检查以上两个数组

i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array

我想在数据库中存储值NameValueList,但问题是,它有时是SimpleXMLElement对象,有时是数组

how can i store them ...??

我怎么能把它们…?

2 个解决方案

#1


0  

You can detect is by is_array().

可以通过is_array()检测is。

$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
    foreach($myVal as $item){
        echo $item->Name.":".echo $item->Value;
    }
} else {
    echo $myVal->Name.":".echo $myVal->Value;
}

#2


0  

Did you tried using json_encode like below. You can convert the object to array.

你试过像下面这样使用json_encode吗?可以将对象转换为数组。

$array=json_decode(json_encode($object),true);

#1


0  

You can detect is by is_array().

可以通过is_array()检测is。

$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
    foreach($myVal as $item){
        echo $item->Name.":".echo $item->Value;
    }
} else {
    echo $myVal->Name.":".echo $myVal->Value;
}

#2


0  

Did you tried using json_encode like below. You can convert the object to array.

你试过像下面这样使用json_encode吗?可以将对象转换为数组。

$array=json_decode(json_encode($object),true);