Read array-node with foreach, doesn't work - php

I have this array in a foreach-loop with the 'as' $train
Now i want to read out the 'mat'-node.
I've tried this:
<?php
echo "<!-- ";
foreach ($train['mat'] as $mat) {
echo "Mat:" . $mat . "";
}
echo " -->";
?>
But it gives a empty foreach result in mijn HTML-source, between the comments.
Array
(
[status] => 0
[via] => Utrecht C., Houten, Geldermalsen
[bestemming] => Tiel
[vleugels] => Array
(
[0] => Array
(
[stopstations] => Array
(
[0] => Array
(
[naam] => Vleuten
[code] => VTN
)
[1] => Array
(
[naam] => Utrecht Terwijde
[code] => UTT
)
[2] => Array
(
[naam] => Utrecht Leidsche Rijn
[code] => UTLR
)
)
[bestemming] => Tiel
[mat] => Array
(
[0] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2422
)
[1] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2464
)
)
)
)
[vervoerder] => NS
[spoor] => 6
)

Hope this help.
But I think you need read more about PHP Array and Array index
echo "<!-- ";
foreach($train['vleugels'][0]['mat'] as $mat) {
echo "Mat:".$mat[2]."<br>";
}
echo " -->";
// <!-- 2422 2464 -->

Related

PHP how to acces multiple array values

Hello guys i have this multiple array but i don't realy know how to access the values can i get all values of the company for example i will build an table with this values as shown bellow: is it possible to
build an nested while loop?
<?php
while (($company_name = current($aCompanys)) !== FALSE ){
echo key($aCompanys).'<br />';
next($aCompanys);
}
?>
[CompanyName1+] => Array (
[Zen] => Array (
[article] => Array (
[0] => Array (
[0] => Kalender
[1] => 9.99
[2] => 2017
)
[1] => Array (
[0] => Notizbuch DINA A4
[1] => 24.99
[2] => 2017
)
)
)
[PlenkS] => Array (
[article] => Array (
[0] => Array (
[0] => Kugelschreiber
[1] => 19.99
[2] => 2017
)
)
)
)
[CompanyName2] => Array (
[asdasd] => Array (
[article] => Array (
[0] => Array (
[0] => Kugelschreiber
[1] => 19.99
[2] => 2017
)
)
)
)
Since you want to print the company names as CompanyName1 : Zen , CompanyName2 : Plenks
Please find below complete code for your array, it works fine and gives the desired output.
<?php
$main_array = array("CompanyName"=>array("Zen"=>array("Article"=>array(array('kalendar','9.99','2017'),array('Notizbuch DINA A4','24.99','2017'))),"Plensk"=>array("Article"=>array(array('Kugelschreiber','9.99','2017')))),"CompanyName2"=>array("Zen2"=>array(1,2,3),"Plensk2"=>array(1,2,3)));
var_dump($main_array);
echo "<br /><br />";
foreach ($main_array as $i => $values){
foreach ($values as $key => $t){
echo $i . ":" . $key . "<br />";
}
}
?>
you can access them for example:
$aCompanys[CompanyName1+][Zen][article][0][2];
to get this value: "2017"

Php multidimensional array

I have build this array structure from dig query data.
[10] => Array
(
[id] => 150
[0] => 200.201.202.23
[1] => dns.name1.com
[2] => 200.201.202.24
[3] => dns.name2.com
[4] => 200.201.202.25
[5] => dns.name3.com
) `
I need something like:
[10] => Array
(
[0] => array ( [0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => array ( [0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => array ( [0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
) `
I'm not sure if this is possible?
Heres the code where i create the array:
At the first time from the dig i use array_push() to add content to it.
$temp = array();
$i = 0;
foreach ($digResult as $single){
if (preg_match('/(?:^|\s+)(\d+)(?:\s+|\n+|$)/', $single )){
$temp []["id"]= $single;
$i++;
}else {
$temp[$i][] = $single;
}
}
This will work for you :
<?php
$dataArray = array(10 => array
(
'id' => 150 ,
0 => '200.201.202.23' ,
1 => 'dns.name1.com',
2 => '200.201.202.24',
3 => 'dns.name2.com',
4 => '200.201.202.25',
5 => 'dns.name3.com',
)
);
$newArray = array();
$id = $dataArray[10]['id'];
for($i=0; $i< 6; $i++)
{
$newArray[10][] = array(0=>$dataArray[10][$i],1=>$dataArray[10][$i+1],'id'=> $id);
$i+=1;
}
print_r($newArray);
?>
This will output
Array
(
[10] => Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
)
LIVE EXAMPLE : CLICK HERE
Try this:-
<?php
$array = array(
'id' => '150',
'0' => '200.201.202.23',
'1' => 'dns.name1.com',
'2' => '200.201.202.24',
'3' => 'dns.name2.com',
'4' => '200.201.202.25',
'5' => 'dns.name3.com'
);
$i = 0;
$arrayLenght = (count($array)-2);
$newArray = array();
while ($i <= $arrayLenght) {
$newArray[] = array(
"0" => $array[$i++],
"1" => $array[$i++],
"id" => $array['id']
);
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
?>
Output:-
Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)

