Change specific array values to uppercase - php

I have an array with the following key/values:
FirstName, LastName, EmployeeNumber, Location
I need to convert the values contained in FirstName and LastName to all UPPERCASE.
I have tried using
array_map('strtoupper', $myARR);
But I get this error:
Warning: strtoupper() expects parameter 1 to be string, array given in
I think it is because employee number is numeric, so it errors out because a numeric is not a string? I do not know what else I can try, to attempt this. The reason being is that I have combined two arrays into one. One of the arrays coming from a DB is in all UPPER, while the other is not. This causes me to be unable to sort the array properly, as it does not see BROWN and Brown as the same value.
If I sort these five names:
BATMAN CHARLES BYRON Carl Bill
The result is this
BATMAN
BYRON
Bill
CHARLES
Carl
If I can get the ucfirst values to be all UPPER then the sort will work correctly (as one value from the table that has ucfirst only, is a 2 letter nickname (AJ) and it sorts correctly with the other data that is all UPPER).
Any ideas on how I can get the values to be UPPER for just those two portions of the array? Any help would be greatly appreciated.
EDIT
Here is some data pulled from the array:
Portion of Array with First Letter Capitalized
[59] => Array (
[0] => First
[1] => Last
[2] => 123456
[3] => Location )
Portion of Array with All Letter Capitalized
[1116] => Array (
[0] => FIRST
[1] => LAST
[2] => 123456
[3] => Location )
I cannot put the actual values here on the website, but essentially the values change for all people in the array. But at least this should show how each element in the array looks as far as uppercase values and what not.

foreach ($employees as $e) {
$e['firstName'] = strtoupper($e['firstName']);
... same for lastName
}

As an alternative to parsing all to upper case.. You could just use a case-insensitive sorting function: natcasesort
$sorted = natcasesort($myARR)

a simple way would be
$Capsfield = strtoupper($youarray['yourfeild']);
but that would not be recrusive, #Just Somebody gave a better thinking...

Related

PHP using key of one array to access same key position on another array

I've looked at a number of suggestions for this, and they seem to rely on array_combine() which unfortunately is not suitable, as they arrays need to stay separate for other functions.
The arrays are set out as per
Array ( [0] => 3 [1] => 1 [2] => 3 )
Array ( [0] => 194 [1] => 0 [2] => 452 )
When I read in the first array, I get the key as $key and everything works fine.
Whenever I try and access the second array the whole script just whites out the page.
I want the code to work simliar to this ...
$a2value = $a2[$key] => $value;
echo $a2value;
Then I can do SQL lookups using $a2value
Any help would be appreciated
Here Try this
let's suppose two arrays of same number of elements
$a1=[1,2,3];
$a2=[194,0,452];
//A Simple foreach
foreach($a1 as $key=>$a1value){
$a2value=$a2[$key];
//Perform Query here
}
Remember one thing number of elements of both array should always be equal or it will cause an error
If you aren't certain about the size of both arrays then replace the line with this code
$a2value=empty($a2[$key]) ? null : $a2[$key];
Hope this works !!!

Operation at strings and arrays

