How can I get a specific item from a multidimensional array (php)? - php

How can I get the specific item from the following array. I'd like to get the close values.
What should I do in this case?
$data = json_decode(file_get_contents($req_url),true);
// print_r($data); //give the following array;
// Array ( [chart] => Array ( [result] => Array ( [0] => Array ( [meta] => Array ( [currency] => USD [symbol] => SEAS [exchangeName] => NYQ [instrumentType] => EQUITY [firstTradeDate] => 1366378200 [regularMarketTime] => 1588622401 [gmtoffset] => -14400 [timezone] => EDT [exchangeTimezoneName] => America/New_York [regularMarketPrice] => 14.55 [chartPreviousClose] => 14.11 [previousClose] => 14.11 [scale] => 3 [priceHint] => 2 [currentTradingPeriod] => Array ( [pre] => Array ( [timezone] => EDT [end] => 1588599000 [start] => 1588579200 [gmtoffset] => -14400 ) [regular] => Array ( [timezone] => EDT [end] => 1588622400 [start] => 1588599000 [gmtoffset] => -14400 ) [post] => Array ( [timezone] => EDT [end] => 1588636800 [start] => 1588622400 [gmtoffset] => -14400 ) ) [tradingPeriods] => Array ( [0] => Array ( [0] => Array ( [timezone] => EDT [end] => 1588622400 [start] => 1588599000 [gmtoffset] => -14400 ) ) ) [dataGranularity] => 5m [range] => 1d [validRanges] => Array ( [0] => 1d [1] => 5d [2] => 1mo [3] => 3mo [4] => 6mo [5] => 1y [6] => 2y [7] => 5y [8] => 10y [9] => ytd [10] => max ) ) [timestamp] => Array ( [0] => 1588599000 [1] => 1588599300 [2] => 1588599600 [3] => 1588599900 [4] => 1588600200 [5] => 1588600500 ) [indicators] => Array ( [quote] => Array ( [0] => Array ( [open] => Array ( [0] => 13.3599996567 [1] => 13.8599996567 [2] => 13.720000267 [3] => 13.8800001144 [4] => 14.0349998474 [5] => 14.051199913 ) [close] => Array ( [0] => 13.9499998093 [1] => 13.7299995422 [2] => 13.8400001526 [3] => 14.0450000763 [4] => 14.0600004196 [5] => 14.1899995804 ) [high] => Array ( [0] => 14 [1] => 13.8599996567 [2] => 13.9399995804 [3] => 14.0900001526 [4] => 14.1000003815 [5] => 14.2600002289 ) [low] => Array ( [0] => 13.1099996567 [1] => 13.5900001526 [2] => 13.720000267 [3] => 13.8699998856 [4] => 13.8509998322 [5] => 14 ) [volume] => Array ( [0] => 124733 [1] => 49489 [2] => 51657 [3] => 48362 [4] => 34055 [5] => 36566 ) ) ) ) ) ) [error] => ) );
$close= $data['chart']['result']['indicators']['close'] ;
echo $close;

