1d数组并通过索引插入以生成arraylist

时间:2015-11-06 12:45:26

标签: php arrays

你好任何人都可以帮助我 我有一个主要类例如城市的输入 雅加达,泗水,棉兰,巴厘岛保存变量 示例首先将值雅加达数组。并列出雅加达,泗水,棉兰和巴厘岛 和下一个阵列像泗水和列表雅加达,泗水,棉兰,巴厘岛

从这个我希望在城市间建立一个距离并制作一个数组2多维
你能给我任何想法吗?

mainclass.php

    $test = 'Jakarta';
    $test1 = 'Brebes';
    $test2='Pekalongan';
    $test3 ='Semarang';

    $simpankota = array($test,$test1,$test2,$test3);
    $totalkota=array($simpankota);
    $jarakd2->probabilitasTotal ($totalkota);

功能如

   public function probabilitasTotal($totalkota)
   {
      echo "<pre>" . print_r ($totalkota,true) ."</pre>";
      if ($totalkota != null)
      {
         foreach($totalkota as $key => $value)
            {
               foreach($value as $k => $v)
               {
                   $a=array($v);
                  echo "<pre>" .print_r($a ,true)."</pre>";
               }

            }
            $save[] = array_values($totalkota,array_values($a));
            $koplo[]= array($totalkota ,array_values($a));

                  echo "<pre>" .print_r($koplo ,true)."</pre>";
      }

   }

但我看到了

Array
(
[0] => Array
    (
        [0] => Jakarta
        [1] => Brebes
        [2] => Pekalongan
        [3] => Semarang
    )

)
Array
(
    [0] => Jakarta
)
Array
(
    [0] => Brebes
)
Array
(
    [0] => Pekalongan
)
Array
(
    [0] => Semarang
)

从那里我想做一个输入并生成自动从输入

制作一个列表
Array
(
[0] => Array
    (
        [0] => Jakarta
              (
                  [0] => Jakarta
                  [1] => Brebes
                  [2] => Pekalongan
                  [3] => Semarang
              )

        [1] => Brebes
             (
                  [0] => Jakarta
                  [1] => Brebes
                  [2] => Pekalongan
                  [3] => Semarang
              )
        [2] => Pekalongan
               (
                  [0] => Jakarta
                  [1] => Brebes
                  [2] => Pekalongan
                  [3] => Semarang
              )
        [3] => Semarang
               (
                  [0] => Jakarta
                  [1] => Brebes
                  [2] => Pekalongan
                  [3] => Semarang
              )
    )

)

所以,如果我访问距离城市三宝垄 - 布雷布我只喜欢$ cityindex [3] [1]

抱歉英语不好:)

2 个答案:

答案 0 :(得分:0)

不确定这是否足够,但我从你的例子中做到了......

$test  = 'Jakarta';
$test1 = 'Brebes';
$test2 = 'Pekalongan';
$test3 = 'Semarang';

$simpankota = array($test,$test1,$test2,$test3);

probabilitasTotal($simpankota);

function probabilitasTotal($simpankota) {
    echo "<pre>" . print_r($simpankota, true) . "</pre>";
    $simpankota2 = $simpankota;
    $a = array();
    foreach ($simpankota as $key => $value) {
        $array = array();
        foreach ($simpankota2 as $k => $v) {
            $array[] = $value . ' - ' . $v;
        }
        $a[$key] = $array;
    }
    echo "<pre>" . print_r($a, true) . "</pre>";
}

它输出了以下内容......

Array
(
    [0] => Jakarta
    [1] => Brebes
    [2] => Pekalongan
    [3] => Semarang
)

Array
(
    [0] => Array
        (
            [0] => Jakarta - Jakarta
            [1] => Jakarta - Brebes
            [2] => Jakarta - Pekalongan
            [3] => Jakarta - Semarang
        )

    [1] => Array
        (
            [0] => Brebes - Jakarta
            [1] => Brebes - Brebes
            [2] => Brebes - Pekalongan
            [3] => Brebes - Semarang
        )

    [2] => Array
        (
            [0] => Pekalongan - Jakarta
            [1] => Pekalongan - Brebes
            [2] => Pekalongan - Pekalongan
            [3] => Pekalongan - Semarang
        )

    [3] => Array
        (
            [0] => Semarang - Jakarta
            [1] => Semarang - Brebes
            [2] => Semarang - Pekalongan
            [3] => Semarang - Semarang
        )

)

答案 1 :(得分:0)

试试这个:

   public function probabilitasTotal($totalkota) {
        echo "<pre>" . print_r($totalkota, true) . "</pre>";
        if ($totalkota != null) {
            foreach ($totalkota as $key => $value) {
                foreach ($value as $k => $v) {
                    $koplo[$v] = $value;
                }
            }

            print_r($koplo, true);
        }
    }

由于Brebes不能成为Array名称,因此您无法获得所期望的内容。它将拼写为Array