Output of all result of grouped array

I hope you could help me on how should I make this output be done.
CSV file
Department,Name,Employee No.,Date Time
LMS,"Bach, Jerome",102,6/30/2014 12:23
MTS,"Lorvia, Christine",103,6/16/2014 9:31
SSS,Jannah Curtis,104,6/16/2014 8:45
SSS,Jannah Curtis,104,6/28/2014 14:29
ITM,Sassy Mica,105,6/17/2014 9:12
ITM,Sassy Mica,105,6/17/2014 20:43
ITM,Sassy Mica,105,6/18/2014 9:12
I already grouped the department and this is the output
[MTS] => Array
(
[103] => Array
(
[0] => MTS
[1] => Lorvia Christine
[2] => 103
[3] => 6/16/2014 9:31
)
)
[SSS] => Array
(
[104] => Array
(
[0] => SSS
[1] => Jannah Curtis
[2] => 104
[3] => 6/28/2014 14:29
)
)
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => 6/18/2014 9:12
)
)
but I want an output that will result all her date/time record under the element [3].
Ex.
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => 6/17/2014 9:12
=> 6/17/2014 20:43
=> 6/18/2014 9:12
)
)
While the time record is sorted.
You don't have a choice. You need to process them accordingly. The array format you desire is invalid, since they cannot share the same key, just create another dimension for the time. Example:
$data = array();
$handle = fopen('sample.csv', 'r');
while(!feof($handle)) {
$row = fgetcsv($handle, '4096');
if(!isset($data[$row[0]][$row[2]])) {
// simple assignment
$data[$row[0]][$row[2]] = array($row[0], $row[1], $row[2], array($row[3],));
} else {
// process
$data[$row[0]][$row[2]][3][] = $row[3]; // push it inside instead of assigning a new one
$temp = $data[$row[0]][$row[2]][3]; // temporary storage
$temp = array_map(function($var){
return strtotime($var); // convert to unix time
}, $temp);
sort($temp); // sort ascending
$data[$row[0]][$row[2]][3] = array_map(function($var){
return date('m/d/Y H:i', $var); // return to old format with sorted values
}, $temp);
}
}
array_shift($data); // remove the first (the header)
echo '<pre>';
print_r($data);
Should yield something like:
Array
(
[LMS] => Array
(
[102] => Array
(
[0] => LMS
[1] => Bach, Jerome
[2] => 102
[3] => Array
(
[0] => 6/30/2014 12:23
)
)
)
[MTS] => Array
(
[103] => Array
(
[0] => MTS
[1] => Lorvia, Christine
[2] => 103
[3] => Array
(
[0] => 6/16/2014 9:31
)
)
)
[SSS] => Array
(
[104] => Array
(
[0] => SSS
[1] => Jannah Curtis
[2] => 104
[3] => Array
(
[0] => 06/16/2014 08:45
[1] => 06/28/2014 14:29
)
)
)
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => Array
(
[0] => 06/17/2014 09:12
[1] => 06/17/2014 20:43
[2] => 06/18/2014 09:12
)
)
)
)

Find a value in nested associative array

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.

PHP echo out multidimensional array from facebook var

I have a facebook array and I am trying to echo out the names and ID.
All I have is this array: $friends_list.
When I use this code:
print_r($friends_list);
Then the following comes out below: But how do I loop thru these? Or what if I just wanted to find out how many names there are or just call on one var by id. In short how do I echo out $friends_list which is a multiD array?
Array ( [data] => Array ( [0] => Array ( [name] => Wendy Cukierski [id] => 524320383 ) [1] => Array ( [name] => Leticia Mercado Correa [id] => 537763225 ) [2] => Array ( [name] => Olivia Urbina [id] => 538711855 ) [3] => Array ( [name] => Ruth Albrecht [id] => 541610111 ) [4] => Array ( [name] => Josh Brahm [id] => 577546485 ) [5] => Array ( [name] => Kim Yu [id] => 583515871 ) [6] => Array ( [name] => SisterTracey Dugas [id] => 643567171 ) [97] => Array ( [name] => Mary Martinez [id] => 100004696266210 ) ) [paging] => Array ( [next] => https://graph.facebook.com/1566027944/friends?limit=5000&offset=5000&__after_id=100004696266210 ) )
DUPLICATE --> How to store a Facebook user's friends list in MySQL using PHP?
$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );
$friends_list_array = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
echo $friend_ids; // output: id1,id2,id3...etc

Categories