I wish to ask about operations over arrays in php.
I'm capturing two cells from one table in database, which are holding strings formatted like "1;3;6;" and whole second table - it has numbers in first column and names in second. Im dividing strings from first table into two arrays using explode() function, and second table into next two arrays with column per array. Therefore, when print_r() is used over those three arrays, I will see:
error_reporting(E_ALL); ini_set('display_errors', 1);
first table
"amount" string transformed into array called $ship_storage_parted_amount Array
(
[0] => 5
[1] => 10
[2] =>
)
"ware id" string transformed into array called $ship_cargo_bay_parted Array
(
[0] => 1
[1] => 2
[2] =>
)
second table transfomed into two arrays $ware_id and $ware_name
table contains 7 rows at all
Array
(
[0] => Array
(
[ware_id] => 1
[name] => Energized energy cell
)
[1] => Array
(
[ware_id] => 2
[name] => Depleted energy cell
)
[2] => Array
(
[ware_id] => 3
[name] => Vegetables
)
[3] => Array
(
[ware_id] => 4
[name] => Meat
)
)
I want to achieve next thing: showing at website appropriate equal names from second table according to their IDs with conjunction of what is stored in first table.
Therefore, it should look like:
Energized energy cell x5
Depleted energy cell x10
When I've tryied to show them like that:
echo $ware_name[$ship_cargo_bay-1]." x".$ship_storage_parted_amount[$ware_position];
it resulted in such output:
Energized energy cell x
where $ship_cargo_bay contains all captured string "ware id" and $ware_position was amount counter. Amount counter pointed at 3rd array element which is empty; but when I reorganized printing with for()
for($i=0;$i}lowerthan{$ware_counter;$i++)
{echo $ware_name[$ship_cargo_bay_parted[$i]]." x".$ship_storage_parted_amount[$i];}
I've got now
x5 x10 x x
And my poor knowledge ends here. As I know, first attempt of printing shall not work at all, but why I've got such output in second attempt is over my understanding.
Finally found reasons, where I made mistakes. After carefully checking names of each single variable, printing what exactly everything contains, I found that Ive somehow mispelled variable names.
for($i=1;$i<$ware_counter;$i++)
{
echo $ware_name[$ship_storage_parted[$i]]." x ".$ship_storage_parted_amount[$i]."<br/>";
}
This few lines gives exactly what Ive needed. Therefore Im putting it here - maybe someone will encounter similar problem.

2 or more arrays from a list

I am quite not able to get the logic for my requirement.
Lets consider I have an array
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 1
[4] => 3
[5] => 3
)
I would like to split the below array on the basis if the array element matches its previous element.
i.e in the above example value at [1] matches the value at [0]. Hence put it into the same array as [0]. Now check if the value at [2] matches the value at [1] if it matches put it into the same array, if not put it into a different array. The process continues.
Below is an example of the desired outpout.
Array
(
[0] => 1
[1] => 1
)
Array
(
[0] => 2
)
Array
(
[0] => 1
)
Array
(
[0] => 3
[1] => 3
)
Thanks for your help in advance.
Justin
you can obtain that result in a loop checking on previous element. the output can be an array of arrays! (or anything you would prefer.. do your thing here)
$array1 = array(1, 1, 2, 1, 3, 3);
$output_array=array();
$previous_value="";
$output_array_index=0;
foreach ($array1 as $value) {
if($value != $previous_value){
$output_array_index+=1;
}
$output_array[$output_array_index][]=$value;
$previous_value=$value;
}
print_r($output_array);
so, let me know if you need more pointers! array logic is fun, and php will let you do alot, out of the box. though this specific need is not covered, have a look when you have a minute # the manual, it'll save you time in the future, guarantee http://php.net/manual/en/ref.array.php
This question is a bit confusing but doesn't sound too difficult to implement if I'm understanding it correctly. All you need to do is have a temporary array (or array list) that checks user input. If that user input happens to be the same as the previous input (you can keep a counter variable and check to see if ArrayList.get(counter) == ArrayList.get(counter-1)). Keep adding things to this temporary arrayList and once you have a number that is different, just iterate through the arraylist and add it to a new array.
Another question you have to consider is how you are going to store all these arrays. For that you may want to create an ArrayList containing Arrays. That way after you find user input that is different from the previous input you can just use the toArray method provided with the ArrayList class and add it to the ArrayList containing all of your separate Arrays!
Hope this helps!

in_array is not finding values that are in the array

