多维非序列化数组 - 提取数据 - 计算提取的数据并对A-Z进行排序

时间:2016-07-01 10:35:11

标签: php arrays multidimensional-array

我有这个多维数组:

Array ( 
[0] => a:7:{s:21:"mage_form_post_status";s:5:"draft";s:19:"mage_form_post_type";s:4:"post";s:25:"mage_form_post_permission";s:6:"public";s:21:"mage_form_post_author";s:1:"1";s:23:"mage_form_post_redirect";s:1:"0";s:19:"mage_form_post_edit";b:0;} 
[1] => a:4:{s:14:"mage_your_name";s:14:"George Jackson";s:15:"mage_your_email";s:24:"amy.thompson41@gmail.com";s:13:"mage_who_name";s:8:"Gym Goer";s:10:"mage_video";s:0:"";} 
[2] => a:7:{s:21:"mage_form_post_status";s:5:"draft";s:19:"mage_form_post_type";s:3:"gym";s:25:"mage_form_post_permission";s:6:"public";s:21:"mage_form_post_author";s:1:"1";s:23:"mage_form_post_redirect";s:2:"88";s:19:"mage_form_post_edit";b:0;}
[3] => a:2:{s:15:"mage_tags_input";s:0:"";s:14:"mage_your_name";s:5:"Denis";} 
[4] => a:1:{s:14:"mage_your_name";s:5:"Denis";}
[5] => a:2:{s:13:"mage_gym_name";s:12:"Fanna - test";s:14:"mage_your_name";s:4:"John";} 
[7] => a:2:{s:13:"mage_gym_name";s:11:"Boss - test";s:14:"mage_your_name";s:4:"Rudy";} 
[8] => a:6:{s:21:"mage_form_post_status";s:5:"draft";s:19:"mage_form_post_type";s:4:"post";s:25:"mage_form_post_permission";s:11:"contributor";s:21:"mage_form_post_author";i:0;s:20:"mage_form_post_email";s:0:"";s:23:"mage_form_post_redirect";i:0;} 
[9] => a:2:{s:13:"mage_gym_name";s:11:"Batt - test";s:14:"mage_your_name";s:3:"Ann";} 
[10] => a:2:{s:13:"mage_gym_name";s:11:"Boss - test";s:14:"mage_your_name";s:6:"Freddy";} 
)

我想以A-Z顺序显示这样的结果:

Batt - test(1)

老板 - 测试(2)

范娜 - 测试(1)

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

使用import UIKit let b1 = UIBarButtonItem(title: "Red", style: .Plain, target: nil, action: nil) b1.tintColor = UIColor.redColor() let b2 = UIBarButtonItem(title: "Green", style: .Plain, target: nil, action: nil) b2.tintColor = UIColor.greenColor() let item = UINavigationItem(title: "nav") item.leftBarButtonItem = b1 item.rightBarButtonItem = b2 dump(item.titleView?.tintColor) dump(item.leftBarButtonItem?.tintColor) dump(item.rightBarButtonItem?.tintColor) /* - nil ▿ UIDeviceRGBColorSpace 1 0 0 1 ▿ Some: UIDeviceRGBColorSpace 1 0 0 1 #0 ▿ UIDeviceRGBColor: UIDeviceRGBColorSpace 1 0 0 1 ▿ UIColor: UIDeviceRGBColorSpace 1 0 0 1 - NSObject: UIDeviceRGBColorSpace 1 0 0 1 ▿ UIDeviceRGBColorSpace 0 1 0 1 ▿ Some: UIDeviceRGBColorSpace 0 1 0 1 #0 ▿ UIDeviceRGBColor: UIDeviceRGBColorSpace 0 1 0 1 ▿ UIColor: UIDeviceRGBColorSpace 0 1 0 1 - NSObject: UIDeviceRGBColorSpace 0 1 0 1 */ array_mapunserialize(自PHP 5.5开始提供),array_columnarray_count_values函数的解决方案:

ksort

输出与以下内容类似:

// $serialized_data is your initial array

$unserialized = array_map('unserialize', $serialized_data);
$gym_names = array_column($unserialized, 'mage_gym_name');  // getting all 'mage_gym_name' entries
$counts = array_count_values($gym_names);
ksort($counts);  // sorting by keys

print_r($counts);