how can iterate inner array of 7 and 51 in php - 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

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

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

Find Unique Value from a array

I have a Multidimensional array, I need to find if array have same value of 'brand' attribute then return its id.
I tried via some array functions but it didn't work.
What I Tried:
1)
$backwards = array_reverse($attribute);
echo '<pre>';
$last_item = NULL;
$i = 0;
foreach ($backwards as $current_item) {
if ($last_item === $current_item[$i]['value']) {
echo '<pre>'; print_r($current_item[$i]['value']);
}
$last_item = $current_item[$i]['value'];
echo '<pre>'; print_r($last_item);
$i++;
}
2)
$j = 1;
$i = 0;
foreach ($attributeValues as $attributeData) {
foreach ($attribute as $value) {
if($value[$i]['value'] == $value[$j]['value']) {
echo '<pre>'; print_r($value); die();
}
$j++;
}
}
All my solution's not worked, please help.
[0] => Array
(
[0] => Array
(
[name] => brand
[value] => 54
[id] => 5251
[price] => 15000.0000
)
[1] => Array
(
[name] => model
[value] => 1200
[id] => 5251
[price] => 15000.0000
)
)
[1] => Array
(
[0] => Array
(
[name] => brand
[value] => 54
[id] => 5250
[price] => 15000.0000
)
[1] => Array
(
[name] => model
[value] => 1200
[id] => 5250
[price] => 12000.0000
)
)
[2] => Array
(
[0] => Array
(
[name] => brand
[value] => 89
[id] => 518
[price] => 100.0000
)
[1] => Array
(
[name] => model
[value] => 12
[id] => 518
[price] => 100
)
)
If [name]=>brand and [name]=>model value's of first array is same as second array's value then return [id].
You need two for loop.
$result =[];
foreach ($arr as $key => $value) {
foreach($value as $v){
$result[$v['name']][] = $v['id'];
}
}
$result = array_map("array_unique", $result); // to make it unique
print_r($result);
// if you want to check ids for brand
//print_r($result['brand']);
Output:
Array
(
[brand] => Array
(
[0] => 5251
[1] => 5250
[3] => 518
)
[model] => Array
(
[0] => 5251
[1] => 518
)
)
Demo.
EDIT
Then you can group it by name and value of it
$result =[];
foreach ($arr as $key => $value) {
foreach($value as $v){
$result[$v['name']."|".$v['value']][] = $v['id'];
}
}
$result = array_map("array_unique", $result);
print_r($result);die;
Demo.
you can use foreach and iterate through the array
$res = [];
foreach($arr as $k => $v){
if($v[0]['name'] == $v[1]['name'])
$res[$v[0]['name']] = $v[0]['id'];
}
If you want to match the index value try this
foreach($arr as $k => $v){
if($v[0]['value'] == $v[1]['value'])
$res[] = $v[0]['id'];
}
Working example

find the minimum and maximum value in array group php

Array
(
[0] => Array
(
[package] => LTE_15AGB
[value] => Array
(
[0] => 52690
[1] => 24700
[2] => 43972
[3] => 506417
[4] => 488125
[5] => 935918
[6] => 1322816
[7] => 1189040
[8] => 2805279
[9] => 2764825
[10] => 1688294
[11] => 1228812
[12] => 2232345
[13] => 3356143
[14] => 1193213
[15] => 167589
[16] => 1373104
[17] => 691411
[18] => 1398647
[19] => 5
)
)
[1] => Array
(
[package] => LTE_15AGB_NT
[value] => Array
(
[0] => 953370
[1] => 151168
[2] => 37605
[3] => 428769
[4] => 755222
[5] => 1146719
[6] => 494289
[7] => 889002
[8] => 307200
[9] => 127972
[10] => 2764815
[11] => 1426224
[12] => 262669
[13] => 648757
[14] => 1485
[15] => 1202
[16] => 998
[17] => 1
)
)
)
This is what I have tried:
$tmp = array();
foreach($arrayName as $arg){
$tmp[$arg['package']][] = $arg['value'];
}
$output = array();
foreach($tmp as $type => $labels){
$output[] = array( 'package' => $type, 'value' => $labels, );
}
print_r(($output))
Try this:
foreach ($your_array as $subarr) {
echo $subarr[package]." minimum = ";
echo min($subarr[value])." and maximum = ";
echo max($subarr[value])."<br>";
}
this will output each package name together with the minimum and maximum values.
Simply use the min() and max() functions.
Your code:
foreach(array_column($array, 'value') as $key => $values){
echo PHP_EOL . 'SubArray '. $key .' min = '. min($values) . ' and max value = '. max($values);
}
output is:
SubArray 0 min = 5 and max value = 3356143
SubArray 1 min = 1 and max value = 2764815
References:
PHP min() manual
PHP max()) manual
PHP array_column
manual
Live demo: https://eval.in/941702
#Arebhy Sri, You should search about array in php, It's like basic problem.
$finalArray = [];
foreach ($mainArr as $subArr){
$array = $subArr['value'];
sort($array);
$tempArray['minimum'] = reset($array);
$tempArray['maximum'] = end($array);
$tempArray['package'] = $subArr['package'];
$finalArray[] = $tempArray;
}
$finalArray //you can use
I am using simple foreach and two functions of array reset() and end().
reset(): Returns the value of the first array element, or FALSE if the array is empty.
end(): Returns the value of the last element or FALSE for empty array.

I want only values of 1st index then 2nd and then 3rd index. How can I iterate through this with a loop and get the values dynamically?

I want the values from this array for each index. I tried to access the values using foreach but I can´t figure out how to get all values for each index (i.e. for 1st, 2nd, 3rd)
Array
(
[1] => Array
(
[0] => 244
[1] => 6014
[2] => 5067
[3] => 5059
[4] => 1726
[5] => 56
[6] => 1234
[7] => 56
)
[2] => Array
(
[0] => 122
[1] => 123
[2] => 56
[3] => 246
[4] => 7360
[5] => 8058
[6] => 1290
[7] => 1234
[8] => 5454
)
[3] => Array
(
[0] => 7890
[1] => 5454
)
)
Please try this. hopefully It will help u.
foreach ($array as $currencyId => $subArray){
echo $currencyId . "<br>";
foreach ($subArray as $value) {
echo $value . "<br>";
}
}
Follow this sample I made, if you still don't get it, leave a comment.
$array = array(
array('abc','def','xyz'),
array('asd','fun','bad'),
array('blue','red','green')
);
foreach($array as $key=>$value){
foreach($value as $subkey=>$subvalue) {
echo $subkey . " : " . $subvalue . "<br>";
}
}
If you want to get the currency pair key also, use this example:
$assoc_array = array(
"eurusd" => array('abc','def','xyz'),
"usdjpy" => array('good','bad','ugly'),
"aususd" => array('blue','red','green')
);
foreach($assoc_array as $key=>$value){
echo $key . ":: <br>";
foreach($value as $subkey=>$subvalue) {
echo $subkey . " : " . $subvalue . "<br>";
}
}
I want to show result this way

Categories