Jump to content

Array Problem


viviosoft

Recommended Posts

Hello All -

 

I have the following array:

 

array
  0 => 
    array
      'farmName' => string 'Hallinan' (length=
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  1 => 
    array
      'farmName' => string 'Holt' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  2 => 
    array
      'farmName' => string 'Holt' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  3 => 
    array
      'farmName' => string 'Holt' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  4 => 
    array
      'farmName' => string 'Holt' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  5 => 
    array
      'farmName' => string 'Holt' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  6 => 
    array
      'farmName' => string 'Home' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  7 => 
    array
      'farmName' => string 'Home' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  8 
    array
      'farmName' => string 'Home' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)

 

The data comes from a flat file that has (as you can see) many of the same names like Home and Holt.  Is there a way to extract the first Holt and Home from the array and put those values in a new array?  So my new array would look something like:

 

array
  0 => 
    array
      'farmName' => string 'Hallinan' (length=
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  1 => 
    array
      'farmName' => string 'Holt' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)
  2 => 
    array
      'farmName' => string 'Home' (length=4)
      'farmInsNum' => string '' (length=0)
      'farmFSA' => string '' (length=0)

 

I look through php.net and really couldn't find a function that could handle what I was after.  I'm sure I'd have to use a combination of each() and reset() or maybe not?  Any help would be very welcome.  Thanks!

 

Link to comment
Share on other sites

I think this will do what I'm after.  If there's a cleaner way or another approach, by all means please...

 


$dup_name = array();

foreach ($farmData as $farm) {
    $single_name = array_merge($dup_name, explode(",", $farm['farmName']));
}

var_dump(array_unique($single_name));

Link to comment
Share on other sites

It would be nice if I could retain the key names in the initial array.  Here's the output now.  The method I'm using "works" but I loose the other keys in the array.

 

<?php
$dup_name = array();

foreach ($farmData as $farm) {
    $single_name = array_merge($dup_name, explode(",", $farm['farmName']));
}

var_dump(array_unique($single_name));
?>

 

Output:

 

<?php
array
  0 => string 'Hallinan' (length=
  1 => string 'Holt' (length=4)
  6 => string 'Home' (length=4)
  9 => string 'Kenyon' (length=6)
  19 => string 'Lane' (length=4)
  21 => string 'Leach' (length=5)
  23 => string 'Robinson' (length=
  24 => string 'Shorey' (length=6)
  26 => string 'Stream' (length=6)
?>

Link to comment
Share on other sites

Wow!  I guess I should have waited to post ;) .  I figured out a solution.  Well, I didn't I found the solution on php.net.  Here's what I found for those that might have the same problem.

 

<?php
var_dump(remove_duplicate($farmData, 'farmName'));

function remove_duplicate($array, $field)
{
    foreach ($array as $sub)
       $cmp[] = $sub[$field];
    $unique = array_unique($cmp);
   
foreach ($unique as $k => $rien)
        $new[] = $array[$k];
    return $new;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.