Array Duplicate Item Filtering by Element Value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I'm learning to code PHP and gave myself a challenge: Filter array data by matching a specific element value for duplication. I went through it logically on paper then wrote code and it worked on the first try :) But I bet it is the most inefficient way, so to learn I would like to compare it against what others would do. Below is my code. The array is just a few elements with keys like name and id. My result is a list of only items with unique ids from an array with many duplicate ids but different names.



Also, part of the challenge was testing XML, JSON, and Arrays so I made the original data XML and used SimpleXML but made a copy of that data as a PHP array and worked with that to do filtering (XPath challenge next).



Thanks for looking and any suggested improvements that I can learn from.



//$items is a SimpleXML array of items with name and id elements with many duplicate ids but different names.

//first iteration of id collection
$ids = array();
$countedIds = array();
//convert simplexml array $items to normal array (copy)
$itemsArray = json_decode(json_encode($items), TRUE);
foreach ($itemsArray as $idItem)

//collection all ids
$ids = $idItem['id'];

//count number of each id and store in separate array
$countedIds = array_count_values($ids);
#print_r($countedIds);

//start cleaning out duplicates
foreach($itemsArray as $key => &$item)

//clear any whitespace (thanks internets)
#$thisid = trim($item->id); //if using simplexml
$thisid = trim($item['id']);
//check if this id is duplicated (multiple copies) in array
if($countedIds[$thisid] >1)

echo $thisid . "item has duplicates (" . $countedIds[$thisid] . ")<br />";
//add found dupes to separate array
$dupes = $thisid;
//remove this item from $itemsArray and original $items simplexml array
unset($itemsArray[$key]);
unset($items[$key]);
echo "removed element index " . $key . $item . "<br /><hr>";
//remove this id from $ids array
unset($ids[$key]);
//clear out the counted ids
unset($countedIds);
//make a fresh array for counting ids
$countedIds = array();
//get updated id count
$countedIds = array_count_values($ids);

//test simplexml unset?
#unset($sxe->$this->channel->item);


//review results
print_r($countedIds);
print_r($ids);
print_r($dupes);
print_r($itemsArray);
print_r($items);






