Having issues with an array being parsed..
Here is a dump from the $_GET var
Array
(
[perm0] => Array
(
[0] => View
[1] => Add
[2] => Edit
[3] => Delete
[4] => Export
)
[perm1] => Array
(
[0] => View
[1] => Add
[2] => Export
)
[add] =>
)
When I try and use a foreach to insert into the db its only inserting the first item.
I am need to add the number based on the perm[] value such as perm0, perm1 etc
Here is my code
$i = 0;
$id = 0;
foreach($_GET['perm'.$i] as $permission)
{
do {
$perms = implode(":",$_GET['perm'.$i]);
mysqli_query($dbc,"INSERT INTO `permissions` (`mid`,`uid`,`permissions`) VALUES ('$id','99','$perms')");
echo mysqli_error($dbc);
} while(strpos($permission, $i) !== false);
$id++;
$i++;
}
I would have thought this would be really simple so I dont know why I'm having issues with it
The problem is that you are always only looking at the first item, your statement...
foreach($_GET['perm'.$i] as $permission)
will only ever pick up the perm0 item, even though you increment $i in the loop.
Instead, this code loops over all $_GET values, checks if they are the permN values and then inserts the value. I've also converted to prepared statements to add some level of security...
$i = 0;
$insert = mysqli_prepare ($dbc,"INSERT INTO
`permissions` (`mid`,`uid`,`permissions`)
VALUES (?,'99',?)");
foreach ( $_GET as $key => $param ) {
if ( $key == "perm$i" ) {
$perms = implode(":",$param);
mysqli_stmt_bind_param ($insert, "is", $i, $perms);
mysqli_execute($insert);
$i++;
}
}
As it's difficult for me to test this, please let me know if you have issues.
Related
Fetching multi-row data from MySQL database and trying to convert the results into a single array. Since the data is coming from a custom function where modifying it will break many other things, I can't change the way the way it is fetched so must process after retrieval using PHP. This is a sample:
Array
(
[0] => Array
(
[FieldLabel] =>
[FieldName] => ID
[FieldValue] => $ID
[FieldType] => 0
)
[1] => Array
(
[FieldLabel] => Name
[FieldName] => Name
[FieldValue] => $FieldName
[FieldType] => 1
)
)
Looking for something like this with only all the values in a single array but with the variables populated:
Array('','ID',$ID,0,'Name','Name',$FieldName,1)
I found this little function in another post that seemed would at lease create the array it but unfortunately it does not and I don't know enough about array manipulation to be able to sort it out. Any ideas?
function array2Recursive($array) {
$lst = array();
foreach( array_keys($array) as $k ) :
$v = $array[$k];
if (is_scalar($v)) :
$lst[] = $v;
elseif (is_array($v)) :
$lst = array_merge($lst,array2Recursive($v));
endif;
endforeach;
return array_values(array_unique($lst));
}
On way to solve this could be to use a foreach to loop through the items and add them to an array:
$result = [];
foreach ($array as $a) {
foreach($a as $item) {
$result[] = $item;
}
}
print_r($result);
Demo
I have an array set up like:
Array (
[0] => Array ( [stage] => biometrics [applicant_id] => b79a4c6ea30611e3a3160675fe500303 )
[1] => Array ( [stage] => biometrics [applicant_id] => b79a4c6ea30611e3a3160675fe600303 )
[2] => Array ( [stage] => biometrics [applicant_id] => b79a4c6ea30611e3a3160675fe700303 )
[3] => Array ( [stage] => biometrics [applicant_id] => b79a4c6ea30611e3a3160675fe800303 )
[4] => Array ( [stage] => biometrics_queue [applicant_id] => b79a4c6ea30611e3a3160675fe900303 )
)
First, I want to check to see if there are any duplicate applicant_id's then I need to check the stages for the duplicates. If the applicant_id are the same, but stages are different, they are ok, If the applicant_id's are the same and stages are either the same (biometrics & biometrics) or if it is (biometrics and biometrics_queue) I need to delete that entry from the array.
Not sure how to do this.
.
So here is what I have so far. It works, but there are a lot of loops going on, don't wanna end up using too many resources or getting into an infinite loop...Does anyone see anything wrong with what I'm doing?
First, I used a function called convert stages, so that if there is anything that is a stage name and then _queue appended to the end, it changes it to just the stage name.
foreach ($timer_entry as $key => $value){
$timer_entry[$key]['stage'] = convert_stages($timer_entry[$key]['stage']);
}
Then I have a foreach inside of a for, checking for applicant_id's that might be the same:
for ($i = 0; $i < count($timer_entry); $i++) {
foreach ($timer_entry as $key => $value){
if ($key == $i) {
continue;
}
else {
if ($timer_entry[$key]['applicant_id'] == $timer_entry[$i]['applicant_id']) {
if ($timer_entry[$key]['stage'] == $timer_entry[$i]['stage']) {
unset($timer_entry[$key]);
}
}
}
}
}
If they are the same, I unset them.
I didn't test it, but I think this might do the trick:
$array = array_map('unserialize', array_unique(array_map('serialize', $array)));
How about:
foreach( $myArray as $index => $element )
if (isset($tmp[$element['applicant_id']][str_replace('_queue','',$element['stage'])])
unset($myArray[$index]);
else
$tmp[$element['applicant_id']][str_replace('_queue','',$element['stage'])] = true;
I am having a terrible time getting this to work I have been struggling with it for a couple hours now. Can someone please help me? I have included a fiddle.
I believe my problem is in this string:
$$salesAndOwner[$i]["$salesAndOwner[$i]".$l] = $salesAndOwner[$i.$l][$param] = $values[$l];
Basically I have the following multidimensional array:
[sales] => Array
(
[FirstName] => Array
(
[0] => salesFirst1
[1] => salesFirst2
)
[LastName] => Array
(
[0] => salesLast1
[1] => salesLast2
)
)
[decisionmaker] => Array
(
[FirstName] => Array
(
[0] => dmFirst1
[1] => dmFirst2
)
[LastName] => Array
(
[0] => dmLast1
[1] => dmLast2
)
)
)
I need this to be reorganized like I did with the following array:
Array
(
[additionallocations0] => Array
(
[Address] => Address1
[State] => State1
)
[additionallocations1] => Array
(
[Address] => Address2
[State] => State2
)
)
Here is the original:
Array
(
[additionallocations] => Array
(
[Address] => Array
(
[0] => Address1
[1] => Address2
)
[State] => Array
(
[0] => State1
[1] => State2
)
)
This is how I reorganize the above array:
if(isset($_POST['additionallocations'])) {
$qty = count($_POST['additionallocations']["Address"]);
for ($l=0; $l<$qty; $l++)
{
foreach($_POST['additionallocations'] as $param => $values)
{
$additional['additionallocations'.$l][$param] = $values[$l];
}
}
And this is what I am using for the sales and decisionmaker array. If you notice I have an array that contains sales and decisionmaker in it. I would like to be able to sort any future arrays by just adding its primary arrays name. I feel I am close to solving my problem but I can not get it to produce right.
$salesAndOwner = array(0 => "sales", 1 => "decisionmaker");
for($i = 0; $i < 2; $i++){
$qty = count($_POST[$salesAndOwner[$i]]["FirstName"]);
for ($l=0; $l<$qty; $l++)
{
foreach($_POST[$salesAndOwner[$i]] as $param => $values)
{
$$salesAndOwner[$i]["$salesAndOwner[$i]".$l] = $salesAndOwner[$i.$l][$param] = $values[$l];
}
}
}
In the above code I hard coded 'sales' into the variable I need it to make a variable name dynamically that contains the sales0 decisionmaker0 and sales1 decisionmaker1 arrays so $sales and $decisionmaker
I hope this makes sense please let me know if you need any more info
Let's break it down. Using friendly variable names and spacing will make your code a lot easier to read.
Remember. The syntax is for you to read and understand easily. (Not even just you, but maybe future developers after you!)
So you have an array of groups. Each group contains an array of attributes. Each attribute row contains a number of attribute values.
PHP's foreach is a fantastic way to iterate through this, because you will need to iterate through (and use) the index names of the arrays:
<?php
$new_array = array();
// For each group:
foreach($original_array as $group_name => $group) {
// $group_name = e.g 'sales'
// For each attribute in this group:
foreach($group as $attribute_name => $attributes) {
// $attribute_name = e.g. 'FirstName'
// For each attribute value in this attribute set.
foreach($attributes as $row_number => $attribute) {
// E.g. sales0
$row_key = $group_name . $row_number;
// if this is the first iteration, we need to declare the array.
if(!isset($new_array[$row_key])) {
$new_array[$row_key] = array();
}
// e.g. Array[sales0][FirstName]
$new_array[$row_key][$attribute_name] = $attribute;
}
}
}
?>
With this said, this sort of conversion may cause unexpected results without sufficient validation.
Make sure the input array is valid (e.g. each attribute group has the same number of rows per group) and you should be okay.
$salesAndOwner = array("sales", "decisionmaker");
$result = array();
foreach ($salesAndOwner as $key) {
$group = $_POST[$key];
$subkeys = array_keys($group);
$first_key = $subkeys[0];
foreach ($group[$first_key] as $i => $val) {
$prefix = $key . $i;
foreach ($subkeys as $subkey) {
if (!isset($result[$prefix])) {
$result[$prefix] = array();
}
$result[$prefix][$subkey] = $val;
}
}
}
DEMO
Try
$result =array();
foreach($arr as $key=>$val){
foreach($val as $key1=>$val1){
foreach($val1 as $key2=>$val2){
$result[$key.$key2][$key1] = $val2;
}
}
}
See demo here
I would like to replace the value of a multidimensional array if a corrosponding array contains a certain value.
Basically, I have two multidimensional arrays. One contains the actual data and the other contains a yes/no for whether the first array should be modified.
Is there any way to do this:
if optB[i][i] contains 'yes'
then opt[i][i] = '<strong>'.opt[i][i].'</strong>';
I'm lost as to whether this is even possible. Any help would be greatly appreciated -- thank you!
Thank you for the help so far. Here is the array:
[opt] => Array
(
[0] => Array
(
[0] => value1
[1] => value2
)
[1] => Array
(
[0] => value3
[1] => value4
)
)
[optB] => Array
(
[0] => Array
(
[0] => on
)
[1] => Array
(
[1] => on
)
)
Those are some interesting arrays because usually numeric arrays always have a 0. I imagine you may have some different key combination so I think this is the best "future-proof" method:
foreach ($optB as $i => $optB2) {
foreach ($optB2 as $j => $val) {
if ($val) {
$opt[$i][$j] = '<strong>' . $opt[$i][$j] . '</strong>';
}
}
}
It is possible. You can do this:
for ($i = 0; $i < count(opt); $i++) {
if ($optB[$i][$i] == "yes")
opt[$i][$i] = '<strong>'.opt[$i][$i].'</strong>';
}
It can be written like this:
if (strpos($optB[$i][$i], 'yes'))
$opt[$i][$i] = '<strong>'.$opt[$i][$i].'</strong>';
Something along these lines:
foreach ($opt as $i => &$arr) {
foreach ($arr as $j => &$val) {
if ($optB[$i][$j]) {
$val = "<strong>$val</strong>";
}
}
}
Modify as needed.
My question is maybe repetitive but I really find it hard. (I have read related topics)
This is the array :
Array
(
[0] => Array
(
[legend] => 38440
)
[1] => Array
(
[bestw] => 9765
)
[2] => Array
(
[fiuna] => 38779
)
[3] => Array
(
[adam] => 39011
)
[4] => Array
(
[adam] => 39011
)
[5] => Array
(
[adam] => 39011
)
)
I have tried many ways to handle this array and find out the most common value. The result I expect is "adam"
$countwin = array_count_values($winnernames);
$maxwin = max($countwin);
$mostwinner = array_keys($countswin, $maxwin);
EDIT : My array is like this array("A"=>"1" , "B"=>"2" , "A"=>"1") it should return A is the most common
How about iterating your array and counting the values you've got?
$occurences = array();
foreach ($data as $row) {
foreach ($row as $key => $score) {
if (empty($occurences[$key])) {
$occurences[$key] = 1;
} else {
$occurences[$key]++;
}
}
}
and then sorting that
arsort($occurences);
and grabbing the first element of the sorted array
$t = array_keys($occurences);
$winner = array_shift($occurences);
You may try this code. A brute force method indeed. But a quick search made me to find this an useful one:
function findDuplicates($data,$dupval) {
$nb= 0;
foreach($data as $key => $val)
if ($val==$dupval) $nb++;
return $nb;
}
EDIT : Sorry, I misinterpreted your question! But it might be the first hint of finding the one with maximum counter.