string(2) "24"   ["FromMid"] => string(3) "157"   [&q"/> string(2) "24"   ["FromMid"] => string(3) "157"   [&q">

php数组处置

WBOY
Libérer: 2016-06-13 13:11:23
original
931 Les gens l'ont consulté

php数组处理
array(4) {
  [0] => array(3) {
  ["Id"] => string(2) "24"
  ["FromMid"] => string(3) "157"
  ["ToMid"] => string(3) "157"
  }
  [1] => array(3) {
  ["Id"] => string(2) "22"
  ["FromMid"] => string(3) "157"
  ["ToMid"] => string(3) "176"
  }
  [2] => array(3) {
  ["Id"] => string(1) "4"
  ["FromMid"] => string(3) "157"
  ["ToMid"] => string(2) "25"
  }
  [3] => array(3) {
  ["Id"] => string(1) "1"
  ["FromMid"] => string(2) "25"
  ["ToMid"] => string(3) "157"
  }
}

我想把["FromMid"]==["ToMid"]&&["ToMid"]==["FromMid"]处理掉 只留一条
得出的结果应该是
array(4) {
  [0] => array(3) {
  ["Id"] => string(2) "24"
  ["FromMid"] => string(3) "157"
  ["ToMid"] => string(3) "157"
  }
  [1] => array(3) {
  ["Id"] => string(2) "22"
  ["FromMid"] => string(3) "157"
  ["ToMid"] => string(3) "176"
  }
  [2] => array(3) {
  ["Id"] => string(1) "4"
  ["FromMid"] => string(3) "157"
  ["ToMid"] => string(2) "25"
  }
}

------解决方案--------------------

PHP code

<?php $input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
//          "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
//          "blue", "purple", "yellow");
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
Copier après la connexion
PHP code

$data = array(
    array("Id" => "24", "FromMid" => "157", "ToMid" => "157"),
    array("Id" => "22", "FromMid" => "157", "ToMid" => "176"),
    array("Id" => "4", "FromMid" => "157", "ToMid" => "25"),
    array("Id" => "1", "FromMid" => "25", "ToMid" => "157")
);

$markList = array();
foreach ($data as $key => $val) {
    $posi = md5($val['FromMid'] . $val['ToMid']);
    $anti = md5($val['ToMid'] . $val['FromMid']);
    if (isset($markList[$posi]) || isset($markList[$anti])) {
        unset($data[$key]);
    } else {
        $markList[$posi] = $markList[$anti] = 1;
    }
}
echo '<pre class="brush:php;toolbar:false">';print_r($data);

/**
输出:
Array
(
    [0] => Array
        (
            [Id] => 24
            [FromMid] => 157
            [ToMid] => 157
        )

    [1] => Array
        (
            [Id] => 22
            [FromMid] => 157
            [ToMid] => 176
        )

    [2] => Array
        (
            [Id] => 4
            [FromMid] => 157
            [ToMid] => 25
        )
*/
 <div class="clear">
                 
              
              
        
            </div>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!