开发者

PHP, possible to have multiple keys for one array value?

I'm getting an array of values back from a database where I get an array with values like this:

Array ( [6712] => 64.79.197.36:43444 [6712] => 64.79.197.36:43444 [6711] => 64.79.194.56:41113 [6710] => 64.21.47.20:8080 [6709]) 

Where the numbers in brackets, like [6712] are the "id" field in the database.. the issue is that in my script I want to make it so that the each array value has a second key that I assign, which starts from "0" and increments by one, so something like:

Array ( [0][6712] => 64.79.197.36:43444 [1][6712] => 64.79.197.36:43444 [2开发者_JAVA技巧][6711] => 64.79.194.56:41113 [3][6710] => 64.21.47.20:8080 [4][6709]) 

This is for some data processing so I want to be able to have a way to track each one in a meaningful way within my script (As I can't predict what the mysql id would be as there may be deleted records, etc so its not always incremented by 1)..

I have found some ways to create a new array by assigning a new key but I'm wondering if its possible to have 2 keys for 1 array value


It's perfectly possible, using references.

$result = array ( 6712  => '64.79.197.36:43444',
                  6711  => '64.79.194.56:41113',
                  6710  => '64.21.47.20:8080'
                );
$i = 0;
foreach($result as $key => $value) {
    $result[$i++] = &$result[$key];
}

var_dump($result);
echo '<br />';
$result[0] = '127.0.0.1:80';

var_dump($result);
echo '<br />';

but it's likely to prove problematic for you to work with.

The above code titbit demonstrates setting an initial array, then allocating a second key for each of the three values in the initial array. Then, the code modifies a single entry in the array, and demonstrates that the change is reflected in both key values;

EDIT

Having the two keys in one array will be cumbersome to work with if you want to use foreach(), curr(), and other control structures elements that manipulate your position in the array, because you'll pick up each entry twice.

It might be easier to work with two arrays, the second being a series of pointers to the first.

$result = array ( 6712  => '64.79.197.36:43444',
                  6711  => '64.79.194.56:41113',
                  6710  => '64.21.47.20:8080'
                );
$wrkResult = array();
foreach($result as $key => $value) {
    $wrkResult[] = &$result[$key];
}

var_dump($result);
echo '<br />';
var_dump($wrkResult);
echo '<br />';
$result[0] = '127.0.0.1:80';

var_dump($result);
echo '<br />';
var_dump($wrkResult);
echo '<br />';

Then you can work using either $result or $wrkResult, and any change to the values in one will be refelected in the other; you can loop through one or other and only get the correct number of results, not twice as many, etc.


Well, my PHP is a bit rusty, but let me crack my dusty knuckles on this one:

I would make a transformer function for this. It accepts the first array as its input (the one with "6712" et. al. as its key), and returns the second array as its output.

Sorry for any and all syntax errors, but here it goes:

function indexer ($array) {
  $result = array();
  $i = 0;
  foreach ($array as $key => $value) {
    $result[$i][$key] = $value;
    $i++;
  }
  return $result;
}

But! If you don't strictly need the two-dimensional array, you can iterate over an array without all that overhead. See the reset(), next() and prev() functions. In case you're interested in the key (which you probably are, in your case), see also the each() function.

Remember that each array has a stateful internal pointer. This means, if you pass around the array a lot, the state of the pointer will be remembered. So call reset() appropriately.


$keys = array_keys($array);
$values = array_values($array);

$easyArray = array();

foreach($keys as $keyId => $key) {
    $easyArray[$keyId] = array(
        'id'     =>  $key,
        'addr'   =>  $values[$keyId]
    );
}

Then you have an easy array to work with, indexed from 0 to number-of-rows -1, with the correct number of dimensions:

// get the id of element $i:
$easyArray[$i]['id']

// get the address of element $i
$easyArray[$i]['addr']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