Array issue with echoing the resutls - php

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

Related

convert nested foreach tree into recursive php function

Need to make recursive function from the below code. tried multiple ways but failed. because I don't know how much depth will the tree have. depth may be varying according to the data.
$tot_terms = [];
foreach ($tree as $key => $val) {
$tot_terms[$val->depth][] = $val->tid;
if (!empty($val->children)) {
foreach ($val->children as $key1 => $val1) {
$tot_terms[$val1->depth][] = $val1->tid;
if ($val1->children) {
foreach ($val1->children as $key2 => $val2) {
$tot_terms[$val2->depth][] = $val2->tid;
if ($val2->children) {
foreach ($val2->children as $key3 => $val3) {
$tot_terms[$val3->depth][] = $val3->tid;
if ($val3->children) {
foreach ($val3->children as $key4 => $val4) {
$tot_terms[$val4->depth][] = $val4->tid;
if ($val4->children) {
foreach ($val4->children as $key5 => $val5) {
$tot_terms[$val5->depth][] = $val5->tid;
}
}
}
}
}
}
}
}
}
}
}
and the result should be like this
Array
(
[0] => Array
(
[0] => 20011
[1] => 19991
[2] => 20000
)
[1] => Array
(
[0] => 20010
[1] => 19995
[2] => 19994
[3] => 19990
[4] => 20005
[5] => 19985
[6] => 19999
[7] => 19998
)
[2] => Array
(
[0] => 20012
[1] => 20006
[2] => 19996
[3] => 19993
[4] => 19989
[5] => 19988
[6] => 20004
[7] => 19984
[8] => 20001
)
[3] => Array
(
[0] => 20007
[1] => 20009
[2] => 20008
[3] => 19992
[4] => 19987
[5] => 19986
[6] => 19983
[7] => 19982
[8] => 20003
[9] => 20002
)
[4] => Array
(
[0] => 19997
)
[5] => Array
(
[0] => 19981
)
)
Tried according to other post but it is failing. input $tree is something that is array and an object. will post in next comment if required as total body count is more than its expected
We can use a handler function to store data. Note that we are passing the array by reference.
function func( $item, &$data ) {
if ( is_empty( $item ) ) return;
if ( ! is_object( $item ) ) return;
foreach ($item as $key => $val) {
$data[$val->depth][] = $val->tid;
func( $val->children, $data );
}
}
function func_handler( $item ) {
$data = [];
func( $item, $data );
return $data;
}

how can iterate inner array of 7 and 51 in php

How can I get the values of 51 and 7 through iteration.
Array
(
[686] => Array
(
[51] => Array
(
[0] => 1483691174
[1] => 1483691174
)
[7] => Array
(
[0] => 1483691174
[1] => 1483691174
[2] => 1483691174
[3] => 1483691174
[4] => 1483691174
[5] => 1483691174
[6] => 1483691174
[7] => 1483691174
[8] => 1483691174
[9] => 1483691174
[10] => 1483691174
[11] => 1483691174
[12] => 1483691174
[13] => 1483691174
[14] => 1483691174
)
)
)
This function can be used to iterate any level of array with sub array:-
function iteratetor(){
static $cnt = 0;
$args = func_get_args();
if (!empty($args)) {
echo '<ol>';
foreach ($args as $k => $v) {
if(in_array($k,array('7','51'))){
$v = htmlspecialchars(print_r($v, true));
if ($v == '') {
$v = ' ';
}
echo '<li><pre>' . $v . "\n" . '</pre></li>';
}
}
echo '</ol>';
}
$cnt++;}
just expand the statament in_array($k,array('7','51')) to suite your spec
you need to loop through the inner array and to get there you need arrays inside 686. use foreach() to get this done.Try this: .
foreach($arr as $arrs){
foreach($arrs as $key=>$val){
echo "For:".$key."::::::::::<br>";
foreach($val as $k=>$value){
echo $k." =>".$value."<br>";
}
}
}
DEMO
Iterate it like this:
foreach($array[686] as $k => $v)
{
echo $k."\n";
var_dump($v);
}
You can try other way,
<?php
print_r(array_column($arr, 7));
print_r(array_column($arr, 51));
?>
You can check output here
array_column : Return the values from a single column in the input array

Split array into two arrays by value even or odd 5

