我正在尝试计算php数组中的值并消除重复只留下1.我的安排格式如下:
Array
(
[0] => Array
(
[inscripcion_id] => 3932
[nombre] => Prueba
[email] => prueba@prueba.cl
)
[1] => Array
(
[inscripcion_id] => 3926
[nombre] => Prueba
[email] => prueba@prueba.cl
)
[2] => Array
(
[inscripcion_id] => 3921
[nombre] => Nicolas
[email] => nico@prueba.com
)
)
我看起来匹配是基于email
字段,如果电子邮件出现超过1次,安排必须消除,只留下1.问题是我无法获得经常出现的不少消除它。尝试array_count_values
不起作用,我可以找到每封电子邮件的匹配,但只能在子数组中查找,因此总是会找到1,如下所示:
Array ( [3932] => 1 [Prueba] => 1 [prueba@prueba.cl] => 1 )
Array ( [3926] => 1 [Prueba] => 1 [prueba@prueba.cl] => 1 )
我希望结果如下:
Array
(
[0] => Array
(
[inscripcion_id] => 3932
[nombre] => Prueba
[email] => prueba@prueba.cl
)
[1] => Array
(
[inscripcion_id] => 3921
[nombre] => Nicolas
[email] => nico@prueba.com
)
)
答案 0 :(得分:1)
这应该这样做。
print_r(unique_multidim_array($array, 'email'));
function unique_multidim_array($array, $key){
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val){
if(!in_array($val[$key],$key_array)){
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
答案 1 :(得分:0)
使用电子邮件字段创建一个数组,如果重复,则从源数组中删除:
$temp = array();
$out = array();
foreach( $array as $k => $v ) {
if ( isset( $temp[$v['email']] ) )
unset( $array[$k]);
else {
$temp[$v['email']] = $k;
array_push( $out, $v );
}
}
原始数组位于$array
,输出位于$out
如果密钥不重要,则更简单的方法是
$temp = array();
foreach( $array as $k => $v ) {
if ( isset( $temp[$v['email']] ) )
unset( $array[$k]);
else
$temp[$v['email']] = $k;
}
答案 2 :(得分:0)
您可以尝试通过public class MainActivity extends Activity
{
private static int RESULT_LOAD_IMG = 1;
String decodableString;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view)
{
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try {
// When a video is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data)
{
// Get the video from data
Uri selectedVideo = data.getData();
String[] filePathColumn = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(selectedVideo,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
decodableString = cursor.getString(columnIndex);
Log.i("mok",decodableString);
cursor.close();
new PostFile().run(decodableString);
Log.i("mok","done");
} else
{
Toast.makeText(this, "You haven't picked any video",
Toast.LENGTH_LONG).show();
}
} catch (Exception e)
{
e.printStackTrace();
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
创建数组索引(如主键)。
email
结果:
$data = array(
array(
'inscripcion_id'=>3932,
'nombre'=>'Prueba',
'email'=>'prueba@prueba.cl'
),
array(
'inscripcion_id'=>3926,
'nombre'=>'Prueba',
'email'=>'prueba@prueba.cl'
),
array(
'inscripcion_id'=>3921,
'nombre'=>'Nicolas',
'email'=>'nico@prueba.com'
),
);
$result = array();
foreach($data as $key => $value) {
if (!in_array($value['email'], $result)) {
$result[$value['email']] = $value;
}
}
var_dump($result);
此外,您可以使用它的电子邮件值直接访问您想要的每个子阵列:
array (size=2)
'prueba@prueba.cl' =>
array (size=3)
'inscripcion_id' => int 3926
'nombre' => string 'Prueba' (length=6)
'email' => string 'prueba@prueba.cl' (length=16)
'nico@prueba.com' =>
array (size=3)
'inscripcion_id' => int 3921
'nombre' => string 'Nicolas' (length=7)
'email' => string 'nico@prueba.com' (length=15)
结果:
var_dump($result['prueba@prueba.cl']);
答案 3 :(得分:0)
只需使用电子邮件作为密钥创建结果数组,然后就没有重复项:
foreach($array as $value) {
$result[$value['email']] = $value;
}
然后,如果你想重置密钥:
$result = array_values($result);