I'm getting strange results when trying to check a $_GET variable against a whitelist array. The variable is in the array but in_array is not finding it. I'll descibe what's happening as best I can.
The $_GET vars are set from option/select choices, three altogether. Only one of the options is giving me a problem. To run a SELECT query, there have to be at least two options chosen. Any combination of the other two options works as expected.
To get the option/select lists I have a SELECT query retrieving a record subset.
From that recordset I use a foreach loop to get the various values to populate the select tags. So the select values are populated by values from the db table. The whitelist arrays are copied from the db.
I check any $_GET vars against the whitelist arrays. The option in question can be a comma-delimited string (eg. 'Italian,Pizza' or 'American, Barbeque, Sandwiches').
To get the select value list for this option I use implode to create a comma-delimited list of each category, then use explode to create an array, then use array_unique to get an array with single instances of each category. When echoing this array everything is right (it populates the select choices correctly).
implode outputs:
Italian,Pizza,Italian,Pizza,Italian,Pizza,Italian,Italian,Sandwiches,Italian,Pizza,Italian
explode outputs:
Array
(
[0] => Italian
[1] => Pizza
[2] => Italian
[3] => Pizza
[4] => Italian
[5] => Pizza
[6] => Italian
[7] => Italian
[8] => Sandwiches
[9] => Italian
[10] => Pizza
[11] => Italian
)
array_unique outputs:
Array
(
[0] => Italian
[1] => Pizza
[8] => Sandwiches
)
So, the URL can be like:
../search.php?var_src=Sandwiches&city_src=Cityname
To sanitize the $_GET vars:
if((isset($_GET['var_src'])) && in_array($_GET['var_src'], $var_array)) {
$var_sort = $_GET['var_src'];
}
Again, $var_array is an array copied from the db table.
This is where the code stops because the $_GET var is not being found in the array.
If I change one of the db records to just one category value (eg. 'Sandwiches') and select that option in the list, then the results are as expected. However, if the record has more than one category value (each of which is in the whitelist array) like 'Pizza,Sandwiches', then neither of them work.
The kicker is that if I include the category value 'Italian', whether alone or with other values in the db record, and select for this value, then it works. I have no idea why.
So the code works if that particular value ('Italian') is selected whether or not others are included as a comma-delimited string for the record, and does not work with other values where there are more than one associated with the record.
I confirmed that the offending value is in the URL and so should work with the code above (in_array($_GET['var_src'], $var_array)).
Also, I don't know why changing the db record would have an effect because the sanitizing against the whitelist happens before selecting records based on the passed values. As far as I can tell, in_array is just not finding legitimate values in the array.
Hope I haven't made this confusing. Thanks for any help.
Not sure if this will help you, try to change it like in this example:
$opt = array( 'Italian,Pizza' , 'American, Barbeque, Sandwiches');
$opts = implode(',',$opt);
$opta = explode(',',$opts);
foreach ($opta as $key=>$value) {
$opta[$key]=trim($value);
}
$opta = array_flip($opta);
var_export($opta);
//now test it simply with isset:
$val = trim($_GET['var_src']);
echo isset($opta[$val])? 'yes':'no';

how php array_multisort work?

i have some problem to understand array_multisort
See how it sorts when two values are the same:
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
The output of the code above will be:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Fido [2] => Pluto )
let me know why Missy comes first, if you do by ascending it must be
Array ( [0] => Fido, [1] => Missy, [2] => Pluto )
for descending vise versa
also see this
With sorting parameters:
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
The output of the code above will be:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Pluto [2] => Fido )
but Array ( [0] => Missy [1] => Pluto [2] => Fido ) not at SORT_DESC is some type of mixed up.
can some one explain me how the array_multisort is working, so that i can understand how it's working.
Well, you're sorting the arrays in a similar way to programs like Excel. Each array corresponds to a column.
First, all arrays are sorted by the first array given. If there are identical values, those affected are sorted by the second array given. If there are again equal values, the third array is used, etc.
Or in other words: The arrays are sorted using all arrays, but beginning on the right (if you assume it really sorts by all columns once).
For your particular example (the second one):
At first you want to sort in ascending order, so Cat will be first. Therefore the last array element will be moved to the first position in both arrays. The other two elements, Dog are equal. This causes the function to look at the next array. It's told to sort this array in descending order, so Pluto comes first. In this case this leads to the result that the elements aren't moved at all (as their order is correct already).
The entries in the second array corresponding to the identical entries in the first array.
If you look at the documentation and the first example, you'll notice that this is the expected behavior.
With two arguments, both arrays: the first array is sorted; the second array will have its corresponding values re-arranged and sorted if the corresponding values in first column tie. As for your example, think of it as you're doing a SQL ORDER BY Animal, Name:
Cat comes first
The two Dogs have a tie so Fido comes first because Fido < Pluto

Categories