share|improve this question



























    up vote
    2
    down vote

    favorite












    I'm learning to code PHP and gave myself a challenge: Filter array data by matching a specific element value for duplication. I went through it logically on paper then wrote code and it worked on the first try :) But I bet it is the most inefficient way, so to learn I would like to compare it against what others would do. Below is my code. The array is just a few elements with keys like name and id. My result is a list of only items with unique ids from an array with many duplicate ids but different names.



    Also, part of the challenge was testing XML, JSON, and Arrays so I made the original data XML and used SimpleXML but made a copy of that data as a PHP array and worked with that to do filtering (XPath challenge next).



    Thanks for looking and any suggested improvements that I can learn from.



    //$items is a SimpleXML array of items with name and id elements with many duplicate ids but different names.

    //first iteration of id collection
    $ids = array();
    $countedIds = array();
    //convert simplexml array $items to normal array (copy)
    $itemsArray = json_decode(json_encode($items), TRUE);
    foreach ($itemsArray as $idItem)

    //collection all ids
    $ids = $idItem['id'];

    //count number of each id and store in separate array
    $countedIds = array_count_values($ids);
    #print_r($countedIds);

    //start cleaning out duplicates
    foreach($itemsArray as $key => &$item)

    //clear any whitespace (thanks internets)
    #$thisid = trim($item->id); //if using simplexml
    $thisid = trim($item['id']);
    //check if this id is duplicated (multiple copies) in array
    if($countedIds[$thisid] >1)

    echo $thisid . "item has duplicates (" . $countedIds[$thisid] . ")<br />";
    //add found dupes to separate array
    $dupes = $thisid;
    //remove this item from $itemsArray and original $items simplexml array
    unset($itemsArray[$key]);
    unset($items[$key]);
    echo "removed element index " . $key . $item . "<br /><hr>";
    //remove this id from $ids array
    unset($ids[$key]);
    //clear out the counted ids
    unset($countedIds);
    //make a fresh array for counting ids
    $countedIds = array();
    //get updated id count
    $countedIds = array_count_values($ids);

    //test simplexml unset?
    #unset($sxe->$this->channel->item);


    //review results
    print_r($countedIds);
    print_r($ids);
    print_r($dupes);
    print_r($itemsArray);
    print_r($items);






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm learning to code PHP and gave myself a challenge: Filter array data by matching a specific element value for duplication. I went through it logically on paper then wrote code and it worked on the first try :) But I bet it is the most inefficient way, so to learn I would like to compare it against what others would do. Below is my code. The array is just a few elements with keys like name and id. My result is a list of only items with unique ids from an array with many duplicate ids but different names.



      Also, part of the challenge was testing XML, JSON, and Arrays so I made the original data XML and used SimpleXML but made a copy of that data as a PHP array and worked with that to do filtering (XPath challenge next).



      Thanks for looking and any suggested improvements that I can learn from.



      //$items is a SimpleXML array of items with name and id elements with many duplicate ids but different names.

      //first iteration of id collection
      $ids = array();
      $countedIds = array();
      //convert simplexml array $items to normal array (copy)
      $itemsArray = json_decode(json_encode($items), TRUE);
      foreach ($itemsArray as $idItem)

      //collection all ids
      $ids = $idItem['id'];

      //count number of each id and store in separate array
      $countedIds = array_count_values($ids);
      #print_r($countedIds);

      //start cleaning out duplicates
      foreach($itemsArray as $key => &$item)

      //clear any whitespace (thanks internets)
      #$thisid = trim($item->id); //if using simplexml
      $thisid = trim($item['id']);
      //check if this id is duplicated (multiple copies) in array
      if($countedIds[$thisid] >1)

      echo $thisid . "item has duplicates (" . $countedIds[$thisid] . ")<br />";
      //add found dupes to separate array
      $dupes = $thisid;
      //remove this item from $itemsArray and original $items simplexml array
      unset($itemsArray[$key]);
      unset($items[$key]);
      echo "removed element index " . $key . $item . "<br /><hr>";
      //remove this id from $ids array
      unset($ids[$key]);
      //clear out the counted ids
      unset($countedIds);
      //make a fresh array for counting ids
      $countedIds = array();
      //get updated id count
      $countedIds = array_count_values($ids);

      //test simplexml unset?
      #unset($sxe->$this->channel->item);


      //review results
      print_r($countedIds);
      print_r($ids);
      print_r($dupes);
      print_r($itemsArray);
      print_r($items);






      share|improve this question













      I'm learning to code PHP and gave myself a challenge: Filter array data by matching a specific element value for duplication. I went through it logically on paper then wrote code and it worked on the first try :) But I bet it is the most inefficient way, so to learn I would like to compare it against what others would do. Below is my code. The array is just a few elements with keys like name and id. My result is a list of only items with unique ids from an array with many duplicate ids but different names.



      Also, part of the challenge was testing XML, JSON, and Arrays so I made the original data XML and used SimpleXML but made a copy of that data as a PHP array and worked with that to do filtering (XPath challenge next).



      Thanks for looking and any suggested improvements that I can learn from.



      //$items is a SimpleXML array of items with name and id elements with many duplicate ids but different names.

      //first iteration of id collection
      $ids = array();
      $countedIds = array();
      //convert simplexml array $items to normal array (copy)
      $itemsArray = json_decode(json_encode($items), TRUE);
      foreach ($itemsArray as $idItem)

      //collection all ids
      $ids = $idItem['id'];

      //count number of each id and store in separate array
      $countedIds = array_count_values($ids);
      #print_r($countedIds);

      //start cleaning out duplicates
      foreach($itemsArray as $key => &$item)

      //clear any whitespace (thanks internets)
      #$thisid = trim($item->id); //if using simplexml
      $thisid = trim($item['id']);
      //check if this id is duplicated (multiple copies) in array
      if($countedIds[$thisid] >1)

      echo $thisid . "item has duplicates (" . $countedIds[$thisid] . ")<br />";
      //add found dupes to separate array
      $dupes = $thisid;
      //remove this item from $itemsArray and original $items simplexml array
      unset($itemsArray[$key]);
      unset($items[$key]);
      echo "removed element index " . $key . $item . "<br /><hr>";
      //remove this id from $ids array
      unset($ids[$key]);
      //clear out the counted ids
      unset($countedIds);
      //make a fresh array for counting ids
      $countedIds = array();
      //get updated id count
      $countedIds = array_count_values($ids);

      //test simplexml unset?
      #unset($sxe->$this->channel->item);


      //review results
      print_r($countedIds);
      print_r($ids);
      print_r($dupes);
      print_r($itemsArray);
      print_r($items);








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 9 at 16:50









      Sam Onela

      5,77461543




      5,77461543









      asked May 9 at 16:36









      iswitch

      111




      111




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          If the sole purpose was to have a resultant array of objects that did not have a duplicated id then I would be tempted with the below:



          // Indexes keyed by item id
          $indexes = array();

          // Cast to array of objects
          $items = current((array)$items);

          foreach ($items as $index => $item)
          $id = ((array)$item)['id'];

          if (isset($indexes[$id]))
          // Unset this item
          unset($items[$index]);
          // Unset the original instance
          unset($items[$indexes[$id]]);
          else
          $indexes[$id] = $index;



          // List names of items that doesn't have an id that is duplicated
          foreach ($items as $item)
          // Still have original SimpleXml object to work with
          echo $item->name . "n";






          share|improve this answer





















          • Looks elegant, thanks for sharing this.
            – iswitch
            May 11 at 14:06










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194026%2farray-duplicate-item-filtering-by-element-value%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          If the sole purpose was to have a resultant array of objects that did not have a duplicated id then I would be tempted with the below:



          // Indexes keyed by item id
          $indexes = array();

          // Cast to array of objects
          $items = current((array)$items);

          foreach ($items as $index => $item)
          $id = ((array)$item)['id'];

          if (isset($indexes[$id]))
          // Unset this item
          unset($items[$index]);
          // Unset the original instance
          unset($items[$indexes[$id]]);
          else
          $indexes[$id] = $index;



          // List names of items that doesn't have an id that is duplicated
          foreach ($items as $item)
          // Still have original SimpleXml object to work with
          echo $item->name . "n";






          share|improve this answer





















          • Looks elegant, thanks for sharing this.
            – iswitch
            May 11 at 14:06














          up vote
          1
          down vote













          If the sole purpose was to have a resultant array of objects that did not have a duplicated id then I would be tempted with the below:



          // Indexes keyed by item id
          $indexes = array();

          // Cast to array of objects
          $items = current((array)$items);

          foreach ($items as $index => $item)
          $id = ((array)$item)['id'];

          if (isset($indexes[$id]))
          // Unset this item
          unset($items[$index]);
          // Unset the original instance
          unset($items[$indexes[$id]]);
          else
          $indexes[$id] = $index;



          // List names of items that doesn't have an id that is duplicated
          foreach ($items as $item)
          // Still have original SimpleXml object to work with
          echo $item->name . "n";






          share|improve this answer





















          • Looks elegant, thanks for sharing this.
            – iswitch
            May 11 at 14:06












          up vote
          1
          down vote










          up vote
          1
          down vote









          If the sole purpose was to have a resultant array of objects that did not have a duplicated id then I would be tempted with the below:



          // Indexes keyed by item id
          $indexes = array();

          // Cast to array of objects
          $items = current((array)$items);

          foreach ($items as $index => $item)
          $id = ((array)$item)['id'];

          if (isset($indexes[$id]))
          // Unset this item
          unset($items[$index]);
          // Unset the original instance
          unset($items[$indexes[$id]]);
          else
          $indexes[$id] = $index;



          // List names of items that doesn't have an id that is duplicated
          foreach ($items as $item)
          // Still have original SimpleXml object to work with
          echo $item->name . "n";






          share|improve this answer













          If the sole purpose was to have a resultant array of objects that did not have a duplicated id then I would be tempted with the below:



          // Indexes keyed by item id
          $indexes = array();

          // Cast to array of objects
          $items = current((array)$items);

          foreach ($items as $index => $item)
          $id = ((array)$item)['id'];

          if (isset($indexes[$id]))
          // Unset this item
          unset($items[$index]);
          // Unset the original instance
          unset($items[$indexes[$id]]);
          else
          $indexes[$id] = $index;



          // List names of items that doesn't have an id that is duplicated
          foreach ($items as $item)
          // Still have original SimpleXml object to work with
          echo $item->name . "n";







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 11 at 1:35









          GeorgeQ

          1112




          1112











          • Looks elegant, thanks for sharing this.
            – iswitch
            May 11 at 14:06
















          • Looks elegant, thanks for sharing this.
            – iswitch
            May 11 at 14:06















          Looks elegant, thanks for sharing this.
          – iswitch
          May 11 at 14:06




          Looks elegant, thanks for sharing this.
          – iswitch
          May 11 at 14:06












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194026%2farray-duplicate-item-filtering-by-element-value%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?