13

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?

7 Answers 7

22

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.

Sign up to request clarification or add additional context in comments.

Comments

7
$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).

2 Comments

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).
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.
2

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'));

Comments

2

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/>";
}

Comments

1

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.

1 Comment

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
-1

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
)

Comments

-3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.