tags:

views:

106

answers:

7

I have a result set of data that I want to write to an array in php. Here is my sample data:

**Name** **Abbrev**
Mike     M
Tom      T
Jim      J

Using that data, I want to create an array in php that is of the following:

1|Mike|M
2|Tom|T
3|Jim|j

I tried array_push($values, 'name', 'abbreviation') [pseudo code], which gave me the following:

1|Mike
2|M
3|Tom
4|T
5|Jim
6|J

I need to do a look up against this array to get the same key value, if I look up "Mike" or "M".

What is the best way to write my result set into an array as set above where name and abbreviation share the same key?

A: 

have to set the same value to both Mike and M for keys.

Gnostus
+2  A: 

PHP's not my top language, but try these:

array_push($values, array("Mike", "M"))
array_push($values, array("Tom", "T"))
array_push($values, array("Jim", "J"))

$name1 = $values[1][0]
$abbrev1 = $values[1][1]

or:

array_push($values, array("name" => "Mike", "abbrev" => "M"))
array_push($values, array("name" => "Tom", "abbrev" => "T"))
array_push($values, array("name" => "Jim", "abbrev" => "J"))

$name1 = $values[1]["name"]
$abbrev1 = $values[1]["abbrev"]

The trick is to use a nested array to pair the names and abbreviations in each entry.

Walter Mundt
+1  A: 
$person = array('name' => 'Mike', 'initial' => 'M');
array_push($people, $person);

That said, I'm not sure why you're storing the data separately. The initial can be fetched directly from the name via substr($name, 0, 1).

ceejayoz
Basically, he's saying that you need to use a multi-dimensional array. The data array should contain numbers as keys and arrays as values. The arrays as values contain the name in one key and the value in another key. These two keys should be predefined (0 and 1 / name and initial / n and i / etc).
Tom
@ceejayoz, thanks for the suggestion on fetching the abbrev directly, but your suggestion only works for my simplified sample set. This suggestion would not work for my real data set because abbreviation is actually more characters and not always within the set of characters within the name.
Mike Munroe
A: 

php arrays work like hash lookup tables, so in order to achieve the desired result, you can initialize 2 keys, one with the actual value and the other one with a reference pointing to the first. For instance you could do:

$a = array('m' => 'value');
$a['mike'] = &$a['m']; //notice the end to pass by reference

if you try:

$a = array('m' => 'value');
$a['mike'] = &$a['m'];

print_r($a);

$a['m'] = 'new_value';
print_r($a);

$a['mike'] = 'new_value_2';
print_r($a);

the output will be:

Array
(
    [m] => value
    [mike] => value
)
Array
(
    [m] => new_value
    [mike] => new_value
)
Array
(
    [m] => new_value_2
    [mike] => new_value_2
)
Maurice Kherlakian
A: 

You could use two separate arrays, maybe like:

$values_names = array();
$values_initials = array();
array_push($values_names, 'Mike');
array_push($values_initials, 'M');
array_push($values_names, 'Tom');
array_push($values_initials, 'T');
array_push($values_names, 'Jim');
array_push($values_initials, 'J');

So you use two arrays, one for each of the second and third columns using the values in the first one as keys for both arrays.

Tom
PS: the keys are indexed starting with zero in PHP (as in most languages), so the first key-value pair will have the key 0; the second key-value pair will have the key 1; the third key-value pair will have the key 2; etc
Tom
+1  A: 

You will need to create a two dimensional array to store more than one value.

Each row in your result set is already an array, so it will need to be added to your variable as an array.

array_push($values, array('name', 'abbreviation'));
Phil
A: 

maybe you create a simple class for that as the abbreviation is redundant information in your case

class Person
{
    public $name;

    pulbic function __construct($name)
    {
        $this->name = (string)$name;
    }

    public function getAbbrev()
    {
        return substr($this->name, 0, 1);
    }

    public function __get($prop)
    {
        if ($prop == 'abbrev') {

            return $this->getAbbrev();
        }
    }
}


$persons = array(
    new Person('Mike'),
    new Person('Tom'),
    new Person('Jim')
);

foreach ($persons as $person) {

   echo "$person->name ($person->abbrev.)<br/>";
}
zolex