Array
(
[stat] => ok
[offset] => 0
[limit] => 50
[total] => 1
[monitors] => Array
(
[monitor] => Array
(
[0] => Array
(
[id] =>
[friendlyname] =>
[url] =>
[type] => 3
[subtype] =>
[keywordtype] =>
[keywordvalue] =>
[httpusername] =>
[httppassword] =>
[port] =>
[interval] => 300
[status] => 2
[alltimeuptimeratio] => 100
[log] => Array
(
[0] => Array
(
[type] => 2
[datetime] => 11/24/2016 04:01:32
)
[responsetime] => Array
(
[0] => Array
(
[datetime] => 12/09/2016 19:34:02
[value] => 109
)
[1] => Array
(
[datetime] => 12/09/2016 19:29:02
[value] => 110
)
[2] => Array
(
[datetime] => 12/09/2016 19:24:02
[value] => 110
)
)
)
)
)
)
I need to get the value of datetime, and value from the responsetime array. I tried the following but it seems to not return anything.
foreach($multidim as $value) {
foreach($value as $key => $val) {
if($key == "responsetime") {
echo $val[3];
}
}
}
Where $multidim is the large multi-dim array listed above. Any help is appreciated as I am not sure where to go from here.
Thank you in advance.
to access all the response times you should do sth like this
foreach($multidim['monitors']['monitor'][0]['responsetime'] as $key => $value) {
//here $key will be 0,1,2,3...
//$value['datetime'] will be 11/24/2016 04:01:32...
//$value['value'] will be 109,110,...
}
that is if you just want to access all the response times of the first monitor. if you want to access all the monitors and their response times you would need 2 loops for example
foreach($multidim['monitors']['monitor'] as $monitorId => $monitorData) {
foreach($monitorData['responsetime'] as $key => $value) {
//here you can access all the variables e.g
//$monitorId will be 0,1,2,3...
//$key will be 0,1,2,3...
//$value['datetime'] will be 11/24/2016 04:01:32...
//$value['value'] will be 109,110,...
}
}
I hope that sends you in the right direction :)
Related
My arrays result is like this one
Array
(
[0] => Array
(
[id] => Bank Transfer
[ec] => 1000
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
)
)
[1] => Array
(
[id] => Wired
[ec] => 1001
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
[2] => Array
(
[name] => Account ID
[value] =>
)
)
)
)
It's weird because 2nd array of accounts contains same value as first array.
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
How to prevent this duplicated so the 2nd array of accounts will only show
[0] => Array
(
[name] => Account ID
[value] =>
)
Here's my code
$arr = $arr_pay = array();
foreach($site_payment as $key => $value){
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
Array of $site_payment contains
[Bank Transfer] => Array
(
[id] => Bank Transfer
[ec] => 1000
[status] => 1
[auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
)
[Dana] => Array
(
[id] => Wired
[ec] => 1001
[status] => 1
[auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
)
I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array
Please help me to solve the problem
Duplication is due to the $arr is not being reset
$arr_pay = array();
foreach($site_payment as $key => $value){
$arr = array(); // Resetting
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
I need help in extracting "Duration" from the data structure below, to a variable called var_dur.
The data comes from: print_r($data);
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Job] => Array
(
[Arn] => arn:aws:elastictranscoder:us-west-2:98yufdos8u:job/fsdoiufds98u
[Id] => fdsu98sdufio
[Input] => Array
(
[AspectRatio] => auto
[Container] => auto
[FrameRate] => auto
[Interlaced] => auto
[Key] => iudyf98udsf
[Resolution] => auto
)
[Output] => Array
(
[AlbumArt] =>
[Composition] =>
[Duration] => 31
[Height] => 522
[Id] => 1
[Key] => dlsjf9ds8uf9d8sjuf9s.mp4
[PresetId] => sdufhy89dsfu98dsf
[Rotate] => 0
[SegmentDuration] =>
[Status] => Complete
[StatusDetail] =>
[ThumbnailPattern] => filename-700thumb-{resolution}-{count}
[Watermarks] => Array
(
)
[Width] => 640
)
[OutputKeyPrefix] =>
[Outputs] => Array
(
[0] => Array
(
[AlbumArt] =>
[Composition] =>
[Duration] => 31
[Height] => 522
[Id] => 1
[Key] => dlsjf9ds8uf9d8sjuf9s.mp4
[PresetId] => duisfy98dsuf89sd
[Rotate] => 0
[SegmentDuration] =>
[Status] => Complete
[StatusDetail] =>
[ThumbnailPattern] => filename-700thumb-{resolution}-{count}
[Watermarks] => Array
(
)
[Width] => 640
)
)
[PipelineId] => dsuf89dsuf89d
[Playlists] => Array
(
)
[Status] => Complete
)
)
)
Do something like this to iterate over the Array
function searchfor($text, $array) {
foreach (array_expression as $key => $val)
if($key == $text){
return $val
}
if(is_array($var){
return searchfor($text, $var);
}
}
return null;
}
Try it
$reflect = new ReflectionClass(OBJECT);
$props = $reflect->getProperties();
foreach ($props as $prop) {
print $prop->getName();
var_dump($prop->getValue());
}
About reflection class http://www.php.net/manual/en/reflectionclass.getproperties.php
About reflection property http://www.php.net/manual/en/class.reflectionproperty.php
I have array like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
i want to locate user_id 22 and put this value "GO_FUEL_SGD_W" on brand, what should i do, so the view of array will look like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W => [1] =>GO_FUEL_SGD_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
Just use loop:
foreach($array as &$item)
{
if(array_key_exists('user_id', $item) &&
$item['user_id']==22 &&
array_key_exists('brand', $item) &&
!in_array('GO_FUEL_SGD_W', $item['brand']))
{
$item['brand'][] = 'GO_FUEL_SGD_W';
}
}
A simple foreach loop will do the job:
foreach($myarray AS &$subarray) {
if($subarray['user_id'] == 22) {
$subarray['brand'][] = "GO_FUEL_SGD_W";
break;
}
}
Working example: http://3v4l.org/8aQMj
You will need to iterate over the array and look for the element you're searching for.
foreach ($array as &$element) {
if ($element['user_id'] != 22)
continue;
$element['brand'][] = "GO_FUEL_SGD_W";
break;
}
With continue; all elements will be skipped, who have $element['user_id'] != 22 (and so none of the code after the continue; will be applied to them!).
Also it will end the loop once the requested element is reached and modified, thanks to break;.
$array= //your array;
foreach($array as $x){
if($x['user_id']=='22'){
$x['brand'][]='GO_FUEL_SGD_W';
break;
}
}
and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.
I am having trouble getting my head around how to loop through stdClasses.
Printing the array gives me the following:
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1003
[entity_id] => 0
[redirect_url] => http://yahoo.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1002
[entity_id] => 0
[redirect_url] => http://google.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1001
[entity_id] => 0
[redirect_url] => http://bing.com
[type] => Image
)
)
[total_count] => 1
I am trying to loop though with the following and print out a value (id) but I keep getting nothing.
foreach ($piggies_array as $key => $value) {
echo $piggies_array[$key]['id'];
}
foreach ($piggies_array as $key => $value) {
if (is_array($value)){
echo $value[0]->id;
}
}
I think you need:
for ($i = 0; $i < count($piggies_array); $i++) {
echo $piggies_array[$i]['piggyback'][0]->id;
}
...assuming we can only see part of your output ;)
try replace it to echo $value->id;