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.
Related
This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
here's how it looks in the PHP code:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
print_r($array);
?>
Example of print_r() function output:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml
)
[1] => Array
(
[name] => filter_amount
[value] => 200-ml
)
[2] => Array
(
[name] => page_size
[value] => 7
)
)
I need to combine duplicates of filter_amount values from the array.
The values of these duplicates must be commas separated and the result should be the following code:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml,200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
[2] => Array
(
[name] => orderby
[value] => rating
)
[3] => Array
(
[name] => paged
[value] => 1
)
)
Since you want value to be concatenated by a comma, you'll have to make a cycle of it
<?php
//Allow me to change this variable name, just to not create confusion
$content = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
//Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array
//consisting in key => value where the key is 'name' and equals
//what we look for that is(filter_ammount)?
$key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//If not, let's place this array in the $output array
if ($key === false) {
array_push($outputArray, $innerArray);
} else {
//If exists, then $key is the $key of the $outputArray and let's add to its value
//our current value, that is in our $innerArray, concatenated with a comma
$outputArray[$key]['value'] .= ",". $innerArray['value'];
}
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>
References
http://php.net/manual/en/function.array-search.php
http://php.net/manual/en/function.array-column.php
How to combine two items by 2 duplicate columns?
[root#localhost TEST]# php R00.php
Array
(
[0] => Array
(
[0] => S01
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445
)
[1] => Array
(
[0] => S02
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[2] => Array
(
[0] => S03
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 23
)
[4] => Array
(
[0] => S05
[1] => 100.100.100.100
[2] => 192.168.100.100
[3] => 22
)
[5] => Array
(
[0] => S06
[1] => 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[6] => Array
(
[0] => S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[7] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[8] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[9] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
[root#localhost TEST]#
combine 1 or 2 items by 2 column duplicate below is result I need
Array
(
[0] => Array
(
[0] => S01 , S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445 , 23
)
[1] => Array
(
[0] => S02 , S03 , S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S05 , S06
[1] => 100.100.100.100 , 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[4] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[5] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[6] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
Try this:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$tmp = array();
foreach($array as $val) {
$tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
$item = implode(',', array_unique(explode(',', implode(',',$v['values']))));
$newArr[] = array('name' => $k, 'value' => $item);
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
got it with the following crazy mess:
$name = array_column($array, 'name');
$value = array_column($array, 'value');
foreach($name as $nk=>$nv)
foreach($value as $vk=>$vv)
if($nk == $vk)
$a[$nv][] = $vv;
foreach($a as $k=>$v)
$b[$k] = implode(',', $v);
$z = 0;
foreach($b as $k=>$v)
{
$c[$z]['name'] = $k;
$c[$z]['value'] = $v;
$z++;
}
$c is the resulting array
Or using a medley of array functions:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];
foreach (array_unique($names) as $k)
$result[$k] = implode(", ", array_filter($values,
function($v, $indx) use ($names, $k) {
return $names[$indx] == $k;
}, ARRAY_FILTER_USE_BOTH));
print_r($result);
$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];
print_r($result2);
Results in:
Array
(
[filter_amount] => 100-ml, 200-ml
[page_size] => 7
)
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml, 200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
)
All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.
Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.
Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).
Remove the temporary keys when the loop is finished by calling array_values().
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['name']])) {
$result[$row['name']] = $row;
} else {
$result[$row['name']]['value'] .= ',' . $row['value'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'name' => 'filter_amount',
'value' => '100-ml,200-ml',
),
1 =>
array (
'name' => 'page_size',
'value' => '7',
),
)
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
)
)
)
This is the result of array after i build it using array_push function from mssql result.
Array
(
[0] => Array
(
[STICKER] => FALCON
[MONTH] => 1
[JUM] => 65826210.00
)
[1] => Array
(
[STICKER] => FALCON
[MONTH] => 2
[JUM] => 68070573.00
)
[2] => Array
(
[STICKER] => FALCON
[MONTH] => 3
[JUM] => 99053067.60
)
[3] =>
[4] => Array
(
[STICKER] => HRD
[MONTH] => 2
[JUM] => 1521400.00
)
[5] => Array
(
[STICKER] => HRD
[MONTH] => 3
[JUM] => 2093200.00
)
)
I need to convert array above into this structure:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210.00
[2] => 68070573.00
[3] => 99053067.60
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400.00
[3] => 2093200.00
)
)
Note:
Array[0] values would be 1,2,3 (this is actualy month, i just input up to 3 in order to get the code not too long. but it will be up to 12 (Jan - Dec)).
If from original array, there is none value (example from array[3]), then it will be convert to new array[2]->[1] with value 0.
Any help would be very appreciated.
Thanks all!.
Try this,
If any month is not mentioned from stickers the jum considered as 0,
$array = array(
array('STICKER' => 'FALCON', 'MONTH' => 1, 'JUM' => 65826210.00),
array('STICKER' => 'FALCON', 'MONTH' => 2, 'JUM' => 68070573.00),
array('STICKER' => 'FALCON', 'MONTH' => 3, 'JUM' => 99053067.60),
array(),
array('STICKER' => 'HRD', 'MONTH' => 2, 'JUM' => 1521400.00),
array('STICKER' => 'HRD', 'MONTH' => 3, 'JUM' => 2093200.00),
);
$result[0][] = '';
foreach ($array as $key => $res) {
if (!empty($res)) {
$result[0][$res['MONTH']] = $res['MONTH'];
$result1[$res['STICKER']][$res['MONTH']] = $res['JUM'];
}
}
foreach ($result1 as $key => $res) {
$fin = array();
foreach ($res as $key1 => $re) {
foreach ($result[0] as $key2 => $month) {
if ($month != '') {
if (array_key_exists($month, $res)) {
if (!array_key_exists($month, $fin) && $month == $key1) {
$fin[$month] = $re;
}
} else {
$fin[$month] = 0;
}
}
}
}
$result[] = array_merge(array($key), $fin);
}
echo'<pre>';
print_r($result);
echo'<pre>';
Result:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210
[2] => 68070573
[3] => 99053067.6
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400
[3] => 2093200
)
)
I hope this is used to achieve your output(you mentioned in above question)!!
try something like this
not the issue with the empty array i could not understand how the program would know that it is STICKER HRD so it will be filled with zero.But you could later check if every month "isset" and if it is not act as it is zero
$arr1=array(
array('STICKER'=>'FALCON','MONTH'=>1,'JUM'=>65826210.00),
array('STICKER'=>'FALCON','MONTH'=>2,'JUM'=>68070573.00),
array('STICKER'=>'FALCON','MONTH'=>3,'JUM'=>99053067.60),
array(),
array('STICKER'=>'HRD','MONTH'=>2,'JUM'=>1521400.00),
array('STICKER'=>'HRD','MONTH'=>3,'JUM'=>2093200.00),
);
$arr2=array();
$arr3=array();
$arr2[0][0]="";
foreach($arr1 as $key => $val){
if(!empty($val)){
$arr2[0][$val['MONTH']]=$val['MONTH'];
//group each STICKER
$arr3[$val['STICKER']][]=$val['JUM'];
}
}
//transfer the grouped data to arr2
foreach($arr3 as $key => $val){
$tmp_arr=array($key);
$arr2[]=array_merge($tmp_arr,$val);
}
print_r($arr2);
I am working with a permission system where I have to convert a single level array (separated by underscores) to a multidimentional array. Any help with a function that fix this?
Input array:
Array
(
[0] => dashboard
[1] => dashboard_read
[2] => dashboard_update
[3] => dashboard_create
[4] => dashboard_delete
[5] => dashboard_search
[6] => timeplan_event_read
[7] => timeplan_event_search
[8] => timeplan_read
[9] => timeplan_search
[10] => webhotel
[11] => webhotel_read
[12] => webhotel_update
[13] => webhotel_create
[14] => webhotel_delete
[15] => webhotel_search
)
Output array:
array(
'dashboard' => array(
'read',
'update',
'create',
'delete',
'search'
),
'timeplan' =>array(
'read',
'search',
'event' => array(
'read',
'search'
)
),
'webhotel' =>array(
'read',
'update',
'create',
'delete',
'search'
),
)
You can iterate through your array and explode on '_'-character. After this you can simple build up your new array in your desired treeformat.
<?php
$tmp = array("dashboard","dashboard_read","dashboard_update","dashboard_create","dashboard_delete","dashboard_search","timeplan_event_read","timeplan_event_search","timeplan_read","timeplan_search","webhotel","webhotel_read","webhotel_update","webhotel_create","webhotel_delete","webhotel_search");
$newarray = array();
foreach ($tmp as $value) {
$parts = explode("_", $value);
if (count($parts) < 2)
continue;
$arr = &$newarray;
for ($i=0;$i<count($parts)-1;$i++) {
if (!array_key_exists($parts[$i], $arr)) {
$arr[$parts[$i]] = array();
}
$arr = &$arr[$parts[$i]];
}
$arr[] = $parts[count($parts)-1];
}
print_r($newarray);
--- Output ---
$ php -f tmp.php
Array
(
[dashboard] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
[timeplan] => Array
(
[event] => Array
(
[0] => read
[1] => search
)
[0] => read
[1] => search
)
[webhotel] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
)
[akshay#localhost tmp]$ cat test.php
<?php
$array = array("dashboard","dashboard_read","dashboard_update","dashboard_create","dashboard_delete","dashboard_search","timeplan_event_read","timeplan_event_search","timeplan_read","timeplan_search","webhotel","webhotel_read","webhotel_update","webhotel_create","webhotel_delete","webhotel_search");
function build_arr($array, $delim='_')
{
$output = array();
foreach($array as $key)
{
$main = explode($delim, $key);
if (count($main) < 2)
continue;
$bottom = &$output;
while(count($main) > 1)
{
$sub = array_shift($main);
if (!isset($bottom[$sub]))
{
$bottom[$sub] = array();
}
$bottom = &$bottom[$sub];
}
$bottom[] = $main[count($main)-1];
}
return $output;
}
// Input
print_r( $array);
// Output
print_r( build_arr($array) );
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => dashboard
[1] => dashboard_read
[2] => dashboard_update
[3] => dashboard_create
[4] => dashboard_delete
[5] => dashboard_search
[6] => timeplan_event_read
[7] => timeplan_event_search
[8] => timeplan_read
[9] => timeplan_search
[10] => webhotel
[11] => webhotel_read
[12] => webhotel_update
[13] => webhotel_create
[14] => webhotel_delete
[15] => webhotel_search
)
Array
(
[dashboard] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
[timeplan] => Array
(
[event] => Array
(
[0] => read
[1] => search
)
[0] => read
[1] => search
)
[webhotel] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
)
I'd had the same problem times ago, but I don't know how the code exactly looks like. My idea was to write a recursive function, which explode('_', $string) the current permission, and iterates through the parts. And there it will do something like
if count $parts === 1:
$result[] = $parts
else:
$result[$parts[0]] = recursive call ($parts, $result)
I hope that this will bring you on the right way...
No depth limit.
$arr = [];
while($path = array_pop($x))
{
$parts = explode('_',$path);
$value = array_pop($parts);
$cell = &$arr;
while($part = array_shift($parts)){
if (!isset($cell[$part])) $cell[$part] = [];
$cell = &$cell[$part];
}
if (!array_key_exists($value, $cell))
array_push($cell, $value);
}
Output:
array:3 [
"webhotel" => array:5 [
0 => "search"
1 => "delete"
2 => "create"
3 => "update"
4 => "read"
]
"timeplan" => array:3 [
0 => "search"
1 => "read"
"event" => array:2 [
0 => "search"
1 => "read"
]
]
"dashboard" => array:5 [
0 => "search"
1 => "delete"
2 => "create"
3 => "update"
4 => "read"
]
]
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,
)