我有一个多维数组,我希望从中获取唯一的子值,但也要计算这些唯一子值出现的次数。
例如,这将是我的起始数组:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
最后,我想要一个看起来像这样的数组:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
是否有任何一般/简单的方法可以在不迭代第一个数组的情况下执行此操作,比较/存储临时数组中的值,检查它们并添加到计数中?
答案 0 :(得分:1)
要获得这种确切的格式,你可能需要迭代思考你当前的数组并手动进行计数,但是php有这样的事情的array_count_values()和array_unique()函数:
答案 1 :(得分:1)
因为您只关心数组的最深层值,所以使用<manifest android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23"/>
<uses-permission android:name="android.permission.INTERNET"/>
<!-- GCM PERMISSIONS -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Only this application can receive the messages and registration result -->
<permission android:name="APPLICATION_PACKAGE.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="APPLICATION_PACKAGE.permission.C2D_MESSAGE" />
<application>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="APPLICATION_PACKAGE" />
</intent-filter>
</receiver>
<service
android:name="com.distriqt.extension.pushnotifications.gcm.GcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.distriqt.extension.pushnotifications.gcm.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service android:name="com.distriqt.extension.pushnotifications.gcm.RegistrationIntentService" android:exported="false" />
<activity android:name="com.distriqt.extension.pushnotifications.PushNotificationsActivity">
<intent-filter>
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_DEFAULT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- NOTIFICATIONS -->
<receiver android:name="com.distriqt.extension.pushnotifications.notifications.receivers.NotificationReceiver">
<intent-filter>
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_SELECTED" />
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_DELETED" />
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_ACTION" />
<data android:scheme="dtpn" />
</intent-filter>
</receiver>
</application>
</manifest>
似乎适合这一点。请注意,在回调中使用了对输出数组array_walk_recursive
的引用。
$counted
使用id作为array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
数组中的键将简化计数。结果会与您建议的输出略有不同,但在我看来,实际上使用起来会更简单。 (例如$counted
)。
foreach ($counted as $id => $count) {...