在php中访问没有键值的json数据?

时间:2014-05-24 14:42:44

标签: php arrays json

 echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
 echo "<tr>";
 echo "<td align='center;' style='color:black;'>Rank</td>";
 echo "<td align='center;' style='color:black;'>Branch</td>";
 echo "</tr>";
$find = array_slice($_SESSION["data"], $rank - 2, 5);

foreach($find as $key => $element)
    {


    echo "<tr>";

    foreach($element as $subkey => $subelement)
        {
        echo $subelement;
        if ($subkey++ < 2)
            {


            if ($subkey == 1)
                {
                echo "<td align='center;' style='color:black;'>$subelement</td>";
                }
              else
                {
                echo "<td align='center;' style='color:black;'><a href='getdata.php?key=$key'>" . $subelement . "</a></td>";
                }
            }
        }

    echo "</tr>";
    }

echo "</table>";

我想使用一个密钥获取数据,在某种意义上,我所面临的问题是我的数据中没有密钥。我使用排名作为完整数据显示的关键,但在这种情况下我不能这样做,因为我的密钥是0到5而不是说7到12

我的$ find数组:

Array ( [0] => Array ( [0] => 7 [1] => KOTTAYAM TIEDAGENTSII [2] => V01393 - Venu M Nair [3] => R02208 - Reji Kumar [4] => 0.8854 [5] => 0.77570425252759 [6] => 1 [7] => 0.925 [8] => 0.9246 [9] => 1 [10] => 9.0926620166705 ) [1] => Array ( [0] => 8 [1] => PITAMPURA DLHI DIRECT [2] => C00405 - CHANDER MANI [3] => S00927 - Sachin Kumar [4] => 0.9478 [5] => 0.71204754186926 [6] => 0.94999999925494 [7] => 0.9845 [8] => 0.9692 [9] => 0.9400000013411 [10] => 9.0808534450678 ) [2] => Array ( [0] => 9 [1] => MUMBAI BHANDUP DIRECT [2] => B00841 - BHAVESH M KOTHARI [3] => A02234 - AMOL PRAKASH KANADE [4] => 0.9881 [5] => 1 [6] => 0.89000000059605 [7] => 0.9968 [8] => 1 [9] => 0.62000000476837 [10] => 9.0665850088513 ) [3] => Array ( [0] => 10 [1] => RATLAM DIRECT [2] => R01754 - Rajesh Joshi [3] => S03643 - Shishir Jain [4] => 1 [5] => 1 [6] => 0.74000000953674 [7] => 0.9779 [8] => 0.9902 [9] => 0.68999999761581 [10] => 8.9068650118017 ) [4] => Array ( [0] => 11 [1] => LUDHIANA DIRECT [2] => R02596 - RAKESH SHARMA [3] => J00753 - JAIPAL SINGH [4] => 1 [5] => 1 [6] => 0.72999998927116 [7] => 1 [8] => 0.9949 [9] => 0.65999999642372 [10] => 8.8850849763966 ) )

编辑:我的元素数组我想将7,8,9作为数组的键,以便我可以有效地使用它

  Array(
    [0] => 7[1] => KOTTAYAMTIEDAGENTSII[2] => V01393 - VenuMNair[3] => R02208 - RejiKumar[4] => 0.8854[5] => 0.77570425252759[6] => 1[7] => 0.925[8] => 0.9246[9] => 1[10] => 9.0926620166705
) Array(
    [0] => 8[1] => PITAMPURADLHIDIRECT[2] => C00405 - CHANDERMANI[3] => S00927 - SachinKumar[4] => 0.9478[5] => 0.71204754186926[6] => 0.94999999925494[7] => 0.9845[8] => 0.9692[9] => 0.9400000013411[10] => 9.0808534450678
) Array(
    [0] => 9[1] => MUMBAIBHANDUPDIRECT[2] => B00841 - BHAVESHMKOTHARI[3] => A02234 - AMOLPRAKASHKANADE[4] => 0.9881[5] => 1[6] => 0.89000000059605[7] => 0.9968[8] => 1[9] => 0.62000000476837[10] => 9.0665850088513
) Array(
    [0] => 10[1] => RATLAMDIRECT[2] => R01754 - RajeshJoshi[3] => S03643 - ShishirJain[4] => 1[5] => 1[6] => 0.74000000953674[7] => 0.9779[8] => 0.9902[9] => 0.68999999761581[10] => 8.9068650118017
) Array(
    [0] => 11[1] => LUDHIANADIRECT[2] => R02596 - RAKESHSHARMA[3] => J00753 - JAIPALSINGH[4] => 1[5] => 1[6] => 0.72999998927116[7] => 1[8] => 0.9949[9] => 0.65999999642372[10] => 8.8850849763966
)

2 个答案:

答案 0 :(得分:1)

      <?php

    $data=Array (Array (7,'KOTTAYAM TIEDAGENTSII'),
        Array (8,'PITAMPURA DLHI DIRECT '),
        Array (9,'PITAMPURA DLHI DIRECT1 '),
        Array (10,'PITAMPURA DLHI DIRECT2 '),
        Array (11,'PITAMPURA DLHI DIRECT3'),
       );
     print_r($data);
     echo "<br/><br/>";
    $formateddata;
    foreach ($data as $key => $value) {
         $tmp=$value[0];
        $formateddata[$tmp]=$value;
     }
  print_r($formateddata);

   echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup       span='2' width='50%'></colgroup>";
   echo "<tr>";
   echo "<td align='center;' style='color:black;'>Rank</td>";
  echo "<td align='center;' style='color:black;'>Branch</td>";
   echo "</tr>";
 //$find = array_slice(formateddata, $rank - 2, 5);

foreach($formateddata as $key => $element)
{


echo "<tr>";

foreach($element as $subkey => $subelement)
    {
    echo $subelement;
    if ($subkey++ < 2)
        {
        if ($subkey == 1)
            {
            echo "<td align='center;' style='color:black;'>$subelement</td>";
            }
          else
            {
            echo "<td align='center;' style='color:black;'><a href='getdata.php?
            key=$key'>" . $subelement . "</a></td>";
            }
        }
    }

      echo "</tr>";
    }

   echo "</table>";
?>

...复制完整代码,并在本地服务器上测试,希望你能得到你想要的输出

答案 1 :(得分:0)

我认为在你做一些操作之前,只需按照你想要的方式安排数据,就像你说的那样7到12 ..这就是解决方案,

<?php

 $data=Array (Array (7,'KOTTAYAM TIEDAGENTSII'),
        Array (8,'PITAMPURA DLHI DIRECT '),
        Array (9,'PITAMPURA DLHI DIRECT '),
        Array (10,'PITAMPURA DLHI DIRECT '),
        Array (11,'PITAMPURA DLHI DIRECT '),
    );
    print_r($data);
    echo "<br/><br/>";
    $formateddata;
    foreach ($data as $key => $value) {
              $tmp=$value[0];
              $formateddata[$tmp]=$value;
     }
    print_r($formateddata);
     ?>

您的会话$数据

Array ( 
         [0] => Array ( 
               [0] => 7 
               [1] => KOTTAYAM TIEDAGENTSII ) 
         [1] => Array ( 
               [0] => 8 
               [1] => PITAMPURA DLHI DIRECT )
      ) 

现在可以根据需要重新排列$ data。对于$ formateddata中的这个例子

Array ( 
       [7] => Array ( 
                   [0] => 7 
                   [1] => KOTTAYAM TIEDAGENTSII ) 
       [8] => Array ( 
                   [0] => 8 
                   [1] => PITAMPURA DLHI DIRECT 
     ) 

- 希望你现在得到你想要的数组?