0

I'm trying to generate an array that will look like this:

Array ( [123 Smith St, Begora] => L1234 [55 Crumble Road, Mosmana] => L2456 [99 Jones Ave, Gestana] => L3456 )  

which will ultimately be used for a select menu on an html form.

I'm retrieving a list of records and propertyID numbers from a database as follows:

foreach($records as $record) {
    $propertyID = $record->getField('propertyID');
    $property = $record->getField('propertyAddress');  
        
    echo $propertyID.'<br>'; 
    echo $property.'<br>';              
}

which displays like this when I retrieve 3 records:

L1234 123 Smith St, Begora L2456 55 Crumble Road, Mosmana L3456 99 Jones Ave, Gestana

I just can't work out how to convert this into an Array which I can then use later on in my page for generating a select menu.

3
  • It is generally considered bad practice to have spaces etc in key names. Especially as you could start getting addresses with apostrophes etc in them Commented May 15, 2013 at 16:19
  • Hi - not sure what you're referring to here by "spaces in key names"? Can you clarify - thanks. Commented May 15, 2013 at 16:23
  • In the example you gave above you would have an array with the key name [123 Smith St, Begora] it would be much better for this to be the value rather than the key where an array is made up of array("key"=>"value") Commented May 15, 2013 at 16:25

2 Answers 2

2

Just do

foreach($records as $record) {
    $propertyID = $record->getField('propertyID');
    $property = $record->getField('propertyAddress');  

    $addresses[$property] = $propertyID;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'd probably use $propertyID for the array index, since it's shorter and easier to sort.
It is also less likely to include non alpha chars
aynber and Anigel yeah i would too, but I was trying to produce output to match OP's requirements in the question :)
1

Something like this:

$array = array();
foreach($records as $record) {
        $array[$record->getField('propertyAddress')] = $record->getField('propertyID');            
}

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.