Change key of multidimensional array - php

I have following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.

This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.

Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);

Related

Combine some php sub-arrays, with condition

$routes array:
[admin/login] => Array
(
[0] => POST|AJAX
[1] => admin/login
[2] => admin/login
)
[admin/logout] => Array
(
[0] => POST
[1] => admin/logout
[2] => admin/logout
)
[admin/dashboard] => Array
(
[0] => GET
[1] => admin/dashboard
[2] => admin/dashboard
)
[admin/products] => Array
(
[0] => GET
[1] => admin/products/[:pag]
[2] => admin/products/all
)
[admin/products/add] => Array
(
[0] => GET
[1] => admin/product/add
[2] => admin/products/add
)
[admin/products/insert] => Array
(
[0] => POST
[1] => admin/product/insert
[2] => admin/products/insert
)
[admin/products/show] => Array
(
[0] => GET
[1] => admin/product/[i:id]
[2] => admin/products/show
)
[admin/products/edit] => Array
(
[0] => GET
[1] => admin/product/[i:id]/edit
[2] => admin/products/edit
)
[admin/products/update] => Array
(
[0] => POST
[1] => admin/product/update
[2] => admin/products/update
)
[admin/products/delete] => Array
(
[0] => POST
[1] => admin/product/delete
[2] => admin/products/delete
)
[admin/categories] => Array
(
[0] => GET
[1] => admin/categories
[2] => admin/categories/all
)
[admin/categories/add] => Array
(
[0] => GET
[1] => admin/category/add
[2] => admin/categories/add
)
[admin/categories/insert] => Array
(
[0] => POST
[1] => admin/category/insert
[2] => admin/categories/insert
)
[admin/categories/show] => Array
(
[0] => GET
[1] => admin/category/[i:id]
[2] => admin/categories/show
)
[admin/categories/edit] => Array
(
[0] => GET
[1] => admin/category/[i:id]/edit
[2] => admin/categories/edit
)
[admin/categories/update] => Array
(
[0] => POST
[1] => admin/category/update
[2] => admin/categories/update
)
[admin/categories/delete] => Array
(
[0] => POST
[1] => admin/category/delete
[2] => admin/categories/delete
)
[admin/api] => Array
(
[0] => GET
[1] => admin/api
[2] => admin/api/all
)
[admin/filter/sessions] => Array
(
[0] => POST|AJAX
[1] => admin/filter/sessions
[2] => admin/filters/sessions
)
This is $admin_routes array (build from $routes):
[login] => Array
(
[0] => admin/login
)
[logout] => Array
(
[0] => admin/logout
)
[dashboard] => Array
(
[0] => admin/dashboard
)
[products] => Array
(
[0] => admin/products
[1] => admin/products/add
[2] => admin/products/insert
[3] => admin/products/show
[4] => admin/products/edit
[5] => admin/products/update
[6] => admin/products/delete
)
[api] => Array
(
[0] => admin/api
)
[filter] => Array
(
[0] => admin/filter/sessions
)
[categories] => Array
(
[0] => admin/categories
[1] => admin/categories/add
[2] => admin/categories/insert
[3] => admin/categories/show
[4] => admin/categories/edit
[5] => admin/categories/update
[6] => admin/categories/delete
)
I need some help from you guys about $admin_routes.
I want to combine all sub-arrays from $admin_routes, from same area, which has only one element, in a new sub-array with key name formed from their key names.
Also i want the new sub-array to have same 'position' in line.
My imperfect solution:
// creating $admin_routes
$admin_routes = [];
foreach ($routes as $name => $route) {
if (preg_match('/^admin\/(.*)$/', $name))
$admin_routes[explode('/', $name)[1]][] = $name;
}
// imperfect solution for what i want
$single = false;
$waiting = [];
foreach ($admin_routes as $section => $routes) {
if (count($routes) == 1) { // remember all sub-arrays, with one single element, from same area
$single = true;
$waiting[$section] = $routes;
}
else if ($single) { // if found all sub-arrays, with one single element, from same area
$name = '';
$subarray = [];
foreach ($waiting as $section => $routes) {
// creating only one sub-array
$name .= ($section . ' ');
$subarray = array_merge($subarray, $routes);
unset($admin_routes[$section]);
}
// the problem is, sub-array it's added at the end of $admin_routes
// (not where sub-array's elements were before)
$admin_routes[$name] = $subarray;
$single = false;
$waiting = [];
}
}
The problem is, the new sub-arrays are placed at the end.
If you have any idea, please help me. Thx.
Using your actual result $admin_routes, you could loop through them and rebuild the desired array:
$real_routes = []; // The new array
$singles_key = ''; // The key for combined sub-arrays
$singles_routes = []; // The routes for combined sub-arrays
foreach ($admin_routes as $name => $r) {
/* If this route array has a single entry,
add its name and value to our temp vars */
if (count($r) === 1) {
$singles_key .= $name . ' ';
$singles_routes = array_merge($singles_routes, $r);
} else {
/* If then a route array has multiple value,
save the combined singles and then save this one */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
$singles_key = '';
$singles_routes = [];
}
$real_routes[$name] = $r;
}
}
/* If the loop ends but the temp vars aren't empty,
save the lasts combined singles */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
}
// Then to output:
echo '<pre>' . print_r($real_routes, true) . '</pre>';