For your Array:
Array ( [chart] =>
Array ( [result] =>
Array ( [0] =>
Array ( [meta] =>
Array ( [currency] => USD [symbol] => SEAS [exchangeName] => NYQ [instrumentType] => EQUITY [firstTradeDate] => 1366378200 [regularMarketTime] => 1588622401 [gmtoffset] => -14400 [timezone] => EDT [exchangeTimezoneName] => America/New_York [regularMarketPrice] => 14.55 [chartPreviousClose] => 14.11 [previousClose] => 14.11 [scale] => 3 [priceHint] => 2 [currentTradingPeriod] =>
Array ( [pre] =>
Array ( [timezone] => EDT [end] => 1588599000 [start] => 1588579200 [gmtoffset] => -14400 ) [regular] =>
Array ( [timezone] => EDT [end] => 1588622400 [start] => 1588599000 [gmtoffset] => -14400 ) [post] =>
Array ( [timezone] => EDT [end] => 1588636800 [start] => 1588622400 [gmtoffset] => -14400 ) ) [tradingPeriods] =>
Array ( [0] =>
Array ( [0] =>
Array ( [timezone] => EDT [end] => 1588622400 [start] => 1588599000 [gmtoffset] => -14400 ) ) ) [dataGranularity] => 5m [range] => 1d [validRanges] =>
Array ( [0] => 1d [1] => 5d [2] => 1mo [3] => 3mo [4] => 6mo [5] => 1y [6] => 2y [7] => 5y [8] => 10y [9] => ytd [10] => max ) ) [timestamp] =>
Array ( [0] => 1588599000 [1] => 1588599300 [2] => 1588599600 [3] => 1588599900 [4] => 1588600200 [5] => 1588600500 ) [indicators] =>
Array ( [quote] =>
Array ( [0] =>
Array ( [open] =>
Array ( [0] => 13.3599996567 [1] => 13.8599996567 [2] => 13.720000267 [3] => 13.8800001144 [4] => 14.0349998474 [5] => 14.051199913 ) [close] =>
Array ( [0] => 13.9499998093 [1] => 13.7299995422 [2] => 13.8400001526 [3] => 14.0450000763 [4] => 14.0600004196 [5] => 14.1899995804 ) [high] =>
Array ( [0] => 14 [1] => 13.8599996567 [2] => 13.9399995804 [3] => 14.0900001526 [4] => 14.1000003815 [5] => 14.2600002289 ) [low] =>
Array ( [0] => 13.1099996567 [1] => 13.5900001526 [2] => 13.720000267 [3] => 13.8699998856 [4] => 13.8509998322 [5] => 14 ) [volume] =>
Array ( [0] => 124733 [1] => 49489 [2] => 51657 [3] => 48362 [4] => 34055 [5] => 36566 ) ) ) ) ) ) [error] => ) );
You need:
<?php
$data = json_decode(file_get_contents($req_url),true);
$close = $data['chart']['result'][0]['meta']['currentTradingPeriod']['pre']['regular']['post']['tradingPeriods'][0][0]['validRanges']['timestamp']['indicators']['quote'][0]['open']['close'];
echo $close;
?>
UPDATE: Your comment shows different array. You need:
<?php
$req_url = 'https://query1.finance.yahoo.com/v7/finance/chart/SEAS?&interval=5m';
$data = json_decode(file_get_contents($req_url), true);
$close = $data['chart']['result'][0]['indicators']['quote'][0]['close'];
print_r($close);
?>

Related

How can we store protected array values in php variables

I am trying to access a protected array tree. I had tried many ways to get the data from the tree. The array is stored in a variable called $printers. I had tried a normal approach like using array index values like echo $printers[0].printers etc, but no luck. Could anyone please guide me to how to access this protected array tree?
Array
(
[0] => PrintNode\Printer Object
(
[id:protected] => 69416933
[computer:protected] => PrintNode\Computer Object
(
[id:protected] => 123456
[name:protected] => DESKTOP
[inet:protected] => 0.0.0.0
[inet6:protected] =>
[hostname:protected] => Controlroom#DESKTOP
[version:protected] => 4.23.1
[jre:protected] =>
[systemInfo:protected] =>
[acceptOfflinePrintJobs:protected] =>
[createTimestamp:protected] => DateTime Object
(
[date] => 2020-04-16 11:23:56.595000
[timezone_type] => 2
[timezone] => Z
)
[state:protected] => connected
)
[name:protected] => ZJ
[description:protected] => ZJ-80 11.3.0.1
[capabilities:protected] => Array
(
[bins] => Array
(
[0] => Automatically Select
[1] => Manual Paper Feed
)
[collate] => 1
[color] =>
[copies] => 9999
[dpis] => Array
(
[0] => 203x203
)
[duplex] =>
[extent] => Array
(
[0] => Array
(
[0] => 719
[1] => 2099
)
[1] => Array
(
[0] => 719
[1] => 32759
)
)
[medias] => Array
(
)
[nup] => Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 6
[4] => 9
[5] => 16
)
[papers] => Array
(
[ZPrinter Paper(80 x 210mm)] => Array
(
[0] => 719
[1] => 2099
)
[ZPrinter Paper(80 x 297mm)] => Array
(
[0] => 719
[1] => 2970
)
[ZPrinter Paper(80 x 3276mm)] => Array
(
[0] => 719
[1] => 32759
)
[ZPrinter Paper(80(72) x 210mm)] => Array
(
[0] => 719
[1] => 2099
)
[ZPrinter Paper(80(72) x 297mm)] => Array
(
[0] => 719
[1] => 2970
)
[ZPrinter Paper(80(72) x 3276mm)] => Array
(
[0] => 719
[1] => 32759
)
)
[printrate] =>
[supports_custom_paper_size] =>
)
[default:protected] =>
[createTimestamp:protected] => DateTime Object
(
[date] => 2020-04-29 13:53:39.527000
[timezone_type] => 2
[timezone] => Z
)
[state:protected] => offline
)
[1] => PrintNode\Printer Object
(
[id:protected] => 123456789
[computer:protected] => PrintNode\Computer Object
(
[id:protected] => 1234566
[name:protected] => DESKTOP
[inet:protected] => 192.168.1.2
[inet6:protected] =>
[hostname:protected] => Controlroom#DESKTOP
[version:protected] => 4.23.1
[jre:protected] =>
[systemInfo:protected] =>
[acceptOfflinePrintJobs:protected] =>
[createTimestamp:protected] => DateTime Object
(
[date] => 2020-04-16 11:23:56.595000
[timezone_type] => 2
[timezone] => Z
)
[state:protected] => connected
)
[name:protected] => ZJ-80(copy of 2)
[description:protected] => ZJ-80 11.3.0.1
[capabilities:protected] => Array
(
[bins] => Array
(
[0] => Automatically Select
[1] => Manual Paper Feed
)
[collate] => 1
[color] =>
[copies] => 9999
[dpis] => Array
(
[0] => 203x203
)
[duplex] =>
[extent] => Array
(
[0] => Array
(
[0] => 719
[1] => 2099
)
[1] => Array
(
[0] => 719
[1] => 32759
)
)
[medias] => Array
(
)
[nup] => Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 6
[4] => 9
[5] => 16
)
[papers] => Array
(
[ZPrinter Paper(80 x 210mm)] => Array
(
[0] => 719
[1] => 2099
)
[ZPrinter Paper(80 x 297mm)] => Array
(
[0] => 719
[1] => 2970
)
[ZPrinter Paper(80 x 3276mm)] => Array
(
[0] => 719
[1] => 32759
)
[ZPrinter Paper(80(72) x 210mm)] => Array
(
[0] => 719
[1] => 2099
)
[ZPrinter Paper(80(72) x 297mm)] => Array
(
[0] => 719
[1] => 2970
)
[ZPrinter Paper(80(72) x 3276mm)] => Array
(
[0] => 719
[1] => 32759
)
)
[printrate] =>
[supports_custom_paper_size] =>
)
[default:protected] => 1
[createTimestamp:protected] => DateTime Object
(
[date] => 2020-04-29 14:34:00.787000
[timezone_type] => 2
[timezone] => Z
)
[state:protected] => offline
)
)
I need to access the values, the hostname:protected and name:protected and paper's 1st element.

