0

I am running a query on a dbase that returns a multi-field recordset or array on fetch. The array is multi-field as I need several fields to output to the screen. However, I then want to save an array of just one variable, the id for each record. to compare against another list of records. So I need to, in effect, strip all but one field from the array.

In effect, I want to reduce (not using proper array notation):

{1 red dog, 2 orange cat, 3 yellow canary}

to

{1,2,3}

Is there an easy way to do this?

Thanks in advance!

2
  • Are the IDs the keys of the array? If not, please use a proper array notation (the result of var_export() will do). Commented Oct 6, 2012 at 15:44
  • so why not simply do select idfield from ... and NOT fetch the fields you don't want? Commented Oct 6, 2012 at 15:49

3 Answers 3

2

Basically what you want is called array plucking; here's an RFC to introduce this into the language, but it hasn't been accepted.

In the meantime you'd just have to do it the old fashioned way:

$a = array(
    array('id' => 1, 'title' => 'title1'),
    array('id' => 2, 'title' => 'title2'),
);

array_map(function($v) {
    return $v['id'];
}, $a);

// returns array(1, 2)

Before 5.3 you'd have to write it like so:

function getid($v)
{
    return $v['id'];
}

array_map('getid', $a);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I am trying to implement this letter for letter and getting error unexpected T_FUNCTION, expecting ')' Any idea what problem could be?
Closures were introduced in 5.3, for earlier erosions you need to write the callback as a regular function and pass that name as the first argument.
ok. I do not know this syntax. Can you provide or do you know a link to translate? Thx.
1

I'm not sure I follow your explanation of the array setup, but maybe the function you're looking for is the "array_keys" PHP function, which returns the array keys only.

If this isn't the answer you're looking for, perhaps you could clarify your question by using the proper array notation, or possibly an actual example from your database specifying which value you're trying to isolate.

1 Comment

The ids I am looking for do not exactly follow keys. There are gaps etc.
1

Your array format is not clear

Option A

$array = array("1 red dog", "2 orange cat", "3 yellow canary");
$array = array_map(function($var){  $var = explode(" ", $var,2) ; return array_shift($var); }, $array);
var_dump($array);

Option B

$array = array("1" =>"red dog", "2" =>"orange cat", "3" =>"yellow canary");
$array = array_keys($array);
var_dump($array);

Output

array
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '3' (length=1)

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.