PHP - How to switch / reverse values in one column of 2D array - php

I have an exploded array and I am trying to reverse the first name and last name in a specific column.
Here is the array:
array(729) {
["ID;Position;Name;ER;FF;GA"]=>
array(24) {
[0]=>
string(3) "ID"
[1]=>
string(3) "Position"
[2]=>
string(4) "Name"
[3]=>
string(4) "ER"
[4]=>
string(6) "FF"
[5]=>
string(13) "GA"
}
["7702;Manager;Johnson, Bill;44.5;6T;406"]=>
array(24) {
[0]=>
string(4) "7702"
[1]=>
string(1) "Manager"
[2]=>
string(11) "Johnson, Bill"
[3]=>
string(3) "44.5"
[4]=>
string(4) "6T"
[5]=>
string(1) "406"
}
As you can see, I am need to flip the first name and last name in every 3rd element (index[2]). I was thinking to explode every 3rd element via ',' delimiter (since it is always fname, lname) and then use array_reverse to reverse them... and then reconstruct.
$dump_array = explode(PHP_EOL, $dump);
foreach($dump_array as $line){
$temp[$line]=explode(';', $line);
}
foreach($temp as $ele){
var_dump($temp[$ele]);
$temp[$ele]=array_reverse($temp[$ele[2]]); #I need to do another explode (',') somewhere?
}

You want to do implode instead of explode() in the last line or a sample demo code below :
foreach($dump as $line) {
$temp_str = $line[2];
$temp_str = explode(",", $temp_str);
$line[2] = $temp_str[1] . ", " . $temp_str[0];
}
How does it work: For every array, it picks that array out as $line and then explode the string stored in $line[2] and reverse their order and replace $line[2] with the new reversed value joined with a comma in middle of them

Here is the another way:
$temp = array("asjd", "first , last", "asdjlakd");
foreach($temp as $ele){
if (strpos($ele, ",")!== false){
$data = explode(",", $ele);#I need to do another explode (',') somewhere?
$reverse_data=array_reverse($data);
print_r($reverse_data);
}
}

Related

Difference between two arrays containing numbers as string

I´ve got a question about compairing two (text)arrays. I obtained two arrays from a loop containing numbers as shown below:
array(96) { [1]=> string(2) "20" [2]=> string(2) "18" [3]=> string(2)...
array(96) { [1]=> string(3) "135" [2]=> string(3) "103" [3]=> string(2) "88"
What I want is a new array which contains the difference of the values (1-2). This means that the 2 arrays above will results in the following new (text)array
array(96) { [1]=> string(3) "-115" [2]=> string(3) "-85" [3]=> string(2)
Can someone help me?
You could do this with one foreach-loop. And then substract them from each other. You just need the same count of elements in $array and in $array2.
$new = array();
foreach($array as $key => $val) {
$new[] = (string) ($val - $array2[$key]);
}
If it doesn't matter if the values are integers, you can remove the type casting (string).
this is a function,
function foo($array1, $array2){
$resultArray = array();
for($i=0; i<count($array1); $i++){
$resultArray[] = (string)((int)$array1[$i] - (int)$array2[$i]);
}
return $resultArray;
}
Hope this will help :)
<?php
$a=array(20,18);
$b=array(135,103);
function fr($d,$d1){
global $c;
$c[]=$d-$d1;
}
array_map('fr',$a,$b);
var_dump($c);

php code to print multi dimensional array list values one by one

I am reading excel data and storing it in php multi dimensional array. I would like to print multi dimensional array list values one by one as given in expected out put...
Php Code
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$i=0;
foreach($sheetData as $rec)
{
for($j=0;$j<4;$j=$j+1)
{
echo $rec[$i][$j];
}
$i=$i+1;
}
Actual Out put:
nothing displayed
var_dump($sheetData) shows as follows
array(4) { [1]=> array(4) { ["A"]=> string(10) "First Name" ["B"]=>
string(9) "Last Name" ["C"]=> string(11) "Nationality" ["D"]=>
string(6) "Gender" } [2]=> array(4) { ["A"]=> string(5) "AGNI" ["B"]=>
string(6) "namera" ["C"]=> string(5) "india" ["D"]=> string(1) "M" }
[3]=> array(4) { ["A"]=> string(4) "QUEEN" ["B"]=> string(5) "fefar"
["C"]=> string(5) "india" ["D"]=> string(1) "M" } [4]=> array(4) {
["A"]=> string(7) "MASTER" ["B"]=> string(5) "patel" ["C"]=> string(5)
"India" ["D"]=> string(1) "M" } }
Expected output:
----------------
First Name
Last Name
Nationality
Gender
-----------------
AGNI
NAMERA
INDIA
M
------------------
...and so on...
You have two issues here... the first is that you are trying to access indexes on $rec as if it is $sheetData.
The line foreach($sheetData as $rec) means "loop through the array, and create a variable $rec each time which references the the current item in array $sheetData. So when you try to call index 0, 1, 2, etc. on $rec, they don't exist, which leads me to the second point...
In your for loop, you are trying to access items via their index as a numeric, when they are indexed by a string (A, B, C, D, etc.) For example...
<?php
$numeric = array('foo', 'bar');
$strings = array('foo' => 'bar');
In the first example, $numeric[0] will return foo, while $numeric[1] will return bar. In the second example, there is only one item in the array, with index foo. It would be accessed by $strings['foo']. Trying to access $strings[0] would return a notice because the index does not exist.
You should be doing this like so:
<?php
print "----------" . PHP_EOL;
foreach($sheetData as $rec)
{
foreach($rec as $part)
{
print $part . PHP_EOL;
}
print "----------" . PHP_EOL;
}
You should probably read up on arrays. Also, if you got no output when running your script, you should probably set your error reporting level to E_ALL in your PHP configuration during development.
Try:
echo "------------"."<br>";
foreach($sheetData as $ind)
{
foreach($ind as $index=>$val){
echo $val."<br>";
}
echo "------------"."<br>";
}

Creating an associative array with strings

I read some words from a text file, storing each word as an array element using the file() function. Now I need to sort each word and create an associative array that stores the sorted strings as keys and the original strings as values like so :
$hash_table = array( 'sorted_string' => 'original string' );
I loop through every word read from the file and sort it in ascending order but when it comes to pushing it to the associative array, I am completely lost. Can anyone show me how to create the associative array ?
$a = array('green', 'yellow', 'red');//actual
$b = array('green', 'yellow', 'red');
sort($b); //sorted
$c = array_combine($b, $a);
If I understand your question right, consider this:
$sorted; //sorted array
$original; //original array
foreach($sorted as $key){
$index = 0;
$new_array[$key] = $original[$index++];
}
Here is what you want:
<?php
//create an array with words, similar to what you get with file()
$str = "here is a list of random words that will be sorted";
$array = explode(" ", $str);
//a place to store the result
$result = array();
//check each value
foreach($array as $word) {
//str_split will create an array from a string
$letters = str_split(trim($word));
//sort the letters
sort($letters);
//implode the letters again to a single word
$sorted = implode($letters);
//add to result
$result[$sorted] = $word;
}
//dump
var_dump($result);
//sort on the key
ksort($result);
//dump
var_dump($result);
?>
This will output
//unsorted
array(11) {
["eehr"]=>
string(4) "here"
["is"]=>
string(2) "is"
["a"]=>
string(1) "a"
["ilst"]=>
string(4) "list"
["fo"]=>
string(2) "of"
["admnor"]=>
string(6) "random"
["dorsw"]=>
string(5) "words"
["ahtt"]=>
string(4) "that"
["illw"]=>
string(4) "will"
["be"]=>
string(2) "be"
["deorst"]=>
string(6) "sorted"
}
//sorted on key
array(11) {
["a"]=>
string(1) "a"
["admnor"]=>
string(6) "random"
["ahtt"]=>
string(4) "that"
["be"]=>
string(2) "be"
["deorst"]=>
string(6) "sorted"
["dorsw"]=>
string(5) "words"
["eehr"]=>
string(4) "here"
["fo"]=>
string(2) "of"
["illw"]=>
string(4) "will"
["ilst"]=>
string(4) "list"
["is"]=>
string(2) "is"
}

Regex to match certain pattern of number combinations

I'm looking making a regex in php that gets data from the following format:
"1,2,3;7,1,3;1" returns an $matches array with "(1,2,3,7,1,3,1)"
"1" returns an $matches with "(1)"
"1;1;3;5;7;10;999" returns an $matches array with "(1,1,3,5,7,10,999)"
"1,1,1;2,5;3,4" doesn't pass since numbers are repeating within semicolon boundaries
"2,3,4;5,;" doesn't pass since it doesn't satisfy the format.
(Quotes in the examples are there to make them easier to read; they should not appear in the real results.)
The format is digit numbers separated by either commas or semicolons and within semicolons they don't repeat each other. It should not accept any other format.
I tried /(^(\d{1,3})$)|(([0-9]+)([,|;]{1}[0-9]+)+)/ but it didn't work.
I also tried /[0-9]+([,|;]{1}[0-9]+)+/ but it didn't worked either. When I got the $matches array it didn't have the values I needed as depicted above.
I'm doing this in PHP 5.2.
Thanks.
This particular problem has too much logic for regular expressions to be practical; this is how you could solve it with regular code:
// reduction function - keeps merging comma separated arguments
// until there's a duplicate or invalid item
function join_unique(&$result, $item)
{
if ($result === false) {
return false;
}
$items = explode(',', $item);
$numbers = array_filter($items, 'is_numeric');
if (count($items) != count($numbers)) {
return false;
}
$unique = array_unique($numbers);
if (count($unique) != count($numbers)) {
return false;
}
return array_merge($result, $numbers);
}
// main function - parse a string of comma / semi-colon separated values
function parse_nrs($str)
{
return array_reduce(explode(';', $str), 'join_unique', array());
}
var_dump(parse_nrs('1,2,3;7,1,3;1'));
var_dump(parse_nrs('1'));
var_dump(parse_nrs('1;1;3;5;7;10;999'));
var_dump(parse_nrs('1,1,1;2,5;3,4'));
var_dump(parse_nrs('2,3,4;5,;'));
Output:
array(7) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "7"
[4]=>
string(1) "1"
[5]=>
string(1) "3"
[6]=>
string(1) "1"
}
array(1) {
[0]=>
string(1) "1"
}
array(7) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "3"
[3]=>
string(1) "5"
[4]=>
string(1) "7"
[5]=>
string(2) "10"
[6]=>
string(3) "999"
}
bool(false)
bool(false)
See also: array_reduce() array_unique()
It is impossible to do this in a single step. First you will need to check for the requirement of numbers repeating within a semicolon boundary, and then if it passes that checks split the string.
For example:
if (!preg_match('/\b(\d+),[^;]*\b\1\b/', $string)) {
$matches = preg_split('/[,;]/', $string);
} else {
$matches = NULL;
}
Ideone: http://ideone.com/Y8xf1N

PHP regex. How to get an array of matches?

I have a string in a format like this:
5;1-x;1-2;(1-x;)+
I used 1-x as a notation for all integers from 1 to infinity and (1-x;)+ to mark that the last integer may be repeated any number of times.
Some example strings:
5;1;1;1
5;7;2;7;5;1;9
How can I match these strings with regex and get all the (1-x;)+ matches?
I have tried the following:
preg_match_all('%5;([1-9]{1}[0-9]*);([1-2]);([1-9]{1}[0-9]*;?)+%',
$str, $matches);
And the result for string "5;1;1;1" is:
array(4) {
[0]=>
array(1) {
[0]=>
string(7) "5;1;1;1"
}
[1]=>
array(1) {
[0]=>
string(1) "1"
}
[2]=>
array(1) {
[0]=>
string(1) "1"
}
[3]=>
array(1) {
[0]=>
string(1) "1"
}
}
For the string "5;7;2;7;5;1;9" it is:
array(4) {
[0]=>
array(1) {
[0]=>
string(13) "5;7;2;7;5;1;9"
}
[1]=>
array(1) {
[0]=>
string(1) "7"
}
[2]=>
array(1) {
[0]=>
string(1) "2"
}
[3]=>
array(1) {
[0]=>
string(1) "9"
}
}
As you can see, only the last integer from (1-x;)+ is in the matches array, but I want the matches array to contain values 7, 5, 1, and 9, not just the last one. Is this even possible using regex or do I need to use another approach to validate and get values from these strings?
One way
//$str = "5;1;1;1";
$str = "5;7;2;7;5;1;9";
$pattern = '%^5;([1-9]\d*;)[12];((?:[1-9]\d*;?)+)$%';
$str = preg_replace( $pattern, '$1$2', $str, -1, $count );
if ( $count ) {
print_r( explode( ';', $str ) );
} else {
echo 'Invalid string';
}
The -1 means no limit to the number of replacements.
$count is the number of replacements made. It will be 1 if the string is valid, or 0 otherwise.
The above assumes a string is still valid if it ends in a ;.
I honestly would just explode the string and remove the unwanted first and third elements that like this:
$array = explode(';', $string);
$diff_array = array(0 => 'not_used', 2 => 'not_used');
$final_array = array_key_diff($array, $diff_array);
This gives you an array of all the values except the first and third elements which are the ones which you seem to not be interested in.
If you then need to verify that the remaining elements are indeed integers with value >= 1 you could run an array_filter on it like this:
$filtered_array = array_filter($final_array, function($value) {
if ((string)(int)$value == $value && (int)$value >= 1) return true;
return false;
}

Categories