Looping a Complex Multidimensional Array

I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}

combining two arrays data on same id in php

hello i have this type of array,
Array
(
[1] => Array
(
[0] => FGC00092
[splcode] => FGC00092
[1] => 2016-05-02,2016-05-09
[tran_date] => 2016-05-02,2016-05-09
[2] => 3,40
[unit] => 3,40
[3] => 300,320
[value] => 300,320
)
[2] => Array
(
[0] => FGC00085
[splcode] => FGC00085
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 2
[unit] => 2
[3] => 400
[value] => 400
)
[3] => Array
(
[0] => FGC00099
[splcode] => FGC00099
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 3
[unit] => 3
[3] => 600
[value] => 600
)
[4] => Array
(
[0] => FGC00092
[splcode] => FGC00092
[1] => 2016-05-09
[tran_date] => 2016-05-09
[2] => 40
[unit] => 40
[3] => 320
[value] => 320
)
[5] => Array
(
[0] => FGC00115
[splcode] => FGC00115
[1] => 2016-05-10
[tran_date] => 2016-05-10
[2] => 476
[unit] => 476
[3] => 3808
[value] => 3808
)
[6] => Array
(
[0] => FGC00062
[splcode] => FGC00062
[1] => 2016-05-25
[tran_date] => 2016-05-25
[2] => 10
[unit] => 10
[3] => 80
[value] => 80
)
[7] => Array
(
[0] => FGC00059
[splcode] => FGC00059
[1] => 2016-05-30
[tran_date] => 2016-05-30
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
[8] => Array
(
[0] => FGC00117
[splcode] => FGC00117
[1] => 2016-05-31
[tran_date] => 2016-05-31
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
)
i want to combine the them which have same ID. like for FGC00092, array structure must be,
Array
(
[1] => Array
(
[0] => FGC00092
[splcode] => FGC00092
[1] => 2016-05-02
[tran_date] => 2016-05-02
[2] => 3
[unit] => 3
[3] => 300
[value] => 300
)
[2] => Array
(
[0] => FGC00085
[splcode] => FGC00085
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 2
[unit] => 2
[3] => 400
[value] => 400
)
[3] => Array
(
[0] => FGC00099
[splcode] => FGC00099
[1] => 2016-05-06
[tran_date] => 2016-05-06
[2] => 3
[unit] => 3
[3] => 600
[value] => 600
)
[4] => Array
(
[0] => FGC00115
[splcode] => FGC00115
[1] => 2016-05-10
[tran_date] => 2016-05-10
[2] => 476
[unit] => 476
[3] => 3808
[value] => 3808
)
[5] => Array
(
[0] => FGC00062
[splcode] => FGC00062
[1] => 2016-05-25
[tran_date] => 2016-05-25
[2] => 10
[unit] => 10
[3] => 80
[value] => 80
)
[6] => Array
(
[0] => FGC00059
[splcode] => FGC00059
[1] => 2016-05-30
[tran_date] => 2016-05-30
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
[7] => Array
(
[0] => FGC00117
[splcode] => FGC00117
[1] => 2016-05-31
[tran_date] => 2016-05-31
[2] => 50
[unit] => 50
[3] => 400
[value] => 400
)
)
You can use foreach and in_array to solve your issue as below.
$tempArray = $outArray = array();
$count = 1;
foreach($yourArray as $aKey=>$aVal){
if (in_array($aVal['splcode'], $aVal)){
//do something
continue; // it will skip the current loop if the condition satisfies
}
$outArray[$count] = $aVal;
$tempArray[] = $aVal['splcode'];
$count++;
}
//You will get the result in the variable $outArray
unset($tempArray); // clear the temp array
The array_unique function will do this for you. You just needed to add the SORT_REGULAR flag:
$items_thread = array_unique($items_thread, SORT_REGULAR);

Retrieving elements from a nested array

I am using MultipleIterator() to iterate through two different arrays and get each element.
My code
$d = new MultipleIterator ();
$d->attachIterator ( new ArrayIterator ( $tbl_one_data ) );
$d->attachIterator ( new ArrayIterator ( $tbl_two_data ) );
foreach ( $d as $data ) {
print_r($data);
}
Which generates the following :
My question is how do I loop through the array and return each element? For example I would like to return 2014-11-06 11:31:58.781018. Tried using $data[0][0] but this returns all elements in the first index but I only want one element.
EDIT
print_r($d);
MultipleIterator Object ( [storage:SplObjectStorage:private] => Array ( [00000000583bd67b000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => 1 [1] => 2014-11-06 11:31:58.781018 [2] => NONE [3] => NONE ) [1] => Array ( [0] => 2 [1] => 2014-11-06 11:31:58.799436 [2] => MANAGER [3] => 500 ) [2] => Array ( [0] => 3 [1] => 2014-11-06 11:31:58.841035 [2] => MANAGER [3] => 501 ) [3] => Array ( [0] => 4 [1] => 2014-11-06 11:33:00.741873 [2] => MANAGER [3] => 500 ) [4] => Array ( [0] => 5 [1] => 2014-11-06 11:33:00.802389 [2] => MANAGER [3] => 501 ) [5] => Array ( [0] => 6 [1] => 2014-11-06 13:15:49.457646 [2] => MANAGER [3] => 500 ) [6] => Array ( [0] => 7 [1] => 2014-11-06 13:37:16.259128 [2] => NONE [3] => NONE ) [7] => Array ( [0] => 8 [1] => 2014-11-06 13:37:16.275201 [2] => NONE [3] => 500 ) [8] => Array ( [0] => 9 [1] => 2014-11-06 13:37:27.682873 [2] => NONE [3] => NONE ) [9] => Array ( [0] => 10 [1] => 2014-11-06 13:37:27.690863 [2] => NONE [3] => 500 ) [10] => Array ( [0] => 11 [1] => 2014-11-06 13:52:21.108003 [2] => MANAGER [3] => 500 ) [11] => Array ( [0] => 12 [1] => 2014-11-06 14:17:01.266769 [2] => NONE [3] => NONE ) [12] => Array ( [0] => 13 [1] => 2014-11-06 14:17:01.279507 [2] => node1-1415283420.0 [3] => 500 ) [13] => Array ( [0] => 14 [1] => 2014-11-06 14:17:02.527183 [2] => node1-1415283420.0 [3] => 500 ) [14] => Array ( [0] => 15 [1] => 2014-11-06 14:17:23.775279 [2] => node1-1415283442.1 [3] => 500 ) ) ) [inf] => ) [00000000583bd67a000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [1] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [2] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [3] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [4] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [5] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [6] => Array ( [0] => Local/120#disc-agents/n [1] => PAUSEALL [2] => [3] => [4] => [5] => [6] => ) [7] => Array ( [0] => Dunc Test [1] => PAUSE [2] => [3] => [4] => [5] => [6] => ) [8] => Array ( [0] => Local/120#disc-agents/n [1] => UNPAUSEALL [2] => [3] => [4] => [5] => [6] => ) [9] => Array ( [0] => Dunc Test [1] => UNPAUSE [2] => [3] => [4] => [5] => [6] => ) [10] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [11] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [12] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) [13] => Array ( [0] => NONE [1] => ABANDON [2] => 1 [3] => 1 [4] => 1 [5] => [6] => ) [14] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) ) ) [inf] => ) ) )
if this is really output by print_r then $data[0][1] must be '2014-11-06 11:31:58.781018' . I test it by code:
$data = array(array('1','2014-11-06 11:31:58.781018'));
echo '<pre>';
print_r($data); echo '<br>';
echo 'what we want: '.$data[0][1].'<br>';
otput:
Array
(
[0] => Array
(
[0] => 1
[1] => 2014-11-06 11:31:58.781018
)
)
what we want: 2014-11-06 11:31:58.781018
If you have the $data as an array you can access the value: 2014-11-06 11:31:58.781018 by using,
$date = $data[0][1];

PHP nested array remove specific keys by condition

I am using PHP 5.5.2 with MySQL back-end. I have a self-referencing database table from that I am getting an array as below :
Array
(
[0] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => Aaa
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 48
[s_id] => 37
[m_id] =>
[title] => bbbbbbbbbbbbb
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 49
[s_id] => 38
[m_id] =>
[title] => cccccccccc
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 50
[s_id] => 39
[m_id] =>
[title] => ddddddddd dddd
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[2] => Array
(
[id] => 51
[s_id] => 40
[m_id] =>
[title] => eeeeeee eeeeeee.
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 54
[s_id] => 18
[m_id] =>
[title] => fffffffff ffff
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 55
[s_id] => 19
[m_id] =>
[title] => gggggg gggggg
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 56
[s_id] => 20
[m_id] =>
[title] => hhhhhhhhhh hhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[2] => Array
(
[id] => 57
[s_id] => 21
[m_id] =>
[title] => iiiiiiii iiii
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
[0] => Array
(
[id] => 58
[s_id] => 22
[m_id] =>
[title] => jjjjjjj jjj
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 59
[s_id] => 23
[m_id] =>
[title] => kkkkkkkk kkkk
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => xxxx xxx xxx
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 112
[s_id] => 37
[m_id] =>
[title] => lllllllllll llllll
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 113
[s_id] => 38
[m_id] =>
[title] => mmmmmmm mmmm
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 123
[s_id] => 39
[m_id] =>
[title] => nnnnnnnnn hhhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 456
[s_id] => 18
[m_id] =>
[title] => ppppppp pppppp
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 545
[s_id] => 19
[m_id] =>
[title] => qqqqqqqqq qqq
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 435
[s_id] => 20
[m_id] =>
[title] => ssssssssss sssssssssss
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
)
)
)
There might be N nesting levels in this array.
Now for each array element, if ['type'] is G then I want to remove keys [s_id] and [m_id] and for any other type I want to remove keys [start], [end] and [time].
Edit : I have tried using a recursive function but it is not giving the desired result.
private function getarray(array &$arr){
foreach ($arr as $item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
foreach ($item["children"] as $c)
{
$child = array();
getarray($child);
}
}
}
How can I achieve this?
Thanks.
function filterMyArray($arr){
if(isset($arr['type'] && $arr['type'] == 'G'){
unset($arr['s_id']);
unset($arr['m_id']);
}
else if(isset($arr['type']){
unset($arr['start']);
unset($arr['end']);
unset($arr['time']);
}
if(isset($arr['children'])
$arr['children'] = filterMyArray($arr['children']);
return $arr;
}
This function is not tested, I wrote it from memory.
But seriously, Googling something like that doesn't hurt. It would take you 5 minutes to get all the required knowledge to do this...
Resolved it myself :
private function getarray(array &$arr){
foreach ($arr as &$item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
if (is_array($item['children']) && sizeof($item['children']) > 0){
getarray($item['children']);
}
}
}
And this is working fine. I have posted this answer to help other people who are novice to php and trying to overcome their problem.
And this is a slap to the jealous people who did not helped me but asked "What did you tried so long?" with a superiority complex attitude. I did it myself.

Categories