vini
vini

Reputation: 4742

Access json data without key value in php?

 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>";

I want to get the data using a key which in the sense is the rank the issue i am facing is i don't have a key in my data. I am using the rank as a key for full data display however in this case i cannot do that as my key is coming as 0 to 5 rather than say 7 to 12

My $find Array:

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 ) )

EDIT: My element array i want to make 7, 8 , 9 as the keys of the array such that i can use it effectively

  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
)

Upvotes: 1

Views: 487

Answers (2)

Chinmoy
Chinmoy

Reputation: 318

      <?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>";
?>

...copy the full code , and test in ur local server , hope u'll get the output as u wanted

Upvotes: 1

Chinmoy
Chinmoy

Reputation: 318

I think before u do some operation just arrange the data the way you want , as u say like 7 to 12 .. here is the solution ,

<?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);
     ?>

You session $data coming as

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

now rearrange the $data as you like . For this example in $formateddata

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

--hope now you got the array as u wanted ?

Upvotes: 0

Related Questions