Jump to content

How check if somthing is an array


johnsmith153

Recommended Posts

I dont think any of this will do it.

 

I need to CREATE unique values for all matching arrays,

 

so, if I start

 

$array[0]="Jim Smith";

$array[1]="Dave Brown";

$array[2]="Dennis Brown";

$array[3]="Steve Smith";

 

I need to return the surname, but if not unique, I need to append the first letter of name, then keep checking to ensure unique

 

So I would then end with:

 

$array[0]="J.Smith";

$array[1]="Da.Brown";

$array[2]="De.Brown";

$array[3]="S.Smith";

 

Any ideas?

 

 

Link to comment
Share on other sites

There may be a function out there to work this out but a long way round would be looping through the array to create a string then testing the string with substr_count() to return an integer of matching occurances. Then it would be a simple case of testing the value of the integer using an if statement.

Link to comment
Share on other sites

This may be expensive, but could you possibly set each name to its own array containing first ans last names

 

array(

array('first_name' => 'jim', 'last_name' => 'smith')

);

 

then use this class to order by last name (http://www.phpfreaks.com/forums/index.php/topic,220273.0.html) then loop through that returned array and see if the current iteration's last_name field is equal to the previous iteration's. If it is, appened the second letter of the first name, if not, just the first letter.

 

 

Link to comment
Share on other sites

Thanks for the ideas.

 

Emehrkay has a possible idea, but it does seem very long winded.

 

Just to help, there will never be any more than 14 different names to consider.

 

This may make it easier to do this.

 

Either way, there must be a better way to do this.

 

Any more ideas are appreciated?

Link to comment
Share on other sites

This should be a php4 safe version (your sig says php4) of that sort class

 

<?php

/**
* 
*/
class Utilities_Array_Sort{
    /**
     * @param _array array array of data to be sorted
     * @access private
     */
    var $_array;
    
    /**
     * @param _sort_key String the key to sort the array by
     * @access private
     */
    var $_sort_key;
    
    /**
     * Class Constructor
     *
     * @access public
     * @return null
     */     
    function Utilities_Array_Sort(array $array = array(), $sort_key = null){
        $this->_array       = $array;
        $this->_sort_key    = $sort_key; 
    }
    
    /**
     * sorts the array 
     *
     * @param $direction String Direction that you would like to sort the array by
     * @access public
     * @return RETURN TYPE
     */
    function sort($direction = 'ascending'){
        switch($direction){
            case 'asc':
            case 'ascending':
                usort($this->_array, array('Utilities_Array_Sort', 'ascending'));
                break;
                
            case 'desc':
            case 'descending':
                usort($this->_array, array('Utilities_Array_Sort', 'descending'));
                break;
        }
        
        return $this->_array;
    }
    
    /**
     * sets a new array
     *
     * @access public
     * @return instance of Utilities_Array_Sort class
     */
    function setArray(array $array){
        $this->_array = $array;
        
        return $this;
    }
    
    /**
     * sets a new sort by key
     *
     * @access public
     * @return instance of Utilities_Array_Sort class
     */
    function setSortKey($sort_key = false){
        if($sort_key) $this->_sort_key = $sort_key;
        
        return $this;
    }
    
    /**
     * sorts the array in ascending order
     *
     * @access private
     * @return int
     */
    function ascending($a, $b){
        return strcmp($a[$this->_sort_key], $b[$this->_sort_key]); 
    }
    
    /**
     * sorts the array in descending order
     *
     * @access private
     * @return int
     */
    function descending($a, $b){
        return strcmp($a[$this->_sort_key], $b[$this->_sort_key]) * -1; 
    }
}
?>

Link to comment
Share on other sites

Okay here is a quick script to test a unique array;

 

$array = array("apple","pear","banana","apple");

// create a string out of the array
for($i=0; $i<count($array); $i++) {
$string .= $array[$i];
}

// search the string
$testString = "pear";
if(substr_count($string, $testString) == 1) {
echo "{$testString} is unique!";
} else {
echo "{$testString} is not unique";
}

 

it works leaving the original $array untouched.

Link to comment
Share on other sites

Are these results from a database query? if so, you can skip using the class and just loop through the multi dimensional array like this

 

<?php

$arr = array(
    array('first_name' => 'ab', 'last_name' => 'bb'),
    array('first_name' => 'ac', 'last_name' => 'bb'),
    array('first_name' => 'ad', 'last_name' => 'bbl'),
    array('first_name' => 'ae', 'last_name' => 'bbl'),
    array('first_name' => 'af', 'last_name' => 'bbt'),
    array('first_name' => 'ag', 'last_name' => 'bbr'),
    array('first_name' => 'ah', 'last_name' => 'bbr'),
    array('first_name' => 'ai', 'last_name' => 'bbr'),
    array('first_name' => 'aj', 'last_name' => 'bbs'),
    array('first_name' => 'ak', 'last_name' => 'bbs'),
    array('first_name' => 'al', 'last_name' => 'bbv')
);

$result     = array();
$iteration  = 0;

foreach($arr as $name){
    /**
     * prepare the first name based on teh last name
     * if the current last name is the same as the previous one append the second letter of the first name to the first name
     * else, just use the fist letter
     */
    $first_name = $name['first_name']{0};

    if($iteration++ != 0 && ($name['last_name'] === $arr[($iteration -2)]['last_name'])){ //-2 because we increment $iteration inline
        $first_name .= '.'. $name['first_name']{1};
    }
    
    $result[] = $first_name .' '. $name['last_name'];
}

echo '<pre>', print_r($result), '</pre>';

?>

 

this is assuming that the last names are already in order and your array is a multidimensional array which is easy to do when querying from a database

 

Link to comment
Share on other sites

Look at the code in my last example.

 

Set up your array like it is there

 

array(

array('first_name' => 'joe', 'last_name' => 'smith')

);

 

then run it through the class here (http://www.phpfreaks.com/forums/index.php/topic,220273.0.html) there are usage examples

 

then run sorted output through the code that i wrote above and you'll have you answer

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.