Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按数组键对字典中的数组进行排序';PHP中的s值_Php_Ios_Json - Fatal编程技术网

按数组键对字典中的数组进行排序';PHP中的s值

按数组键对字典中的数组进行排序';PHP中的s值,php,ios,json,Php,Ios,Json,这是一个分为两部分的问题,但下面是我的PHP代码,我想按天的升序(0,1,…6)对业务时间进行排序。这在PHP或iOS中更容易吗(这是为集成到iOS应用程序而编写的) 另外,sidenote,我的iOS开发人员说他在返回位置数组字典时遇到了问题,如下所示。他更希望将位置作为一个编号数组(JSON代表[{},{},…],而不是我的位置作为{},{},…),但问题是我在PHP中找不到一种方法来满足这个应用程序需求。我特别需要使用数组键将营业时间添加到相应的位置。我正在连接三个表以获取营业时间和位置i

这是一个分为两部分的问题,但下面是我的PHP代码,我想按天的升序(0,1,…6)对业务时间进行排序。这在PHP或iOS中更容易吗(这是为集成到iOS应用程序而编写的)

另外,sidenote,我的iOS开发人员说他在返回位置数组字典时遇到了问题,如下所示。他更希望将位置作为一个编号数组(JSON代表[{},{},…],而不是我的位置作为{},{},…),但问题是我在PHP中找不到一种方法来满足这个应用程序需求。我特别需要使用数组键将营业时间添加到相应的位置。我正在连接三个表以获取营业时间和位置id,以便营业时间的位置id与位置本身的位置id匹配;这似乎是让两个数组连接起来以使JSON输出数组工作的唯一方法。。。您可以看到下面的内容,但如果我错了,请告诉我,或者我的iOS开发人员是否可以更轻松地学习如何迭代并返回具有键的多维关联数组的所有数组值。请告知

if ($stmt = $dbh->prepare($query)) {
            // initialise an array for the results 
            $result = array();
            if ( $stmt->execute(array($lat,$lat,$lng,$rest_price,$eat_in,$take_out,$delivery)) ) {
                        // loop through all values
                while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
                    // Have we seen this menu before? If not, add it to the array
                    if ( !isset($result['locations'][$row['rest_id']]) ) {
                        $result['locations'][$row['rest_id']] = array(
                            'rest_id' => $row['rest_id'],
                            'user_id' => $row['user_id'],
                            'rest_name' => $row['rest_name'],
                            'lat' => $row['lat'],
                            'lng' => $row['lng'],
                            'rest_price' => $row['rest_price'],
                            'rest_rating' => $row['rest_rating'],
                            'rest_genre' => $row['rest_genre'],
                            'eat_in' => $row['eat_in'],
                            'take_out' => $row['take_out'],
                            'delivery' => $row['delivery'],
                            'rest_img' => $row['rest_img'],
                            'user_img' => $row['user_img'],
                            'business_hours' => array()
                        );
                    }
                    // Add the ingredient.
                    // remove all NULL, FALSE and Empty Strings but leave 0 (zero) values
                    $result['locations'][$row['rest_id']]['business_hours'][] = array_filter(array(
                        'day' => $row['day'],
                        'open_time' => $row['open_time'],
                        'close_time' => $row['close_time']
                    ), 'strlen');
                }

                // print results if not null
                if( $result != null ) {       
                    // print success. no error.
                    $result['error'] .= '';     
                    echo json_encode($result);
                    //print_r($result);
                } else {
                    echo json_encode(array('error' => 'No locations exist in your area'));
                }
排列

JSON

好吧,这花了很长时间(部分原因是json字符串无效,因为它有一个
,但是是w/e)

您的开发人员更喜欢这样的字典数组
{“locations”:[{},{},…],“error”:“}
,而不是这样的字典数组
{“locations”:{“67”:{},“89”:{},…},“error”:“}
,因为字典数组非常适合iOS表视图范例。然而,将字典中的字典转换为字典数组只需要一行代码,例如

NSArray *locationAllValues = [locationDictionary allValues];
所以唯一的问题是性能。您是强制服务器做更多的工作来生成首选格式,还是让移动设备做一些工作

在解析JSON数据时,我建议使用
NSJSONReadingMutableContainers
选项,以便数组和字典是可变的。这使得排序
数组更容易。是的,在iOS中对阵列进行排序很容易。下面是一整套代码,用于从输入JSON创建字典数组。代码的输入是一个
NSData
对象,其中包含从网络下载的JSON字符串。字典数组按
rest\u id
排序,在每个字典中,
business\u hours
数组按
day
排序

请注意,除了在调用
JSONObjectWithData
后检查
nil
之外,代码没有错误检查。这只是概念证明,而不是生产代码。使用风险自负

- (NSArray *)parseAndSortJsonResponse:(NSData *)data
{
    NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if ( !jsonData )
    {
        NSLog( @"Invalid JSON string" );
        return( nil );
    }

    NSMutableDictionary *locationDictionary = jsonData[@"locations"];
    NSArray *locationAllValues = [locationDictionary allValues];

    NSArray *locationsArray = [locationAllValues sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
    {
        NSDictionary *d1 = obj1;
        NSDictionary *d2 = obj2;

        int v1 = [d1[@"rest_id"] intValue];
        int v2 = [d2[@"rest_id"] intValue];

        if ( v1 < v2 )
            return( NSOrderedAscending );
        else if ( v1 > v2 )
            return( NSOrderedDescending );
        else
            return( NSOrderedSame );
    }];

    for ( NSMutableDictionary *location in locationsArray )
    {
        NSArray *array = location[@"business_hours"];
        NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
        {
            NSDictionary *d1 = obj1;
            NSDictionary *d2 = obj2;

            int v1 = [d1[@"day"] intValue];
            int v2 = [d2[@"day"] intValue];

            if ( v1 < v2 )
                return( NSOrderedAscending );
            else if ( v1 > v2 )
                return( NSOrderedDescending );
            else
                return( NSOrderedSame );
        }];
        [location setObject:sorted forKey:@"business_hours"];
    }

    NSLog( @"%@", locationsArray );

    return( locationsArray );
}
-(NSArray*)parseAndSortJsonResponse:(NSData*)数据
{
NSDictionary*jsonData=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:nil];
if(!jsonData)
{
NSLog(@“无效的JSON字符串”);
回报率(零);
}
NSMutableDictionary*locationDictionary=jsonData[@“位置”];
NSArray*locationAllValues=[locationDictionary allValues];
NSArray*locationsArray=[locationAllValues SortedArray使用比较器:^n比较结果(id obj1,id obj2)
{
NSDictionary*d1=obj1;
NSDictionary*d2=obj2;
intv1=[d1[@“rest_id”]intValue];
intv2=[d2[@“rest_id”]intValue];
如果(v1v2)
返回(下移);
其他的
返回(DeredName);
}];
用于(NSMutableDictionary*locationsArray中的位置)
{
NSArray*数组=位置[@“营业时间”];
NSArray*排序=[使用比较器进行数组排序:^n比较结果(id obj1,id obj2)
{
NSDictionary*d1=obj1;
NSDictionary*d2=obj2;
intv1=[d1[@“day”]intValue];
intv2=[d2[@“day”]intValue];
如果(v1v2)
返回(下移);
其他的
返回(DeredName);
}];
[location setObject:sorted forKey:@“营业时间”];
}
NSLog(@“%@”,位置为RAY);
返回(位置sarray);
}

