1

I would like in php to stop duplicate messages by logging msgid to a text file using something like this file_put_contents("a.txt", implode(PHP_EOL, $array1), FILE_APPEND); and then converting it back to an array using $array1 = file("a.txt"); I would also like to delete messages from the array if they are from a set name I know how to convert json to an array $array1 = json_decode($json, true); Json Reply from an api that I cannot control

{
  "API": "Online",
  "MSG": [
    {
      "info": {
        "name": "example"
      },
      "msg": "example",
      "msgid": "example"
    },
    {
      "info": {
        "name": "example"
      },
      "msg": "example",
      "msgid": "example"
    }
  ]
}
Raccoon
  • 34
  • 9

2 Answers2

1

Hi use the following code, first test it out accordingly

$uniqueMessages = unique_multidim_array($messages,'msg');

Usage : Pass the key as the 2nd parameter for which you need to check the uniqueness of array.

<?php
/* Function to handle unique assocative array */
function unique_multidim_array($array, $key) {
    /* temp array to hold unique array */
        $temp_array = array();

    /* array to hold */
        $i = 0;
    /* array to hold the key for unique array */
    $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;
}

$messages = array(
    0 => array(
        'info' => array(
            'name' => 'example'     
        ),
        'msg' => 'example',
        'msgid' => 'example'
    ),
    1 => array(
        'info' => array(
            'name' => 'example 1'       
        ),
        'msg' => 'example 1',
        'msgid' => 'example 1'
    ),
    3 => array(
        'info' => array(
            'name' => 'example'     
        ),
        'msg' => 'example',
        'msgid' => 'example'
    )

);


echo '<pre>';

echo '*****************BEFORE***********************<br/>';
var_dump($messages);

echo '*****************AFTER***********************<br/>';

$uniqueMessages = unique_multidim_array($messages,'msg');
var_dump($uniqueMessages);
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45
  • Im trying to take all msgids and put them in a file then read that file to check if an msgid has been seen before if it has it will be removed from the array or if info->name = an set string – Raccoon Jul 31 '17 at 13:18
  • @Raccoon you can use the above function just change the following way of calling : $uniqueMessages = unique_multidim_array($messages,'msgid '); but make sure that $messages is an messages array. – Channaveer Hakari Jul 31 '17 at 16:51
  • I got 2 arrays seen msgid and msgs – Raccoon Jul 31 '17 at 17:05
  • @Raccoon Can you do me a favour. Is the array what I have taken as a example same as that of yours. Or else can you please var_dump the result and post in the above question so that I can analyse the array your getting – Channaveer Hakari Jul 31 '17 at 17:07
  • That is the json reply I get from the api the next array is using $Array2 = array_column($Array1, 'mgid'); – Raccoon Jul 31 '17 at 17:10
  • I convert the json to an array with json_decode($json, true); This may help http://jsoneditoronline.org/ – Raccoon Jul 31 '17 at 17:17
  • Okay once you convert that you will be having 2 key value pairs, the 2nd key value pair is what you need to pass as the first parameter and msgid as 2 no parameter to the above function – Channaveer Hakari Jul 31 '17 at 17:19
  • Then I have removed all the msg apart from the id – Raccoon Jul 31 '17 at 17:23
  • Please post the array version of $array2 – Channaveer Hakari Jul 31 '17 at 17:25
  • With the result of that function it will give me a list of unique ids but I still need to remove the stuff from the main array – Raccoon Jul 31 '17 at 17:26
  • Array ( [0] => JzbWg5u [1] => UnPQnns ) used print_r – Raccoon Jul 31 '17 at 17:34
  • Array 2 is never in json as its just used on the server to keep track of seen msgids – Raccoon Jul 31 '17 at 17:45
0

This works for me this is an modded function click here for original function

function RemoveElementByArray($array, $key, $seen){
         foreach($array as $subKey => $subArray){
              if(in_array($subArray[$key], $seen)){
                   unset($array[$subKey]);
              }
         }
         return $array;
    }

Example:

$array = array(
array("id" => "1", "name" => "example1"),
array("id" => "2", "name" => "example2"),
array("id" => "3", "name" => "example3"));
$SeenArray = array("1", "2");
print_r(RemoveElementByArray($array, "id", $SeenArray));

Result:

Array
(
    [2] => Array
        (
            [id] => 3
            [name] => example3
        )

)
Raccoon
  • 34
  • 9