Access json data without key value in php? - 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
)

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

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 ?

Related

Array issue with echoing the resutls

This is my parsed array. I parse some elements and placed them into my array $results. I think i am somewhere wrong cause i want to echo everything speacially the array [display] but i can't
Array
(
[Forologiki_Periodos] => 1ος Μήνας 2020
[Hmerologiaki_periodos] => 01/01/2020 - 31/01/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/01/2020
[Hmerologiaki_periodos_ews] => 31/01/2020
[display] => Array
(
[0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
[1] => vatF2
[2] => 2020
[3] => oneMonth
[4] => 01/01/2020
[5] => 31/01/2020
[6] => 01/01/2020
[7] => 31/01/2020
)
)
Array
(
[Forologiki_Periodos] => 2ος Μήνας 2020
[Hmerologiaki_periodos] => 01/02/2020 - 29/02/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/02/2020
[Hmerologiaki_periodos_ews] => 29/02/2020
[display] => Array
(
[0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
[1] => vatF2
[2] => 2020
[3] => oneMonth
[4] => 01/02/2020
[5] => 29/02/2020
[6] => 01/02/2020
[7] => 29/02/2020
)
)
Array
(
[Forologiki_Periodos] => 3ος Μήνας 2020
[Hmerologiaki_periodos] => 01/03/2020 - 31/03/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/03/2020
[Hmerologiaki_periodos_ews] => 31/03/2020
[display] => Array
(
[0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
[1] => vatF2
[2] => 2020
[3] => oneMonth
[4] => 01/03/2020
[5] => 31/03/2020
[6] => 01/03/2020
[7] => 31/03/2020
)
)
foreach ($result as $value){
echo $value;
}
With this foreach i echo only the last element of $value, it should be echo all the 12 values. Also how can i get into the array [display] and echo values ?
You can loop directy on the display as
foreach ($result['display'] as $display) {
echo $display . '<br>';
}
But you can imbricate 2 loops
foreach ($result as $value) {
if (is_array($value)) {
foreach ($value as $display) {
echo '----' . $display . '<br>';
}
} else {
echo $value . '<br>';
}
}
The best solution is to use a function:
function displayArray($array)
{
foreach ($array as $each) {
if (is_array($each)) {
displayArray($each);
}
echo $each . '<br>';
}
}
So you can call the function like this:
displayArray($result);
Pls try this:
foreach (#$result['display'] as $key=>$value){
echo $value;
}

Getting individual values from multidimensional array

PHP :
<?php
print_r($details);
?>
Hi friends i'm printing the value of $details in php code and the out put i got in browser is as follows.
Array (
[0] =>
Array ( [item_id] => 8 [row] => [merchant_id] => 1 [discount] => [currentController] => store [price] => 60|Large [qty] => 1 [notes] => [cooking_ref] => Cooking reference 1
[ingredients] => Array ( [0] => Ingredients 1 ) [require_addon_5] => 2
[sub_item] => Array (
[5] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
[6] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
[7] => Array ( [0] => 2|10|Addon Item 1|right [1] => 3|20|Addon Item 2|right )
)
[addon_qty] => Array (
[5] => Array ( [0] => 1 [1] => 1 )
[6] => Array ( [0] => 1 [1] => 1 )
[7] => Array ( [0] => 1 [1] => 1 )
)
[require_addon_6] => 2
[require_addon_7] => 2
[two_flavors] =>
[non_taxable] => 2
[addon_ids] => Array ( [0] => 2 [1] => 3 [2] => 2 [3] => 3 [4] => 2 [5] => 3 )
)
)
Now I've to get the values of 'item_id' , 'cooking_ref' , sub_item and addon_qty as an individual array . Now I would like to know how can we access these values . I have tried something like
<?php
print_r($details);
echo "item_id : ".$details[0]['item_id']" <br />"
echo "price : ".$details[0]['price']" <br />"
echo "cooking_ref : ".$details[0]['cooking_ref']" <br />"
echo "cooking_ref : ".$details[0]['cooking_ref']" <br />"
echo "ingredients : "" <br />";
foreach($details['sub_item'] as $sub_item)
{
print_r($sub_item);
echo "<br /><br />";
$sub_item_array_val +=1;
}
?>
But didnt workedout for me could any one please suggest me how to acces these values from the above array , thanks in advance
Try this:
$details = // your array;
foreach($details as $detail)
{
echo $detail['item_id']; // will return 8
echo $detail['cooking_ref']; // will return Cooking reference 1
}
Foreach should be
foreach($details[0]['sub_item'] as $sub_item)
{
print_r($sub_item);
echo "<br /><br />";
}
Here is one possible approach-
foreach($details as $detail)
{
echo "item_id: ".$detail['item_id']."<br/>";
echo "price: ".$detail['price']."<br/>";
echo "cooking_ref: ".$detail['cooking_ref']."<br/>";
foreach($detail[$sub_item] as $sub_item)
{
echo "ingredients: <br/>";
print_r($sub_item);
echo "<br/><br/>";
}
foreach($detail[$addon_qty] as $addon_qty)
{
echo "ingredients: <br/>";
print_r($addon_qty);
echo "<br/><br/>";
}
}

Multidimensional Array Listing Printing Php

I have following array with php code
I could not find where I am mistaken
I'm trying to filter some of these array results and delete them. When I try to list them I could not succeded
array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
4 => 'kabeduvarkad.jpg',
5 => 'kabeduvarkad1-1000x288.jpg',
6 => 'kabeduvarkad1-1024x768.jpg',
7 => 'kabeduvarkad1-150x150.jpg',
8 => 'kabeduvarkad1-300x225.jpg',
9 => 'kabeduvarkad1-400x300.jpg',
10 => 'kabeduvarkad1.jpg',
11 => 'kabeduvarkad2-1000x288.jpg',
12 => 'kabeduvarkad2-1024x768.jpg',
13 => 'kabeduvarkad2-150x150.jpg',
14 => 'kabeduvarkad2-300x225.jpg',
15 => 'kabeduvarkad2-400x300.jpg',
16 => 'kabeduvarkad2.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
)
This is my function
function listArrayRecursive($array_name, $ident = ''){
$result = array();
foreach ($array_name as $k => $v){
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
}else{
$result[] = $ident. '/' . $v . '<br>';
}
}
return $result;
}
I have following result
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[1] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[2] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[3] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad2-1000x288.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad2-1024x768.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad2-150x150.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad2-300x225.jpg<br>
[15] => /wp-content/uploads/2013/05/kabeduvarkad2-400x300.jpg<br>
[16] => /wp-content/uploads/2013/05/kabeduvarkad2.jpg<br>
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
)
)
)
)
[5] => Array
(
[0] => /wp-update/wp-update.tar<br>
[1] => /wp-update/wp-update.tar.gz<br>
[2] => /wp-update/wp-update1.tar<br>
[3] => /wp-update/wp-update1.tar.gz<br>
)
[6] => /wp-update.tar.gz<br>
)
Expected Result is
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
...
[110] => /wp-update/wp-update.tar<br>
[111] => /wp-update/wp-update.tar.gz<br>
[112] => /wp-update/wp-update1.tar<br>
[113] => /wp-update/wp-update1.tar.gz<br>
[114] => /wp-update.tar.gz<br>
)
You can do it like this:
<?php
// Dummy data source
$data = array(
'/do-update.php',
'/sitemap.xml',
'/sitemap.xml.gz',
'/wp-config.php',
array(
array(
array(
'/wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>'
)
)
)
);
// Helper function
function getFiles($data, &$fileList) {
foreach ($data as $dataItem) {
if (is_array($dataItem))
getFiles($dataItem, $fileList);
else
$fileList[] = $dataItem;
}
}
// Debug
echo "<b>Orignal Array</b>";
var_dump($data);
echo "<hr>";
// Helper function usage
echo "<b>Parsed Array</b>";
$fileList = array();
getFiles($data, $fileList);
var_dump($fileList);
?>
Output:
Ok use this function its working
$all=array();
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $v) {
$all[]=$v;
}
print_r($all);
With $var[] = you basically say that you want to add a new Element to $var with an incremented key.
Your Recursive frunction returns an array.
So this array is assigned as a new element in your array.
But what you want is a flat array.
Instead of adding an array to your array like this:
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
Merge the existing arrays like this:
if (is_array($v)){
$tmpResult = listArrayRecursive($v, $ident.'/'.$k);
$result = array_merge($result, $tmpResult);
You can see a working example here.

Can't access multi dimensional associative arrays

I have the following code:
foreach ($cardSuits as $cardSuit) {
$keyCardValues = array_keys($cardValues);
foreach ($keyCardValues as $cardValue) {
$deck[] = array( "cardValue" => $cardValue, "cardSuit" => $cardSuit);
shuffle($deck);
}
}
if ($deal == "Deal") {
shuffle($deck);
$cards1 = array_shift($deck);
$_SESSION['value'][] = $cards1;
I've tried:
echo "<br />" . $_SESSION['value']['cardValue'];
But it's giving me an undefined index error. However, if I do a print_r, it works fine..
How do I echo it so the session can give me the $cardValue in the array?
Thanks
edit for print_r:
Array ( [value] => Array ( [0] => Array ( [cardValue] => nine [cardSuit] => hearts ) ) [cards] => Array ( [0] => 9 [1] => 2 [2] => 10 [3] => 4 [4] => 3 [5] => 10 [6] => 5 [7] => 2 [8] => 10 [9] => 5 ) )
EDIT for echo print_r:
Array ( [value] => Array ( [0] => Array ( [cardValue] => nine [cardSuit] => diamonds ) ) [cards] => Array ( [0] => 9 [1] => 3 [2] => 7 [3] => 10 [4] => 9 [5] => 11 [6] => 7 [7] => 10 [8] => 10 [9] => 5 ) )
try
echo "<br />" . $_SESSION['value'][0]['cardValue'];
Since your $cards1 is an array and that you are assigning this array to $_SESSION['value'][], you want to access cardValue using the following:
echo "<br />" . $_SESSION['value'][0]['cardValue'];
Use this
echo "<br />" . $_SESSION['value'][0]['cardValue'];
Array value is inside the index 0

help to optimize a function in php

i have created a function in php to convert an string like:
[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 20110924, [20110926, 20110927], 20110929]
to php array like:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
[8] => Array
(
[0] => 20111001
[1] => 20111002
)
[9] => 20111004
[10] => Array
(
[0] => 20111006
[1] => 20111007
)
)
The function is:
function dates2Array($d){
if($d!==''){
$d=substr($d, 1, strlen($d)-2);
$d=explode(', ', $d);
$dates=array();
if(!empty($d)){
$j=1;
foreach($d as $k=>$v){
if(substr($v, 0, 1)==='[') $dates[]=array(substr($v, 1, strlen($v)));
elseif(substr($v, strlen($v)-1, strlen($v))===']'){
$dates[$k-$j][1]=substr($v, 0, strlen($v)-1);
$j++;
}
else $dates[]=$v;
}
}
}
return $d!==''?$dates:'';
}
I am not fully happy with my function.
I think it can be more optimized and compressed for speed..
Can it be?
Use JSON (json_decode() and json_encode()) instead
http://sandbox.phpcode.eu/g/b3814/2
result:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
)
You can pass your string in {...} and pass it to json_decode(str, true) to get back an array.
>> json_decode("[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 2
0110924, [20110926, 20110927], 20110929]")
array (
0 => 20110911,
1 => 20110913,
2 =>
array (
0 => 20110915,
1 => 20110918,
),
3 => 20110920,
4 => 20110922,
5 => 20110924,
6 =>
array (
0 => 20110926,
1 => 20110927,
),
7 => 20110929,
)

Categories