How to group an array like this - php

I have an array like this:
Array
(
[0] => Array
(
[lvid] => 01-3234
[status] => 0
[totals] => 500
[tgls] => 2020-05-24
)
[1] => Array
(
[lvid] => 01-3234
[status] => 1
[totals] => 100,100
[tgls] => 2020-05-24,2020-05-25
)
)
and i hope the result like this:
Array
(
[0] => Array
(
[01-3234] => Array
(
[0] => Array
(
[2020-05-24] => 500
)
[1] => Array
(
[2020-05-24] => 100
[2020-05-25] => 100
)
)
)
)
anybody help me,pls

Make use of explode and array_combine functions
akshay#ideapad:~$ cat test.php
<?php
$input = array(
array( "lvid" => "01-3234", "status" => 0, "totals" => 500, "tgls" => "2020-05-24" ),
array( "lvid" => "01-3234", "status" => 1, "totals" => "100,100", "tgls" => "2020-05-24,2020-05-25" )
);
// your given input samples
print_r($input);
// process and create new array
$out=array();
foreach($input as $item)
{
$toadd = array_combine(
explode(",",$item["tgls"]),
explode(",", $item["totals"])
);
if(isset($out[$item['lvid']]))
{
$out[$item['lvid']][] = $toadd;
}else
{
$out[$item['lvid']] = array($toadd);
}
}
// finally expected output
print_r(array($out));
Tested on:
akshay#ideapad:~$ php --version
PHP 7.4.6 (cli) (built: May 14 2020 10:02:44) ( NTS )
Output:
akshay#ideapad:~$ php test.php
Array
(
[0] => Array
(
[lvid] => 01-3234
[status] => 0
[totals] => 500
[tgls] => 2020-05-24
)
[1] => Array
(
[lvid] => 01-3234
[status] => 1
[totals] => 100,100
[tgls] => 2020-05-24,2020-05-25
)
)
Array
(
[0] => Array
(
[01-3234] => Array
(
[0] => Array
(
[2020-05-24] => 500
)
[1] => Array
(
[2020-05-24] => 100
[2020-05-25] => 100
)
)
)
)

Related

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
)
)

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.

show distinct result from two array

I have the following two arrays...
1) how could i get only the different key->value one?
2) how can i insert to mysql the second array?
// first array
$aa = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
// second array
$bb = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => NAN
[p_r] => RN
[id] => 1214125
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] => 21
[p_r] => 25
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
I have used the array_diff function but i get NULL.
please some one help?
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
It's unclear what you want. Please give an example or your desired output. Here's one possibility:
$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);
Output:
Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
)

Eliminate timestamps within a range from array

This is my array of timestamps. I would like to eliminate values within 30 seconds of each other, only keeping the value if there is not another value within 30 sec. Any help would be greatly appreciated!
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356399005
[3] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356399007
[2] => 1356398871
[3] => 1356398876
)
[99996] => Array
(
[0] => 1356399003
[1] => 1356399004
[2] => 1356399008
)
[99995] => Array
(
[0] => 1356399009
)
)
My expected output:
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356398871
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)
Any solutions/advice would be greatly appreciated! Thanks!
Your output is wrong .. because 1356398971 + 30 = 1356399001 which is grater than 1356399000 if i understand you clearly this i what it should look like
$data = array(
99999 => array(
0 => 1356399000,
1 => 1356398971,
2 => 1356399005,
3 => 1356413406,
),
99997 => array(
0 => 1356399002,
1 => 1356399007,
2 => 1356398871,
3 => 1356398876,
),
99996 => array(
0 => 1356399003,
1 => 1356399004,
2 => 1356399008,
),
99995 => array(
0 => 1356399009,
),
);
echo "<pre>";
$data = array_map(function ($values) {
rsort($values);
$ci = new CachingIterator(new ArrayIterator($values));
$values = array();
foreach ( $ci as $ts ) {
if ($ci->hasNext()) {
abs($ci->current() - $ci->getInnerIterator()->current()) > 30 and $values[] = $ts;
} else {
$values[] = $ts;
}
}
sort($values);
return $values;
}, $data);
print_r($data);
Output
Array
(
[99999] => Array
(
[0] => 1356398971
[1] => 1356413406
)
[99997] => Array
(
[0] => 1356398871
[1] => 1356399002
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)

How to Add a new Key and Merge the array values in multidimemsional array PHP

here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);

Categories