so, i have this
Array
(
[ModuleCode] => Array
(
[0] => MD001
[1] => MD002
[2] => MD004
[3] => MD005
)
[MD001] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD002] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD005] => Array
(
[insert] => on
[edit] => on
[delete] => on
[access_edit] => on
)
)
as you can see there are an array with ModuleCode as key.
After some try i can get this
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
MD002
insert => 1
edit => 1
delete => 1
access_edit => 0
MD004
insert => 0
edit => 0
delete => 0
access_edit => 0
MD005
insert => 1
edit => 1
delete => 1
access_edit => 1
with this script
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
echo "<pre>".$dataModul[$x] . "<br>";
for($a=0;$a<count($field);$a++){
$subcheck = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
echo $field[$a]. " => " . $subcheck . "<br>" ;
}
echo "<pre>";
}
Ok, here is what i want to achieve . from this part (for an example)
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
i want to make something like this
Update TableName set insert = 1, edit = 1, delete = 1 , access_edit = 0 where ModuleCode = 'MD001'
How can i achieve that ? thanks in advance
You can try this code:
You can call use the function as echo generate_query_string('MD004', $modules); where the first parameter is the module code and the second the whole array.
<?php
function generate_query_string( $module, $module_arr ) {
if( !isset( $module_arr[$module] ) ) { return false; } // return false if module does not exist
// set default values
$defaults = array(
'insert' => 0,
'edit' => 0,
'delete' => 0,
'access_edit' => 0
);
$settings = array_merge( $defaults, $module_arr[$module] ); // merge default values and the actual values
$settings = array_filter( $settings ); // remove items with 0 value since we don't need to include them on the query string
// render the query string values
$values = [];
foreach ($settings as $key => $value) {
$value = ( $value == 'on' )? 1 : 0;
$values[] = $key. ' = '. $value;
}
$values = implode(', ', $values);
return 'Update TableName set '. $values .' where ModuleCode = '. $module;
}
?>
I found the solustion. Here is what i do
First. i add a custom function ( ref : https://stackoverflow.com/a/42052020/6354277 )
function custom_function($input_array)
{
$output_array = array();
for ($i = 0; $i < count($input_array); $i++) {
for ($j = 0; $j < count($input_array[$i]); $j++) {
$output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
}
}
return $output_array;
}
then i change my code to this.
function updateaccess(){
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
for($a=0;$a<count($field);$a++){
$subcheck[$a] = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
$mynewarray[$dataModul[$x]][] = array($field[$a] => $subcheck[$a]);
}
foreach ($mynewarray as $key => $value) {
$forSave[$dataModul[$x]] = $this->custom_function($value);
}
}
foreach ($forSave as $key2 => $values2) {
$this->mainmodel->updateRow(array("ModuleCode" => $key2),"user_modules", $values2 );
}
}
Related
I am using Spout Excel reader to read Excel files from php code and saving into a multidimensional array in PHP variable,Array looks like this
$array = [
[
'id[0]' => 'BX-78',
'Name[0]' => 'XXX',
'Address[0]' => 'YUUSATD'
],
[
'id[1]' => 'BX-79',
'Name[1]' => 'YYY',
'Address[1]' => 'DHJSHDJGY'
],
[
'id[2]' => 'BX-80',
'Name[2]' => 'ZZZ',
'Address[2]' => 'DDSDSDA'
]
[
'id[3]' => 'BX-78',
'Name[3]' => 'AAA',
'Address[3]' => 'FSDSDS'
][
'id[4]' => 'BX-81',
'Name[4]' => 'XXX',
'Address[4]' => 'DSDSDSD'
]];
Now i want to show duplicate data from above array using two keys ['id'] and ['name'] if id repeats show as duplicate data,
If name repeats show that row as duplicate data if both are duplicate show as again duplicate row
Otherwise it is unique row.
I have tried using multidimensional array sorting but it is using only one key to match data in rows.
foreach ($arrExcelData as $v) {
if (isset($arrExcelData[$v[0]])) {
// found duplicate
continue;
}
// remember unique item
$arrExcelData3[$v[0]] = $v;
}
// if you need a zero-based array, otheriwse work with $_data
$arrExcelData2 = array_values($arrExcelData3);
Edited : Expected Output Result :
Matching Rows:
Id Name Address
-------------------------
BX-78 XXX YUUSATD
BX-78 AAA DDSDSDA
BX-81 XXX DSDSDSD`
If you want to list the duplicate values, I think the address of the second match should be FSDSDS as there is not item with name AAA and value DDSDSDA:
BX-78 AAA FSDSDS
If that is the case, what you could do is to first use a double foreach to mark the arrays that contain a duplicate id or name by for example adding a property named id and name except when the array is itself in the second loop.
After this loop, you can tell which arrays are the duplicate ones. Instead of using a corresponding index 0 as in id[0], I have used reset and next so it is not tied to these indexes.
To get the filtered result you could use array_reduce to check for the array keys and unset them.
For example:
foreach ($array as $index => $a) {
foreach ($array as $v) {
if ($v === $a) continue;
if (reset($v) === reset($a)) $array[$index]["id"] = "duplicate";
if (next($v) === next($a)) $array[$index]["name"] = "duplicate";
}
}
$array = array_reduce($array, function($carry, $item) {
if (array_key_exists("id", $item) || array_key_exists("name", $item)) {
unset($item["id"], $item["name"]);
$carry[] = $item;
}
return $carry;
}, []);
print_r($array);
Result
Array
(
[0] => Array
(
[id[0]] => BX-78
[Name[0]] => XXX
[Address[0]] => YUUSATD
)
[1] => Array
(
[id[3]] => BX-78
[Name[3]] => AAA
[Address[3]] => FSDSDS
)
[2] => Array
(
[id[4]] => BX-81
[Name[4]] => XXX
[Address[4]] => DSDSDSD
)
)
See a php demo
I've this very pragmatic approach:
$spout_output = [
[
'id[0]' => 'BX-78',
'Name[0]' => 'XXX',
'Address[0]' => 'YUUSATD'
],
[
'id[1]' => 'BX-79',
'Name[1]' => 'YYY',
'Address[1]' => 'DHJSHDJGY'
],
[
'id[2]' => 'BX-80',
'Name[2]' => 'ZZZ',
'Address[2]' => 'DDSDSDA'
],
[
'id[3]' => 'BX-78',
'Name[3]' => 'AAA',
'Address[3]' => 'FSDSDS'
],
[
'id[4]' => 'BX-81',
'Name[4]' => 'XXX',
'Address[4]' => 'DSDSDSD'
]];
// store id to row, and name to row mappings.
// id and name will be keys, value will be an array of indexes of the array $spout_output
$id_to_rows = array();
$name_to_rows = array();
$duplicate_ids = array();
$duplicate_names = array();
foreach($spout_output as $row => $data)
{
$key_id = 'id['.$row.']';
$key_name = 'Name['.$row.']';
if(!isset($data[$key_id]))
continue;
$value_id = $data[$key_id];
$value_name = $data[$key_name];
if(!isset($id_to_rows[$value_id]))
{
$id_to_rows[$value_id] = array();
}
else
{
if(!isset($duplicate_ids[$value_id]))
{
$duplicate_ids[$value_id] = $id_to_rows[$value_id];
}
$duplicate_ids[$value_id][] = $row;
}
if(!isset($name_to_rows[$value_name]))
{
$name_to_rows[$value_name] = array();
}
else
{
if(!isset($duplicate_names[$value_name]))
{
$duplicate_names[$value_name] = $name_to_rows[$value_name];
}
$duplicate_names[$value_name][] = $row;
}
$id_to_rows[$value_id][] = $row;
$name_to_rows[$value_name][] = $row;
}
echo 'Duplicates:';
echo '<br>';
$shown_rows = array();
foreach($duplicate_ids as $id => $rows)
{
foreach($rows as $nr)
{
echo $id . '|' . $spout_output[$nr]['Name['.$nr.']'] . '|' . $spout_output[$nr]['Address['.$nr.']'];
echo '<br>';
$shown_rows[] = $nr;
}
}
foreach($duplicate_names as $name => $rows)
{
foreach($rows as $nr)
{
// if already shown above, skip this row
if(in_array($nr, $shown_rows))
continue;
echo $spout_output[$nr]['id['.$nr.']'] . '|' . $spout_output[$nr]['Name['.$nr.']'] . '|' . $spout_output[$nr]['Address['.$nr.']'];
echo '<br>';
$shown_rows[] = $nr;
}
}
Outputs:
Duplicates:
BX-78|XXX|YUUSATD
BX-78|AAA|FSDSDS
BX-81|XXX|DSDSDSD
I think your 'wanted output' contains an error in the address?
Anyway, with my code above I think you'll have enough mapped data to produce the output you want.
You could do something like this:
$dupes = [];
$current = [];
foreach ($array as $index => $entry) {
$idKey = "id[$index]";
$nameKey = "Name[$index]";
if (array_key_exists($entry[$idKey], $current)) {
$dupes[] = [$entry, $current[$entry[$idKey]]];
}
elseif (array_key_exists($entry[$nameKey], $current)) {
$dupes[] = [$entry, $current[$entry[$nameKey]]];
}
else {
$current[$entry[$idKey]] = $current[$entry[$nameKey]] = $entry;
}
}
print_r($dupes);
Which results in an array containing each set of duplicates (array of arrays):
Array
(
[0] => Array
(
[0] => Array
(
[id[3]] => BX-78
[Name[3]] => AAA
[Address[3]] => FSDSDS
)
[1] => Array
(
[id[0]] => BX-78
[Name[0]] => XXX
[Address[0]] => YUUSATD
)
)
[1] => Array
(
[0] => Array
(
[id[4]] => BX-81
[Name[4]] => XXX
[Address[4]] => DSDSDSD
)
[1] => Array
(
[id[0]] => BX-78
[Name[0]] => XXX
[Address[0]] => YUUSATD
)
)
)
Demo here: https://3v4l.org/JAtNU
In case someone of you are searching unique values by key.
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
This function just takes multidimensional array and key value of field you need.
Then takes value of given array one by one (smaller arrays).
Then traverses given array and looking if taken key-value pair matches with given key.
After that if taken key-value pair matches with given key function just inserts smaller array in temporary array (array with unique values).
Don't forget to increment indexes of arrays ($i).
Then return array you got (with unique values) after function ends work.
i have a query inside a for loop that getting the product name of every array element. Now in every element of my array, i have an ID, where i want to concat all product names with the-same shipping_id.
Here i have my array with values like these:
Array name:id with values of:
Array
(
[0] => Array
(
[product_id] => 1
[shipping_id] => 1
)
[1] => Array
(
[product_id] => 2
[shipping_id] => 1
)
[2] => Array
(
[product_id] => 1
[shipping_id] => 2
)
)
now i made this code with these:
$first = true;
$temp_ship_id = "";
$product_list = "";
foreach ($ids as $product) {
$productname = $this->getproductname($product[0][product_id]);
// if($first) {
// $temp_ship_id = $product[0][shipping_id];
// $first = false;
// }
// if($product[0][shipping_id] == $temp_ship_id) {
// $product_list .= $productname.";
// } else {
// $product_list .= $productname.";
// //$product_list = "";
// $temp_ship_id = $product[0]->shipping_id;
// }
}
public function getproductname($product_id) {
$product = DB::table('products')->select('product_name')
->where(['products.product_id'=>$product_id])
->first();
return $product->product_name;
}
what am i doing is, i am getting the first shipping id and store it and i made a condition if they are thesame then i go concat the productname but, i see my logic is bad.
Please help me in other way. Something like This line of code to begin with:
foreach ($ids as $product) {
$productname = $this->getproductname($product[0][product_id]);
//code for concat goes here
}
public function getproductname($product_id) {
$product = DB::table('products')->select('product_name')
->where(['products.product_id'=>$product_id])
->first();
return $product->product_name;
}
Adjust below to your actual data, let me know if you have questions.
<?php
$concat = array();
$array = array( array( 'product_id'=>1, 'shipping_id'=>1, 'product_name' => 'a' ), array( 'product_id'=>2, 'shipping_id'=>1, 'product_name' => 'b' ), array( 'product_id'=>3, 'shipping_id'=>2, 'product_name' => 'c' ), array( 'product_id'=>4, 'shipping_id'=>1, 'product_name' => 'd' ) );
foreach( $array as $row ) {
if( isset( $concat[ $row['shipping_id'] ] ) ) {
$concat[ $row['shipping_id'] ] .= ',' . $row['product_name'];
} else {
$concat[ $row['shipping_id'] ] .= $row['product_name'];
}
}
var_dump( $concat );
?>
I have two string values namely $late_array and $wrong_array. The values are comma delimited.
What I would like to do is compare the two arrays and if the first two elements are the same add the value to the end else make it zero. The arrays I have:
$late_array = array(
[0] => 140610d,Richard,12
[1] => 140610a,Dave,22
[2] => 140610n,Noddy,121
[3] => 140610a,Nick,15
)
$wrong_array = array(
[0] => 140610d,Richard,2
[1] => 140610d,Mary,60
[2] => 140610a,Dave,11
[3] => 140610n,Noddy,90
)
The end result should be:
$combined_array = array(
[0] => 140610d,Richard,12,2
[1] => 140610d,Mary,0,60
[2] => 140610a,Dave,22,11
[3] => 140610a,Nick,15,0
[4] => 140610n,Noddy,121,90
)
I have so far formed a foreach and used the '===' operators to check if the date and name match then output as I want but I have not been able to get it to work if the name is not present in one array but another to make the value zero.
EDIT: Just to clear it up, hopefully. If the value is present in both arrays then the date,name,late value,wrong value should show. But if the value is present in late only then the value for wrong should be 0, same visa versa. Added "Nick" to try and explain a bit better.
This is what I did to solve the problem so far:
$wrong_val = array();
foreach($out as $wrong_value) {
$wrong_tosearch[] = substr($wrong_value,0,strrpos($wrong_value,","));
$w_id = substr($wrong_value,0,strrpos($wrong_value,","));
$wrong_val[$w_id] = substr($wrong_value,strrpos($wrong_value,",")+1,strlen($wrong_value));
}
foreach($sql_late_array as $late_value) {
$late_tosearch[] = substr($late_value,0,strrpos($late_value,","));
$l_id = substr($late_value,0,strrpos($late_value,","));
$late_val[$l_id] = substr($late_value,strrpos($late_value,",")+1,strlen($late_value));
}
$merge = array_merge($wrong_tosearch,$late_tosearch);
$sort = array_values(array_unique($merge));
$combined_array = array();
foreach ($sort as $search_val) {
if (array_key_exists($search_val,$wrong_val) !== FALSE) {
foreach ($wrong_val as $w_key=>$w_val) {
$combined_array[$w_key]['late'] = "0";
$combined_array[$w_key]['wrong'] = $w_val;
}
}
if (array_key_exists($search_val,$late_val) !== FALSE) {
foreach ($late_val as $l_key=>$l_val) {
$combined_array[$l_key]['wrong'] = "0";
$combined_array[$l_key]['late'] = $l_val;
}
}
}
print_r($combined_array);
Check if below code solves your problem.
$late_array = array(
"0" => "140610d,Richard,12",
"1" => "140610a,Dave,22",
"2" => "140610n,Noddy,121"
);
$wrong_array = array(
"0" => "140610d,Richard,2",
"1" => "140610d,Mary,60",
"2" => "140610a,Dave,11",
"3" => "140610n,Noddy,90"
);
foreach($wrong_array as $wv)
{
$tosearch = substr($wv,0,strrpos($wv,",")-1);
$valtoadd = substr($wv,strrpos($wv,",")+1,strlen($wv));
$added=false;
foreach($late_array as $lv)
{
if(strstr($lv, $tosearch) !== false)
{
$combined_array[] = $lv.",".$valtoadd;
$added=true;
break;
}
}
if(!$added)
$combined_array[] = $tosearch.",0,".$valtoadd;
}
$tcombined_array = $combined_array;
foreach($late_array as $wv)
{
$added = false;
foreach($tcombined_array as $cv)
if(strstr($cv,$wv))
$added = true;
if(!$added) $combined_array[] = $wv.",0";
}
print_r($combined_array);
May be big but works
<?php
$late_array = array(
0 => "140610d,Richard,12",
1 => "140610a,Dave,22",
2 => "140610n,Noddy,121",
);
$wrong_array = array(
0 => "140610d,Richard,2",
1 => "140610d,Mary,60",
2 => "140610a,Dave,11",
3 => "140610n,Noddy,90"
);
$pattern = "/[0-9]*[a-zA-Z]*,[0-9]*[a-zA-Z]*,/";
$combined_array = $late_array;
foreach($wrong_array as $wrong_index => $wrong_value){
foreach($late_array as $late_index => $late_value){
preg_match_all($pattern, $late_value, $late_matches);
preg_match_all($pattern, $wrong_value, $wrong_matches);
if($late_matches[0] == $wrong_matches[0]){
$explode = explode(',',$wrong_value);
$combined_array[$late_index] = $combined_array[$late_index].','.$explode[2];
$matchedValues[] = $wrong_index;
}
}
}
$unmatched_values = array_diff(array_keys($wrong_array), array_values($matchedValues));
foreach($unmatched_values as $key => $value){
$combined_array[] = $wrong_array[$value].',0';
}
echo '<pre>';
print_r($combined_array);
?>
I've got an array that uses UNIX timestamps for array keys. The array will typically hold data for anywhere from 15 minutes to maybe an hours worth of time, however there are only entries for seconds that have data.
Most of the data will be spread out, there will be occasional spans of data for consecutive seconds though. What I'd like to do, is retrieve the first and last second of the longest consecutive span of seconds in the array.
If I have this array
Array
(
[1276033307] => 119.0
[1276033331] => 281.8
[1276033425] => 28.2
[1276033431] => 88.2
[1276033432] => 196.2
[1276034207] => 205.5
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
[1276034230] => 79.8
)
I would either need the keys 1276034226 and 1276034228, or the following array returned.
Array
(
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
EDIT:
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$finalArray = array();
foreach($array as $k => $v){
$tempArrays = array();
$index = 0;
while(isset($array[$k + $index])){
$tempArrays[$k+$index] = $array[$k+$index++];
}
if(count($tempArrays) > count($finalArray))
$finalArray = $tempArrays;
}
print_r($finalArray);
the output is the same...
ORIGINAL:
Note: Only the first occurrence of the longest span will be recorded.
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$longspan = 0;
foreach($array as $k => $v){
$index = 1;
while(isset($array[$k+$index])){
$index++;
}
$curspan = --$index;
if($curspan > $longspan){
$longspan = $curspan;
$start = $k;
}
}
for($i=0; $i <= $longspan; $i++)
$results[$start + $i] = $array[$start + $i];
print_r($results);
Outputs:
Array (
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
This code does it in a single loop (i.e. is a Greedy algorithm):
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$long_arr = array();
$curr_arr = array();
$last_key = -1;
foreach($array as $k => $v) {
if ($k != $last_key + 1) {
$curr_arr = array();
}
$curr_arr[$k] = $v;
if (count($curr_arr) > count($long_arr)) {
$long_arr = $curr_arr;
}
$last_key = $k;
}
print_r($long_arr);
Array
(
[00000000017] => Array
(
[00000000018] => Array
(
[00000000035] => I-0SAYHADW4JJA
[00000000038] => I-RF10EHE25KY0
[00000000039] => I-8MG3B1GT406F
)
[00000000019] => I-7GM4G5N3SDJL
)
[00000000025] => Array
(
[00000000011] => I-HT34P06WNMGJ
[00000000029] => I-U5KKT1H8J39W
)
[00000000040] => I-GX43V2WP9KPD
[00000000048] => I-XM526USFJAH9
[00000000052] => I-M414RK3H987U
[00000000055] => I-GABD4G13WHX7
)
I have the above array and i want to create a treeview display..
any recommendation ?
I guess i have to elaborate furthe on my question..
I want to store those array according to the level of array..
Example , I want something look like this :
[level_1]=> 00000000017,00000000025,00000000040, 00000000048, 00000000052
[level_2]=> 00000000018,00000000019, 00000000011, 00000000029
[level_3]=> 00000000035, 00000000038, 00000000039
You want a modified breadth-first search. This has the correct results for your sample structure:
<?php
function BFTraverse(&$tree = NULL, $depth = 0)
{
if (empty($tree))
return FALSE;
$keys = array_keys($tree);
$struct["lvl_$depth"] = $keys;
foreach ($keys as $key)
{
if (is_array($tree[$key]))
{
$struct = array_merge_recursive($struct, BFTraverse($tree[$key], $depth + 1));
}
}
return $struct;
}
$data = array
('00000000017' => array
(
'00000000018' => array
(
'00000000035' => 'I-0SAYHADW4JJA',
'00000000038' => 'I-RF10EHE25KY0',
'00000000039' => 'I-8MG3B1GT406F'
),
'00000000019' => 'I-7GM4G5N3SDJL'
),
'00000000025' => array
(
'00000000011' => 'I-HT34P06WNMGJ',
'00000000029' => 'I-U5KKT1H8J39W'
),
'00000000040' => 'I-GX43V2WP9KPD',
'00000000048' => 'I-XM526USFJAH9',
'00000000052' => 'I-M414RK3H987U',
'00000000055' => 'I-GABD4G13WHX7'
);
$var = BFTraverse($data);
$i = 0;
foreach ($var as $level)
echo "Level " . ++$i . ': ' . implode(', ', $level) . "\n";
?>
The output is:
Level 1: 00000000017, 00000000025, 00000000040, 00000000048, 00000000052, 00000000055
Level 2: 00000000018, 00000000019, 00000000011, 00000000029
Level 3: 00000000035, 00000000038, 00000000039
edit: Modified in the sense that you want the keys and not the node values.
I ran into the same problem recently and this article by Kevin van Zonneveld helped me out.
Basically you have to use a recursive function. Check out the article, it's what you need!