如何转换成数组之类的json对象

时间:2018-09-11 07:13:18

标签: php json

Mycode如下:

// SeatsToCancel = U23,L43,U12

      $tin='S4243534';         //Booking id

      $seatsToCancel=$_GET['seatsToCancel'];

      $seatArray=explode(",",$seatsToCancel);

      $seatCount=count($seatArray);

      $SeatsTocancel=implode ( '", "', $seatArray );

       $params = array(
                                    "tin"=>"$tin",
                                  "seatsToCancel"=>array($SeatsTocancel)
                         );

    echo $params=json_encode($params);

我想要这样的输出:

{"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}

2 个答案:

答案 0 :(得分:1)

我假设您的 const Tab1a = ({ navigation }) => (<Screen label="Tab 1a" navigation={navigation} />); const Tab1b = ({ navigation }) => (<Screen label="Tab 1b" navigation={navigation} />); const Tab1c = ({ navigation }) => (<Screen label="Tab 1c" navigation={navigation} />); const Tab2a = ({ navigation }) => (<Screen label="Tab 2a" navigation={navigation} />); const Tab2b = ({ navigation }) => (<Screen label="Tab 2b" navigation={navigation} />); const Tab2c = ({ navigation }) => (<Screen label="Tab 2c" navigation={navigation} />); // second view on drawer const Tab3a = ({ navigation }) => (<Screen label="Tab 3a" navigation={navigation} />); const Tab3b = ({ navigation }) => (<Screen label="Tab 3b" navigation={navigation} />); const Tab3c = ({ navigation }) => (<Screen label="Tab 3c" navigation={navigation} />); const Tab4a = ({ navigation }) => (<Screen label="Tab 4a" navigation={navigation} />); const Tab4b = ({ navigation }) => (<Screen label="Tab 4b" navigation={navigation} />); const Tab4c = ({ navigation }) => (<Screen label="Tab 4c" navigation={navigation} />); // first tab navigator stacks const stack1StackNavigator = StackNavigator({ A: { screen: Tab1a, }, B: { screen: Tab1b, }, C: { screen: Tab1c, }, }); const stack2StackNavigator = StackNavigator({ A: { screen: Tab2a, }, B: { screen: Tab2b, }, C: { screen: Tab2c, }, }); // second tab navigator stacks const stack3StackNavigator = StackNavigator({ A: { screen: Tab3a, }, B: { screen: Tab3b, }, C: { screen: Tab3c, }, }); const stack4StackNavigator = StackNavigator({ A: { screen: Tab4a, }, B: { screen: Tab4b, }, C: { screen: Tab4c, }, }); const tab1StackNavigator = StackNavigator({ A: { screen: stack1StackNavigator, }, B: { screen: stack2StackNavigator, }, }); const tab2StackNavigator = StackNavigator({ A: { screen: stack3StackNavigator, }, B: { screen: stack4StackNavigator, }, }); const MainNavigator = TabNavigator({ First: { screen: tab1StackNavigator, }, Second: { screen: tab2StackNavigator, }, }, { tabBarPosition: 'bottom', tabBarOptions:{ scrollEnabled: false, } }); const MainDrawerNavigator = DrawerNavigator({ Main: { screen: MainNavigator, }, Secondary: { //I'm trying to do something like this but doesn't work screen: MainNavigator.Second.A.C }, }, { initialRouteName: 'Main', headerMode: 'none', drawerPosition: 'left', contentComponent: CustomDrawerContentComponent, contentOptions: { activeTintColor: '#00a3c3', inactiveTintColor: '#00a3c3', }, drawerWidth: 300, useNativeAnimations: true, contentOptions: { itemsContainerStyle: { //The styles of the container, margin: 23 }, labelStyle: { //Get properties of the label, color, etc. color: '#00a2c3' } }, });<resultMap id="coolResultMap" type="CoolClass"> <result property="_short" column="short"/> </resultMap> <select id="getStuff" resultMap="coolResultMap"> SELECT * FROM CoolTable </select>
在这种情况下,您可以这样:

$seatArray

已编辑以添加已编辑问题中的新要求

答案 1 :(得分:0)

因此,我猜测您要取消的座位将从URL中以逗号分隔的列表形式出现。因此,您所需要做的就是explode() GET参数将其转换为数组,并将其作为keyjson_encode()的值。

$seatsToCancel = explode(',', $_GET['seatsToCancel']);
$params = [
    'tin' => $tin,
    'seatsToCancel' => $seatsToCancel,
];
$paramsJson = json_encode($params);

这应该为您带来想要的结果:

{"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}

您可能要考虑如果GET参数为空以及是否要进行其他错误检查会发生什么情况。