i have created a function in php to convert an string like:
[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 20110924, [20110926, 20110927], 20110929]
to php array like:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
[8] => Array
(
[0] => 20111001
[1] => 20111002
)
[9] => 20111004
[10] => Array
(
[0] => 20111006
[1] => 20111007
)
)
The function is:
function dates2Array($d){
if($d!==''){
$d=substr($d, 1, strlen($d)-2);
$d=explode(', ', $d);
$dates=array();
if(!empty($d)){
$j=1;
foreach($d as $k=>$v){
if(substr($v, 0, 1)==='[') $dates[]=array(substr($v, 1, strlen($v)));
elseif(substr($v, strlen($v)-1, strlen($v))===']'){
$dates[$k-$j][1]=substr($v, 0, strlen($v)-1);
$j++;
}
else $dates[]=$v;
}
}
}
return $d!==''?$dates:'';
}
I am not fully happy with my function.
I think it can be more optimized and compressed for speed..
Can it be?
Use JSON (json_decode() and json_encode()) instead
http://sandbox.phpcode.eu/g/b3814/2
result:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
)
You can pass your string in {...} and pass it to json_decode(str, true) to get back an array.
>> json_decode("[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 2
0110924, [20110926, 20110927], 20110929]")
array (
0 => 20110911,
1 => 20110913,
2 =>
array (
0 => 20110915,
1 => 20110918,
),
3 => 20110920,
4 => 20110922,
5 => 20110924,
6 =>
array (
0 => 20110926,
1 => 20110927,
),
7 => 20110929,
)
Related
I found lots of answers on stackoverflow and other sites relating to arrays but they don't seem to be working with my problem.
I have the following code:
Array (
"-10 to -5" => Array ( [0] => 44.78 [1] => 46.86 [2] => 20.64 )
"-20 to -15" => Array ( [0] => 8.01 [1] => 7.85 [2] => 21.08 )
"-5 to 5" => Array ( [0] => 3.5 [1] => 0.8 [2] => 0.12 )
"-15 to -10" => Array ( [0] => 43.25 [1] => 43.95 [2] => 56.02 )
"-25 to -20" => Array ( [0] => 0.45 [1] => 0.54 [2] => 2.15 )
);
How can I sort it by its keys so that it becomes something like:
Array (
"-25 to -20" => Array ( [0] => 0.45 [1] => 0.54 [2] => 2.15 )
"-20 to -15" => Array ( [0] => 8.01 [1] => 7.85 [2] => 21.08 )
"-15 to -10" => Array ( [0] => 43.25 [1] => 43.95 [2] => 56.02 )
"-10 to -5" => Array ( [0] => 44.78 [1] => 46.86 [2] => 20.64 )
"-5 to 5" => Array ( [0] => 3.5 [1] => 0.8 [2] => 0.12 )
);
Please suggest any solution. I tried alot and I still can't figure it out.
Thanks.
$data = [
'-10 to -5' => [ 44.78, 46.86, 20.64 ],
'-20 to -15' => [ 8.01, 7.85, 21.08 ],
'-5 to 5' => [ 3.5, 0.8, 0.12 ],
'-15 to -10' => [ 43.25, 43.95, 56.02 ],
'-25 to -20' => [ 0.45, 0.54, 2.15 ]
];
uksort($data, fn($key1, $key2) => strtok($key1, ' ') <=> strtok($key2, ' '));
print_r($data);
Try this:
ksort($array, SORT_NUMERIC);
I am trying to fetch values from few - dynamically created HTML FORM & in action file, i am grabbing those values via $_POST. Please consider humbly, that i am using array in dynamic inputs like :
<input name="array1[]" />
<input name="array2[]" />
<input name="array3[]" />
So, that, after FORM SUBMIT, in action file, its giving :
$a = Array
(
[0] => 04/21/2017
[1] => 04/19/2017
[2] => 04/25/2017
[3] => 04/25/2017
[4] => 10/25/2017
)
$b= Array
(
[0] => 11
[1] => 34
[2] => 12
[3] => 12
[4] => 2
)
$c= Array
(
[0] => fghgthg
[1] => ggfg
[2] => fgfgfdgf
[3] => fgfdgdgfgdh
[4] => rgrgfgf
)
Now, m having troubles in arranging this way :
$FinalArray = array(
array($a[0], $b[0], $c[0]),
array($a[1], $b[1], $c[1]),
array($a[2], $b[2], $c[2]),
array($a[3], $b[3], $c[3]),
......last line without comma
);
And submitting in mysql table, so that i can easily retrieve it like :
step-1 : $FinalArray[0];
step-2 : $FinalArray[1];
.......goes on
Thanks, for help, in Advance.
This simple foreach help you in achieving your desired output.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$a = Array
(
0 => "04/21/2017",
1 => "04/19/2017",
2 => "04/25/2017",
3 => '04/25/2017',
4 => "10/25/2017"
);
$b = Array
(
0 => 11,
1 => 34,
2 => 12,
3 => 12,
4 => 2
);
$c = Array
(
0 => "fghgthg",
1 => "ggfg",
2 => "fgfgfdgf",
3 => "fgfdgdgfgdh",
4 => "rgrgfgf",
);
$result=array();
foreach($a as $key => $value)
{
$result[]=array($value,$b[$key],$c[$key]);
}
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 04/21/2017
[1] => 11
[2] => fghgthg
)
[1] => Array
(
[0] => 04/19/2017
[1] => 34
[2] => ggfg
)
[2] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfgfdgf
)
[3] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfdgdgfgdh
)
[4] => Array
(
[0] => 10/25/2017
[1] => 2
[2] => rgrgfgf
)
)
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
)
)
)
I have following array with php code
I could not find where I am mistaken
I'm trying to filter some of these array results and delete them. When I try to list them I could not succeded
array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
4 => 'kabeduvarkad.jpg',
5 => 'kabeduvarkad1-1000x288.jpg',
6 => 'kabeduvarkad1-1024x768.jpg',
7 => 'kabeduvarkad1-150x150.jpg',
8 => 'kabeduvarkad1-300x225.jpg',
9 => 'kabeduvarkad1-400x300.jpg',
10 => 'kabeduvarkad1.jpg',
11 => 'kabeduvarkad2-1000x288.jpg',
12 => 'kabeduvarkad2-1024x768.jpg',
13 => 'kabeduvarkad2-150x150.jpg',
14 => 'kabeduvarkad2-300x225.jpg',
15 => 'kabeduvarkad2-400x300.jpg',
16 => 'kabeduvarkad2.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
)
This is my function
function listArrayRecursive($array_name, $ident = ''){
$result = array();
foreach ($array_name as $k => $v){
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
}else{
$result[] = $ident. '/' . $v . '<br>';
}
}
return $result;
}
I have following result
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[1] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[2] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[3] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad2-1000x288.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad2-1024x768.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad2-150x150.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad2-300x225.jpg<br>
[15] => /wp-content/uploads/2013/05/kabeduvarkad2-400x300.jpg<br>
[16] => /wp-content/uploads/2013/05/kabeduvarkad2.jpg<br>
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
)
)
)
)
[5] => Array
(
[0] => /wp-update/wp-update.tar<br>
[1] => /wp-update/wp-update.tar.gz<br>
[2] => /wp-update/wp-update1.tar<br>
[3] => /wp-update/wp-update1.tar.gz<br>
)
[6] => /wp-update.tar.gz<br>
)
Expected Result is
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
...
[110] => /wp-update/wp-update.tar<br>
[111] => /wp-update/wp-update.tar.gz<br>
[112] => /wp-update/wp-update1.tar<br>
[113] => /wp-update/wp-update1.tar.gz<br>
[114] => /wp-update.tar.gz<br>
)
You can do it like this:
<?php
// Dummy data source
$data = array(
'/do-update.php',
'/sitemap.xml',
'/sitemap.xml.gz',
'/wp-config.php',
array(
array(
array(
'/wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>'
)
)
)
);
// Helper function
function getFiles($data, &$fileList) {
foreach ($data as $dataItem) {
if (is_array($dataItem))
getFiles($dataItem, $fileList);
else
$fileList[] = $dataItem;
}
}
// Debug
echo "<b>Orignal Array</b>";
var_dump($data);
echo "<hr>";
// Helper function usage
echo "<b>Parsed Array</b>";
$fileList = array();
getFiles($data, $fileList);
var_dump($fileList);
?>
Output:
Ok use this function its working
$all=array();
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $v) {
$all[]=$v;
}
print_r($all);
With $var[] = you basically say that you want to add a new Element to $var with an incremented key.
Your Recursive frunction returns an array.
So this array is assigned as a new element in your array.
But what you want is a flat array.
Instead of adding an array to your array like this:
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
Merge the existing arrays like this:
if (is_array($v)){
$tmpResult = listArrayRecursive($v, $ident.'/'.$k);
$result = array_merge($result, $tmpResult);
You can see a working example here.
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.