Creating a 2D array moves data to key position

I declare the following array
$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
I expect it to be created like
array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),
[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")
But when I print the array it looks like
Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )
[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )
[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )
I would like the array to be created in the same format as the second section of code.
All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this
$job_scope = array( "proposal_id" => array(),
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope" => array(),
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
It will produce this array
Array (
[proposal_id] => Array ( )
[will_provide] => Array ( [0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
[general_scope] => Array ( )
[per_bid] => Array ( [0] => Yes
[1] => No
[2] => Omit
)
[job_type] => Array ( [0] => Painting
[1] => Sandblasting
[2] => Scappling
))
If you want to callback the value, (ex : call supervision value).
All you need to do is
print_r($job_scope['will_provide'][1])
and that will print the supervision value
use $new_job_scope = array_values($job_scope);
$job_scope = array(
"proposal_id",
"will_provide" => array(
"0",
"Supervision",
"Labor",
"Material",
"Equpment"
),
"general_scope",
"per_bid" => array(
"Yes",
"No",
"Omit"
),
"job_type" => array(
"Painting",
"Sandblasting",
"Scappling"
)
);
$new_job_scope = array_values($job_scope);
print_r($new_job_scope);
PhpFiddle
Create array first !!! Reassign at specified index with 2D array will be more clear to me
<?php
$arr = array('proposal_id','','general_scope','',''); //create array first
$arr[1] = array("will_provide" => array("0","Supervision","Labor","Material","Equpment"));
$arr[3] = array("per_bid" => array("Yes","No", "Omit"));
$arr[4] = array("job_type" => array("Painting","Sandblasting","Scappling"));
var_dump($arr);
?>
I think this process can serve you. I have just used a foreach loop to convert non-int key to int key:
$new_array = '';
foreach($job_scope as $k => $v){
if(is_int($k)){
$new_array[] = $v;
}else{
$new_array[] = [$k => $v];
}
}
print_r($new_array);
Output would be:
Array
(
[0] => proposal_id
[1] => Array
(
[will_provide] => Array
(
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
)
[2] => general_scope
[3] => Array
(
[per_bid] => Array
(
[0] => Yes
[1] => No
[2] => Omit
)
)
[4] => Array
(
[job_type] => Array
(
[0] => Painting
[1] => Sandblasting
[2] => Scappling
)
)
)

How to sum the numbers of two arrays

This is probably not so hard, but I have searched for a while with no luck so any help will be appreciated.
I'm working in PHP with two arrays which contain a number of multidimensional arrays each with a set of two values. The 2 arrays looks something like this:
Array
(
[0] => Array
(
[0] => 3206.63
[1] => 815.47
)
[1] => Array
(
[0] => 5024.71
[1] => 803.73
)
[2] => Array
(
[0] => 3290.36
[1] => 625.02
)
//...
)
Array
(
[0] => Array
(
[0] => 3138.87
[1] => 819.8
)
[1] => Array
(
[0] => 5000.24
[1] => 810.87
)
[2] => Array
(
[0] => 3221.15
[1] => 668.58
)
//...
)
And I need to achieve this:
Array
(
[0] => Array
(
[0] => 6345.5
[1] => 1635.27
)
[1] => Array
(
[0] => 10024.95
[1] => 1614.6
)
[2] => Array
(
[0] => 6511.51
[1] => 1293.6
)
//...
)
This should work for you:
(Here i loop though each innerArray with foreach and use array_sum() to get the sum of all values of each innerArray)
<?php
$arr = array(
array(
3206.63,
815.47
),
array(
5024.71,
803.73
),
array(
3290.36,
625.02
)
);
$result = array();
foreach($arr as $v)
$result[] = array_sum($v);
print_r($result);
?>
Output:
Array ( [0] => 4022.1 [1] => 5828.44 [2] => 3915.38 )
If you have 2 Arrays you can use this:
$result = array();
foreach($arr1 as $k => $v)
$result[] = array_sum($arr1[$k]) + array_sum($arr2[$k]);
print_r($result);
EDIT:
From your updated question, this should work for you:
<?php
$arr1 = array(
array(
3206.63,
815.47
),
array(
5024.71,
803.73
),
array(
3290.36,
625.02
)
);
$arr2 = array(
array(
3138.87,
819.8
),
array(
5000.24,
810.87
),
array(
3221.15,
668.58
)
);
$sums = array();
foreach ($arr1 as $key => $value) {
$sums[$key][] = $arr1[$key][0] + $arr2[$key][0];
$sums[$key][] = $arr1[$key][1] + $arr2[$key][1];
}
print_r($sums);
?>
Output:
Array
(
[0] => Array
(
[0] => 6345.5
[1] => 1635.27
)
[1] => Array
(
[0] => 10024.95
[1] => 1614.6
)
[2] => Array
(
[0] => 6511.51
[1] => 1293.6
)
)

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.

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

Categories