I want to split the following array into odd and even array by e.g [SUBJECT_CODE] => 05
Array
(
[0] => Array
(
[0] => English
[subject_name] => English
[1] => E-I
[subject_abr] => E-I
[2] =>
[ENROL_NO] =>
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 01
[SUBJECT_CODE] => 01
)
[1] => Array
(
[0] => English
[subject_name] => English
[1] => E-II
[subject_abr] => E-II
[2] => 027-B/FMSGUK-2011
[ENROL_NO] => 027-B/FMSGUK-2011
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 02
[SUBJECT_CODE] => 02
)
[2] => Array
(
[0] => Urdu
[subject_name] => Urdu
[1] => U-I
[subject_abr] => U-I
[2] =>
[ENROL_NO] =>
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 05
[SUBJECT_CODE] => 05
)
)
For this, a simple foreach should suffice. Consider this example:
// $values is your original array
$new_values = array();
foreach ($values as $key => $value) {
if($value['SUBJECT_CODE'] & 1) {
$new_values['odd'][] = $value;
} else {
$new_values['even'][] = $value;
}
}
echo '<pre>';
print_r($new_values);
echo '</pre>';
Step 1: Traverse through the array
$odd = array();
$even = array();
foreach ($arr as $key => $value) {
if ($key % 2 == 0) {
$even[] = $value;
}
else {
$odd[] = $value;
}
}
}
The odd value are stored in the $odd array and even in the $even.
You can print_r($odd)

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

How to loop over associative array using foreach loop construct in following scenairo?

I've an array titled $rebate_by_product:
Array
(
[op] => preview
[id] =>
[form_submitted] => yes
[company_id] => 46
[1] => Array
(
[pack] => 10
[quantity] => 20
[volume] => 30
[units] => 9
[amount] => 40
[rebate_start_date] => 2014-05-01
[rebate_expiry_date] => 2014-05-05
[applicable_states] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[rebate_total_count] => 5000
[products] => Array
(
[1] => 9
[2] => 10
)
)
[2] => Array
(
[pack] => 50
[quantity] => 60
[volume] => 70
[units] => 10
[amount] => 80
[rebate_start_date] => 2014-05-06
[rebate_expiry_date] => 2014-05-10
[applicable_states] => Array
(
[0] => 14
[1] => 15
[2] => 16
)
[rebate_total_count] => 10000
[products] => Array
(
[1] => 11
[2] => 8
)
)
[3] => Array
(
[pack] => 100
[quantity] => 200
[volume] => 300
[units] => 7
[amount] => 400
[rebate_start_date] => 2014-05-21
[rebate_expiry_date] => 2014-05-30
[applicable_states] => Array
(
[0] => 26
[1] => 33
[2] => 42
)
[rebate_total_count] => 9999
[products] => Array
(
[1] => 9
[2] => 8
)
)
[multiselect] => 42
)
You can observe from above array that it has few elements which are not array but it has three such elements which are themselves array and even few of its data elements are also arrays so how to loop over this kind of array using foreach loop?
If you just want to print each one the just use foreach loop. Consider this example:
$product_keys = array(); // edited
// loop them, if its an array, loop inside it again
foreach($rebate_by_product as $index => $element) {
if(is_array($element)) {
foreach($element as $key => $value) {
if(is_array($value)) {
// EDITED
if($key == 'products') {
$product_keys = array_merge($product_keys, $value);
}
$value = implode(',', $value);
echo "$key => $value <br/>";
} else {
echo "$key => $value <br/>";
}
}
} else {
echo "$index => $element <br/>";
}
}
// if product items has duplicates check here (edited)
if(count($product_keys) != count(array_unique($product_keys))) {
echo "<script>alert('This array has duplicate products');</script>";
} else {
echo "<script>alert('Products are ok');</script>";
}
Or if you want, you cant just use iterators on this one:
$recursive = new RecursiveIteratorIterator(new RecursiveArrayIterator($rebate_by_product));
foreach($recursive as $key => $value) {
echo "$key => $value <br/>";
}
I'd propose you're use a recursive approach to bring all the entries of the array on the same level and then print this array:
function loopArray($inputVal,$inputKey = "") {
if(is_array($inputVal)) {
$output = array();
foreach($inputVal as $key => $value) {
$output = array_merge($output,loopArray($value,$key));
}
return $output;
} else {
return array($inputKey => $inputVal);
}
}
// Just for presenting:
$yourArray = array(
"1" => "1",
array(
"2.1" => "2.1",
array(
"2.2.1" => "2.2.1"
)
),
"3" => "3",
array(
"4.1" => "4.1"
)
);
$newArray = loopArray($yourArray);
// > array("1" => 1,"2.1" => "2.1","2.2.1" => "2.2.1","3" => "3","4.1" => "4.1")
foreach($newArray as $key => $value) {
echo $key." => ".$value."<br/>";
}
// > 1 => 1
// > 2.1 => 2.1
// > 2.2.1 => 2.2.1
// > 3 => 3
// > 4.1 => 4.1

Categories