I am trying to print part of this array, basically all the URLS [href] with "ProviderRedirect.ashx" basically [0] to [infinite]
Array
(
[name] => HC Redirect
[count] => 66
[frequency] => Daily
[version] => 14
[newdata] => 1
[lastrunstatus] => partial
[thisversionstatus] => success
[nextrun] => Sun Jan 17 2016 14:03:08 GMT+0000 (UTC)
[thisversionrun] => Sat Jan 16 2016 14:03:08 GMT+0000 (UTC)
[results] => Array
(
[collection1] => Array
(
[0] => Array
(
[Hotel Search] => Array
(
[href] => https://www.domain.com/ProviderRedirect.ashx?key=0.6359329.272723160.5179.GBP.1729297590&saving=410&source=32-0
[text] => View Deal
)
[index] => 1
[url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
)
[1] => Array
(
[Hotel Search] => Array
(
[href] => https://www.domain.com/ProviderRedirect.ashx?key=0.21199849.272723130.457.GBP.753573779&source=32-0
[text] => View Deal
)
[index] => 2
[url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
)
[2] => Array
(
[Hotel Search] => Array
(
[href] => https://www.domain.com/ProviderRedirect.ashx?key=0.23906211.272723157.1326.GBP.2008823249&source=32-0
[text] => View Deal
)
[index] => 3
[url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
)
[3] => Array
(
[Hotel Search] => Array
(
[href] => https://www.domain.com/ProviderRedirect.ashx?key=0.5242811.272723157.3854.GBP.1642352834&source=32-0
[text] => View Deal
)
[index] => 4
[url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
)
[4] => Array
(
[Hotel Search] => Array
(
[href] => https://www.domain.com/ProviderRedirect.ashx?key=0.675524.272723160.1457.GBP.2121712597&saving=18&source=32-0
[text] => View Deal
)
[index] => 5
[url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
)
[5] => Array
(
[Hotel Search] => Array
(
[href] => https://www.domain.com/ProviderRedirect.ashx?key=0.5743724.272723155.847.GBP.1001086600&source=32-0
[text] => View Deal
)
[index] => 6
[url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
)
[6] => Array
Either use array_walk_recursive() function or write your own function to recursively traverse the array and print all urls.
Suppose $arr is your array.
Method(1):
array_walk_recursive($arr, function($item, $key) {
if ($key == "href") {
echo $item . "<br />";
}
});
Method(2):
function process_array($array){
foreach($array as $key => $value){
if(is_array($value)){
process_array($value);
}else{
if($key == "href"){
echo $value . "<br />";
}
}
}
}
process_array($arr);
$items = array(
'results' => array(
'collection1'=> array(
array(
'hotel_search'=>array(
'href'=>'value'
),
'index'=>'value'
),
array(
'hotel_search'=>array(
'href'=>'value'
),
'index'=>'value'
)
)
)
);
$collection1 = $items['results']['collection1'];
foreach($collection1 as $collection) {
printf("%s", $collection['hotel_search']['href']);
}
You would need to loop over the results array though to avoid hardcoding
If all you need to do is print the values, array_walk_recursive would do the job:
array_walk_recursive($array, function($item, $key) {
if ('href' === $key) {
echo "$item\n";
}
});
Related
I am trying to get the 'name' value from within a 'nested array' I am not sure if thats the correct term in PHP.
Array (
[0] => Array
(
[event] => Array
(
[id] => 28140972
[name] => Northwich v Lincoln United FC
[countryCode] => GB
[timezone] => Europe/London
[openDate] => 2017-03-08T19:45:00.000Z
)
[marketCount] => 24
)
[1] => Array
(
[event] => Array
(
[id] => 28140974
[name] => Viimsi MRJK v Paide Linnameeskond II
[countryCode] => EE
[timezone] => Europe/London
[openDate] => 2017-03-08T17:00:00.000Z
)
[marketCount] => 24
)
}
I am trying to access the 'name' key of every item in the array, but struggling to do it. Any advice?
$arrayOfNames = array_map(function ($item) {
return $item['event']['name'];
}, $yourPreviousArray);
Test case:
>>> print_r($data)
Array
(
[0] => Array
(
[event] => Array
(
[id] => 1
[name] => James
)
)
[1] => Array
(
[event] => Array
(
[id] => 2
[name] => Jim
)
)
)
=> true
>>> array_map(function($item) {
... return $item['event']['name'];
... }, $data)
=> [
"James",
"Jim",
]
>>>
You can do something like this:
foreach ($array as $item) {
echo $item['event']['name'].'<br/>';
}
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 :)
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;
}
}
I want to get the value of 'GUID' with the value of 'SamAccountName'. i.e. I only have the value pf 'SamAccountName' and I would like to get the value of 'GUID' for that part of the array.
Array
(
[0] => Array
(
[DistinguishedName] => CN=johnn#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 26d7c204-7db7-4601-8cd2-0dd0d3b37d97
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => johnn#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => John Nolan
[SamAccountName] => johnn_playgroundla
[FullSamAccountName] => EXCH024\johnn_playgroundla
[UserPrincipalName] => johnn#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[1] => Array
(
[DistinguishedName] => CN=csliney#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 71224be8-1b8b-46e7-97ef-2cd873bf9b7f
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => csliney#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Christopher Sliney
[SamAccountName] => csliney_playgroundla
[FullSamAccountName] => EXCH024\csliney_playgroundla
[UserPrincipalName] => csliney#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[2] => Array
(
[DistinguishedName] => CN=lee#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => b428b57f-4cd4-4243-a76a-f25f5ff3be97
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => lee#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => MSExchange2007Mailbox
[1] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Lee Roderick
[SamAccountName] => lee_playgroundla
[FullSamAccountName] => EXCH024\lee_playgroundla
[UserPrincipalName] => lee#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => MSExchangeMailboxes
[1] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
[3] => Array
(
[DistinguishedName] => CN=theresa#playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
[GUID] => 4b2aee17-9e88-4de9-b95b-63a9877835a6
[OriginatingServer] => dcprov024-CA-1.exch024.domain.local
[Name] => theresa#playgroundla
[HostingObjectType] => Array
(
[HostingObjectTypes] => Array
(
[0] => ActiveSync
[1] => MSExchange2007Mailbox
[2] => ActiveDirectoryUser
)
)
[HostingOwners] => Array
(
[HostingObjectOwners] => Array
(
[0] => MSExchange2007Mailboxes
[1] => ActiveDirectoryUsers
)
)
[Attributes] => Array
(
[Hidden] =>
[ReadOnly] =>
[SpecialAccess] =>
[Items] => Array
(
)
)
[DisplayName] => Theresa Baker
[SamAccountName] => theresa_playgroundla
[FullSamAccountName] => EXCH024\theresa_playgroundla
[UserPrincipalName] => theresa#playgroundla.com
[AccountExpires] =>
[Enabled] =>
[EnabledFeatures] => Array
(
[string] => Array
(
[0] => ActiveSync
[1] => MSExchangeMailboxes
[2] => ActiveDirectoryUsers
)
)
[LastLogonTimestamp] =>
)
)
This was originally a stdClass object but I used json_decode(json_encode($obj), true) to convert to an associative array.
Sounds like you want to get the GUID portion for the value of 'SamAccountName'. Use a foreach loop:
function getGUID($san, $array) {
foreach($array as $a) {
if($a['SamAccountName'] == $san) {
return $a['GUID'];
}
}
return false;
}
$guid = getGUID("SamAccountNameHere", $yourArray);
You can use a simple loop to fetch it
$id = 0;
foreach($data as $item) {
if (isset($item['SamAccountName']) && 'accountName' == $item['SamAccountName']) {
$id = $item['GUID'];
break;
}
}
var_dump($id);
is this what you are looking for?
function findBySam($arrayList, $sam) {
foreach($arrayList as $array) {
if($array['SamAccountName'] == $sam) {
return $array;
}
}
return false;
}
Here is an example of a function that you could use. This assumes that there will be only one object with the SamAccountName that you supply in the array (it just uses the first one that it finds). It returns the GUID of the matching array and false if it cannot find an array with a matching SamAccountName.
function getGuidForSamAccountName($arr, $name) {
foreach ($arr as $elem) {
if ($elem['SamAccountName'] === $name) {
return $elem['GUID'];
}
}
return false; //No match found
}
You can use array_filter function of php:
http://php.net/manual/en/function.array-filter.php
example:
$GUID = "sample";
array_filter($array, "findElement");
function findElement($el) {
return $el["GUID"] == $_GLOBAL["GUID"];
}
Not a very elegant solution... but it should work.
I have following array pattern.
Array
(
[0] => Array
(
[label] => 2011
[subtable] => Array
(
[0] => Array
(
[label] => 05
[subtable] => Array
(
[0] => Array
(
[label] => /hello-world.html
[url] => http://example.com/2011/05/hello-world.html
)
[1] => Array
(
[label] => /test-test-test.html
[url] => http://example.com/2011/05/test-test-test.html
)
)
)
[1] => Array
(
[label] => 04
[subtable] => Array
(
[0] => Array
(
[label] => /i-have-some-problem-with-gfc-updates.html
[url] => http://example.com/2011/04/i-have-some-problem-with-gfc-updates.html
)
[1] => Array
(
[label] => /testing-for-google-frineds-update.html
[url] => http://example.com/2011/04/testing-for-google-frineds-update.html
)
)
)
[2] => Array
(
[label] => /05
[url] => http://example.com/2011/05
)
[3] => Array
(
[label] => /04
[url] => http://example.com/2011/04
)
)
)
[1] => Array
(
[label] => /index
[url] => http://example.com/
)
)
I want to get "label" and "url" into another array like
array(
[0] = array([label] => a [url] => http://example.com/a/)
[1] = array([label] => b [url] => http://example.com/b/)
)
How can I achieve it using recursion, or by passing it to a function?
You may use array_walk_recursive for this:
$parsed_array = array();
function parse_array($item, $key) {
global $parsed_array;
if(is_array($item) && isset($item['url'])) {
$parsed_array[] = $item;
}
}
array_walk_recursive($your_array, 'parse_array');
Or you may implement it using a recursive function:
function parse_array($array) {
$result = array();
foreach($array as $item) {
if(isset($item['subtable'])) {
$result = array_merge($result, parse_array($item['subtable']));
}else{
$result[] = $item;
}
}
return $result;
}
$parsed_array = parse_array($your_array);