想象一下,我有一个数据数组。其中的每个项目都是另一个包含'id','link'和'display'的数组:
$travel = array(
array(
'id' => 1,
'link' => 'bus',
'display' => 'bus'
),
array(
'id' => 2,
'link' => 'bike',
'display' => 'bicycle'
),
array(
'id' => 3,
'link' => 'cart',
'display' => 'horse and cart'
)
);
要按已知ID($id
)显示'display',我需要执行以下操作:
foreach($travel as $t) {
if($t['id'] == $id) {
echo $t['display'];
break; // uuurgh, nasty...
}
}
同样,要通过已知链接($link
)显示“显示”,我需要这样做:
foreach($travel as $t) {
if($t['link'] == $link) {
echo $t['display'];
break;
}
}
为了提高效率,我可以创建两个相似的数组:
$travelID = array(
1 => array(
'link' => 'bus',
'display' => 'bus'
),
2 => array(
'link' => 'bike',
'display' => 'bicycle'
),
3 => array(
'link' => 'cart',
'display' => 'horse and cart'
)
);
$travelLink = array(
'bus' => array(
'id' => 1,
'display' => 'bus'
),
'bike' => array(
'id' => 2,
'display' => 'bicycle'
),
'cart' => array(
'id' => 3,
'display' => 'horse and cart'
)
);
然后执行以下操作:
echo $travelID[$id]['display'];
echo $travelLink[$link]['display'];
但是有没有办法将所有数据保持在一起,同时还能够简单/快速地引用它?
答案 0 :(得分:2)
$keys = array('id', 'link');
$multiple_key_array = new MultipleKeyArray($keys);
$data_1 = array('id' => 1, 'link' => 'bus', 'display' => 'bus');
$multiple_key_array->addData($data_1);
$data_2 = array('id' => 2, 'link' => 'bike', 'display' => 'bicycle');
$multiple_key_array->addData($data_2);
$data_3 = array('id' => 3, 'link' => 'cart', 'display' => 'horse and cart');
$multiple_key_array->addData($data_3);
var_dump($multiple_key_array->getData('id', 1));
class MultipleKeyArray{
private $data_array = array();
private $key_array = array();
private $map_array = array();
public function __construct($key_array){
$this->key_array = $key_array;
}
public function addData($data_array){
$index = count($this->data_array);
$this->data_array[] = $data_array;
foreach($this->key_array as $key){
if(array_key_exists($key, $data_array)){
$this->map_array[$key][$data_array[$key]] = $index;
}
}
}
public function getData($key, $value){
$index = $this->map_array[$key][$value];
return $this->data_array[$index];
}
}
答案 1 :(得分:0)
没有办法,你可以只创建一个肝,例如array_find_by(&$array);
在哪里放置所有必要的代码,以便不复制它。
答案 2 :(得分:0)
您可以创建“查找”哈希映射,几乎就像数据库中的索引一样,简单示例:
<?php
$travel = getData();
$idx_id = createIndex($travel, 'id');
$idx_id[2]['foo'] = 'bar';
var_dump($travel);
function createIndex(&$arr, $elementName) {
$rv = array();
foreach( $arr as &$entry ) {
if ( isset($entry[$elementName]) ) {
$rv[ $entry[$elementName] ] = &$entry;
}
}
return $rv;
}
function getData() {
return array(
array(
'id' => 1,
'link' => 'bus',
'display' => 'bus'
),
array(
'id' => 2,
'link' => 'bike',
'display' => 'bicycle'
),
array(
'id' => 3,
'link' => 'cart',
'display' => 'horse and cart'
)
);
}
打印
array(3) {
[0]=>
&array(3) {
["id"]=>
int(1)
["link"]=>
string(3) "bus"
["display"]=>
string(3) "bus"
}
[1]=>
&array(4) {
["id"]=>
int(2)
["link"]=>
string(4) "bike"
["display"]=>
string(7) "bicycle"
["foo"]=>
string(3) "bar"
}
[2]=>
&array(3) {
["id"]=>
int(3)
["link"]=>
string(4) "cart"
["display"]=>
string(14) "horse and cart"
}
}