0

我有一个具有这种结构的数组:

Array
(
    [mysite] => Array
        (
            [0] => Array
                (
                    [0] => uniqueid
                    [1] => brand
                    [2] => horsepower
                    [3] => topspeed
                )

            [1] => Array
                (
                    [0] => uniqueid
                    [1] => brand
                    [2] => horsepower
                    [3] => topspeed
                )

            [2] => Array
                (
                    [0] => uniqueid
                    [1] => brand
                    [2] => horsepower
                    [3] => topspeed
                )
        )
)

每辆车都有一个“独特的”,然后是品牌、马力等。

如果我想获取随机汽车的信息,我会这样做:

$rkey = array_rand($sites['mysite'], 1); // get a random key
$car_info = $myarray['mysite'][$rkey];

您对如何使用某个“唯一标识”获取信息有任何想法吗?

$car_info = "get the information of a car with a certain uniqueid";

泰!

4

3 回答 3

0

尝试,

在 for 循环中传递唯一的 id 你会得到想要的结果它可能会帮助你,

$array = array(

  0   => Array
            (
                0 => 'uniqueid0',
                1 => 'brand',
                2 => 'horsepower',
                3 => 'topspeed',
            ),
  1   => Array
            (
                0 => 'uniqueid1',
                1 => 'brand',
                2 => 'horsepower',
                3 => 'topspeed',
            ),
  2  => Array
            (
                0 => 'uniqueid2',
                1 => 'brand',
                2 => 'horsepower',
                3 => 'topspeed',
            ),
  3  => Array
            (
                0 => 'uniqueid3',
                1 => 'brand',
                2 => 'horsepower',
                3 => 'topspeed',
            ),
);


  foreach($array as $arr){
    if($arr[0] == 'uniqueid2'){
      $result = $arr;
      break;
    }
  }

print_r($result);

输出:

数组([0] => uniqueid2 [1] => 品牌 [2] => 马力 [3] => 最高速度)

于 2012-12-07T06:19:10.713 回答
0
$uniqueid = 12345;

foreach ($myarray['mysite'] as $car) {
    if($car[0] == $uniqueid) {
        $car_info = $car;
        break;
    }
}
于 2012-12-07T06:20:00.340 回答
0

如果 uniqueid 在汽车中是唯一的,那么您可以尝试按如下方式重组数组

如果可能的话

Array (
    [mysite] => Array
    (
        [uniqueid1] => Array
            (
                [0] => uniqueid1
                [1] => brand
                [2] => horsepower
                [3] => topspeed
            )

        [uniqueid2] => Array
            (
                [0] => uniqueid2
                [1] => brand
                [2] => horsepower
                [3] => topspeed
            )

        [uniqueid3] => Array
            (
                [0] => uniqueid3
                [1] => brand
                [2] => horsepower
                [3] => topspeed
            )
    )
)

这样,您可以直接访问汽车以获取唯一 ID,也可以应用随机逻辑

如果不是@Jevgeni Bogatyrjov 解决方案效果很好

foreach ($myarray['mysite'] as $car) {
    if($car[0] == $uniqueid) {
        $car_info = $car;
        break;
    }
}
于 2012-12-07T06:35:11.737 回答