发布一个JSON字符串的实际实例,尽可能缩短,我来看看。换句话说,我希望看到一个JSON字符串,它从顶层开始,包括
[67]=>Array
条目和
[0]=>Array
[1]=>Array
子条目,但没有[67]的其他子条目,也没有完成所有其他条目[89]、[99]等。希望它提供了所有任何人都需要看到才能理解的结果输出。我猜我的iOS开发人员真的很喜欢它,当他看到一个类似于
{“locations”:[{},{},…],“error”:“}
的数组时,我通常使用PHP在服务器端完成所有繁重的工作,但是在按数字日排序工作时间以及我是否需要特定格式的数组(对还是错)时会遇到问题@我已经修改了我关于词典的评论。
NSArray *locationAllValues = [locationDictionary allValues];
- (NSArray *)parseAndSortJsonResponse:(NSData *)data
{
    NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if ( !jsonData )
    {
        NSLog( @"Invalid JSON string" );
        return( nil );
    }

    NSMutableDictionary *locationDictionary = jsonData[@"locations"];
    NSArray *locationAllValues = [locationDictionary allValues];

    NSArray *locationsArray = [locationAllValues sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
    {
        NSDictionary *d1 = obj1;
        NSDictionary *d2 = obj2;

        int v1 = [d1[@"rest_id"] intValue];
        int v2 = [d2[@"rest_id"] intValue];

        if ( v1 < v2 )
            return( NSOrderedAscending );
        else if ( v1 > v2 )
            return( NSOrderedDescending );
        else
            return( NSOrderedSame );
    }];

    for ( NSMutableDictionary *location in locationsArray )
    {
        NSArray *array = location[@"business_hours"];
        NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
        {
            NSDictionary *d1 = obj1;
            NSDictionary *d2 = obj2;

            int v1 = [d1[@"day"] intValue];
            int v2 = [d2[@"day"] intValue];

            if ( v1 < v2 )
                return( NSOrderedAscending );
            else if ( v1 > v2 )
                return( NSOrderedDescending );
            else
                return( NSOrderedSame );
        }];
        [location setObject:sorted forKey:@"business_hours"];
    }

    NSLog( @"%@", locationsArray );

    return( locationsArray );
}