Shortest way to change an array - php

I have an array
$a = array('a', 'b', 'c');
What is the shortest and optimum way to change it to
$a = array('1a1', '1b1', '1c1');

$a = array("1{$a[0]}1", "1{$a[1]}1", "1{$a[2]}1");
With a dynamic number of values you must use a loop
foreach ($a as &$value) $value = "1{$value}1";
(I know: Omitting the braces {} is usually not "a good style", but in such simple cases there is nothing wrong with it. Of course you can add the braces again, if you don't feel comfortable with the compacted form).
or (with PHP5.3)
$a = array_map(function ($value) { return "1{$value}1"; }, $a);

function add1s($val) {
return '1' . $val . '1';
}
$a = array_map("add1s", $a);

most optimum is probably just a good ol' for loop
$cnt = count($a);
for($i = 0; $i < $cnt; $i++) {
$a[$i] = '1' . $a[$i] . '1';
}
or even lambda
$a = array_map(function($el) { return '1' . $el . '1'; }, $a);

$a = array('a', 'b', 'c');
$a = array_map(function ($x) { return ("1".$x."1"); }, $a);
print_r($a);

use a loop or array_map. Simple and clean and efficient .
Loop :
<?php
for($i = 0; $i < count($a); $i++) {
$a[$i] = '1'.$a[$i].'1';
}
var_dump($a);
?>
Array_map:
<?php
function sandwich($item)
{
return '1'.$item.'1';
};
$a = array('a', 'b', 'c');
$a = array_map("sandwich",$a); /* you can also use lambda functions for PHP >= 5.3.0
var_dump($a);
?>

foreach($a as $key => $val) {
$a[$key] = '1' . $val . '1';
}

Related

PHP How to add two list of decimal together

public function Encrypt($message)
{
$character = str_split($message);
$encrypted = '';
foreach ($character as $character)
{
$encrypted .= (ord($character). '.');
}
return $encrypted;
}
I use that code to generate ASCII numbers. Example of the result that I generated
$a = 1.2.4.3.4.3
$b = 1.4.3.2.4.3
Then I want both together (1+1,2+4,4+3,3+2,4+4,3+3) then the result is
$c = 2.6.7.5.8.6
Is it possible to do that ? Can anyone help me please.
It's definitely possible:
$a = '1.2.4.3.4.3';
$b = '1.4.3.2.4.3';
$result = join('.', array_map(
function($a, $b) { return $a + $b; },
explode('.', $a),
explode('.', $b)
));
var_dump($result);
Explanation:
split by .
summarize
join back
Ideone: http://ideone.com/uzBVed
Perhaps you could use a function like this?
function add_number_strings($a, $b) {
$a_arr = explode('.', $a);
$b_arr = explode('.', $b);
$c_arr = array();
for ($i=0; $i<count($a_arr); $i++) {
$c_arr[] = $a_arr[$i] + $b_arr[$i];
}
return implode('.', $c_arr);
}
// Testing
$a = '1.12.9.4.3.2.1';
$b = '2.3.2.4.3.2.1';
$c = add_number_strings($a, $b);
var_dump($c); // should be 3.15.11.8.6.4.2

What is the faster method than array_diff() on PHP? [duplicate]

How does array_diff() work? It obviously couldn't work as follows:
function array_diff($arraya, $arrayb)
{
$diffs = array();
foreach ($arraya as $keya => $valuea)
{
$equaltag = 0;
foreach ($arrayb as $valueb)
{
if ($valuea == $valueb)
{
$equaltag =1;
break;
}
}
if ($equaltag == o)
{
$diffs[$keya]=$valuea;
}
}
return $diffs;
} //couldn't be worse than this
Does anyone know a better solution?
EDIT #animuson:
function array_diff($arraya, $arrayb)
{
foreach ($arraya as $keya => $valuea)
{
if (in_array($valuea, $arrayb))
{
unset($arraya[$keya]);
}
}
return $arraya;
}
user187291's suggestion to do it in PHP via hash tables is simply great! In a rush of adrenaline taken from this phantastic idea, I even found a way to speed it up a little more (PHP 5.3.1):
function leo_array_diff($a, $b) {
$map = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
With the benchmark taken from user187291's posting:
LEO=0.0322 leo_array_diff()
ME =0.1308 my_array_diff()
YOU=4.5051 your_array_diff()
PHP=45.7114 array_diff()
The array_diff() performance lag is evident even at 100 entries per array.
Note: This solution implies that the elements in the first array are unique (or they will become unique). This is typical for a hash solution.
Note: The solution does not preserve indices. Assign the original index to $map and finally use array_flip() to preserve keys.
function array_diff_pk($a, $b) {
$map = array_flip($a);
foreach($b as $val) unset($map[$val]);
return array_flip($map);
}
PS: I found this while looking for some array_diff() paradoxon: array_diff() took three times longer for practically the same task if used twice in the script.
UPDATE
see below for faster/better code.
array_diff behaviour is much better in php 5.3.4, but still ~10 times slower than Leo's function.
also it's worth noting that these functions are not strictly equivalent to array_diff since they don't maintain array keys, i.e. my_array_diff(x,y) == array_values(array_diff(x,y)).
/UPDATE
A better solution is to use hash maps
function my_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
foreach($map as $val => $ok) if($ok) $out[] = $val;
return $out;
}
$a = array('A', 'B', 'C', 'D');
$b = array('X', 'C', 'A', 'Y');
print_r(my_array_diff($a, $b)); // B, D
benchmark
function your_array_diff($arraya, $arrayb)
{
foreach ($arraya as $keya => $valuea)
{
if (in_array($valuea, $arrayb))
{
unset($arraya[$keya]);
}
}
return $arraya;
}
$a = range(1, 10000);
$b = range(5000, 15000);
shuffle($a);
shuffle($b);
$ts = microtime(true);
my_array_diff($a, $b);
printf("ME =%.4f\n", microtime(true) - $ts);
$ts = microtime(true);
your_array_diff($a, $b);
printf("YOU=%.4f\n", microtime(true) - $ts);
result
ME =0.0137
YOU=3.6282
any questions? ;)
and, just for fun,
$ts = microtime(true);
array_diff($a, $b);
printf("PHP=%.4f\n", microtime(true) - $ts);
result
ME =0.0140
YOU=3.6706
PHP=19.5980
that's incredible!
The best solution to know how it works it to take a look at its source-code ;-)
(Well, that's one of the powers of open source -- and if you see some possible optimization, you can submit a patch ;-) )
For array_diff, it should be in ext/standard -- which means, for PHP 5.3, it should be there : branches/PHP_5_3/ext/standard
And, then, the array.c file looks like a plausible target ; the php_array_diff function, line 3381, seems to correspond to array_diff.
(Good luck going through the code : it's quite long...)
It seems you can speed it up a good deal more by using another array instead of unsetting. Though, this uses more memory, which might be an issue depeding on the use-case (I haven't tested actual differences in memory allocation).
<?php
function my_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
foreach($map as $val => $ok) if($ok) $out[] = $val;
return $out;
}
function leo_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
function flip_array_diff_key($b, $a) {
$at = array_flip($a);
$bt = array_flip($b);
$d = array_diff_key($bt, $at);
return array_keys($d);
}
function flip_isset_diff($b, $a) {
$at = array_flip($a);
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
function large_array_diff($b, $a) {
$at = array();
foreach ($a as $i)
$at[$i] = 1;
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
$functions = array("flip_array_diff_key", "flip_isset_diff", "large_array_diff", "leo_array_diff", "my_array_diff", "array_diff");
#$functions = array_reverse($functions);
$l = range(1, 1000000);
$l2 = range(1, 1000000, 2);
foreach ($functions as $function) {
$ts = microtime(true);
for ($i = 0; $i < 10; $i++) {
$f = $function($l, $l2);
}
$te = microtime(true);
$timing[$function] = $te - $ts;
}
asort($timing);
print_r($timing);
My timings are (PHP 5.3.27-1~dotdeb.0):
[flip_isset_diff] => 3.7415699958801
[flip_array_diff_key] => 4.2989008426666
[large_array_diff] => 4.7882599830627
[flip_flip_isset_diff] => 5.0816700458527
[leo_array_diff] => 11.086831092834
[my_array_diff] => 14.563184976578
[array_diff] => 99.379411935806
The three new functions were found at http://shiplu.mokadd.im/topics/performance-optimization/
As this has been brought up (see #BurninLeo's answer), what about something like this?
function binary_array_diff($a, $b) {
$result = $a;
asort($a);
asort($b);
list($bKey, $bVal) = each($b);
foreach ( $a as $aKey => $aVal ) {
while ( $aVal > $bVal ) {
list($bKey, $bVal) = each($b);
}
if ( $aVal === $bVal ) {
unset($result[$aKey]);
}
}
return $result;
}
After performing some tests, results seem to be acceptable:
$a = range(1, 10000);
$b = range(5000, 15000);
shuffle($a);
shuffle($b);
$ts = microtime(true);
for ( $n = 0; $n < 10; ++$n ) {
array_diff($a, $b);
}
printf("PHP => %.4f\n", microtime(true) - $ts);
$ts = microtime(true);
for ( $n = 0; $n < 10; ++$n ) {
binary_array_diff($a, $b);
}
printf("binary => %.4f\n", microtime(true) - $ts);
$binaryResult = binary_array_diff($a, $b);
$phpResult = array_diff($a, $b);
if ( $binaryResult == $phpResult && array_keys($binaryResult) == array_keys($phpResult) ) {
echo "returned arrays are the same\n";
}
Output:
PHP => 1.3018
binary => 1.3601
returned arrays are the same
Of course, PHP code cannot perform as good as C code, therefore there's no wonder that PHP code is a bit slower.
From PHP: "Returns an array containing all the entries from array1 that are not present in any of the other arrays."
So, you just check array1 against all arrayN and any values in array1 that don't appear in any of those arrays will be returned in a new array.
You don't necessarily even need to loop through all of array1's values. Just for all the additional arrays, loop through their values and check if each value is in_array($array1, $value).

How to filter an associative arrays using array of keys in PHP? [duplicate]

This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed last year.
I have an associative arrays and an array of keys.
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
How I build an associative array from all element of $A where the key is in $B?
For the example above, the answer should be
$C = array('a'=>'book', 'b'=>'pencil');
$keys = array_flip($B);
$C = array_intersect_key($A,$keys);
array_intersect_key($A,array_combine($B,$B))
or better: array_intersect_key($my_array, array_flip($allowed))
from the question: PHP: How to use array_filter() to filter array keys?
Here's a simple solution which checks that the key exists in $A before appending it to $C
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
var_dump($C);
// Prints:
array(2) {
["a"]=>
string(4) "book"
["b"]=>
string(6) "pencil"
}
$keys = array_keys($B);
$C = array();
foreach ($A as $key => $value)
{
if (in_array($key, $keys))
{
$C[$key] = $value;
}
}
To my immense surprise, the foreach loop method is faster.
The following quick benchmark script gives me results:
array_intersect_key: 0.76424908638
foreach loop: 0.6393928527832
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$start = microtime(true);
for ($i = 0 ; $i < 1000000; $i++) {
$c = array_intersect_key($A,array_flip($B));
}
$t1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
}
$t2 = microtime(true);
echo "array_intersect_key: " . ($t1 - $start), "\n";
echo "foreach loop: " . ($t2 - $t1), "\n";

PHP array question

This is more of a conceptual question concerning the built in functionality of PHP and arrays. I was wondering if there is any way to do the following:
You have an array $a and this array contains 5 elements (0-4) for the purpose of this example.
Is there any way to make a new array, which would contain the following:
$b[0] = $a[0];
$b[1] = $a[0] + $a[1];
$b[2] = $a[0] + $a[1] + $a[2];
$b[3] = $a[0] + $a[1] + $a[2] + $a[3];
$b[4] = $a[0] + $a[1] + $a[2] + $a[3] + $a[4];
etc..
I imagine an example of it's use would be bread crumbs on a website, where you could click on any directory of a given link like /dir1/dir2/dir3/dir4
Is there anything built into PHP that can handle building up an array in this fashion? Or examples of a function which handles this? Or even a better way to go about this.
Thanks!
EDIT: Here is the final solution via the help of you guys! This will build the link, and create the proper link for each directory/element.
//$a is our array
$max = count($a);
foreach (range(1,$max) as $count) {
$b[] = implode("/", array_slice($a, 0, $count));
}
foreach($b as $c) {
$x = explode('/' , $c);
$y = array_pop($x);
echo "<a href='$c'>".$y."</a>"."/";
}
If you just want the five combinations as in your example then:
foreach (range(1,5) as $count) {
$b[] = implode("/", array_slice($a, 0, $count));
}
You'd be best with a recursive function in that case.
$arr = array('dir1', 'dir2', 'dir3', 'dir4', 'dir5');
function breadcrumbs($a)
{
// Remove first value
$first = array_shift($a);
// Loop through other values
foreach ($a as $key => $value)
{
// Add first to remaining values
$a[$key] = $first . '/' . $value;
}
// Return array
return array($first) + breadcrumbs($a);
}
Untested, but should work. It will make each sequential value contain the values before it in the array.
$b = array();
for($i=0;$i<count($a);$i++) {
$b[] = array_sum(array_splice($a,0,$i));
}
I think, you want something like this:
for($i = 0; $i < count($a); $i++)
for($j = 0; $j < i + 1; $j++)
$b[i] += $a[j];

How does array_diff work?

How does array_diff() work? It obviously couldn't work as follows:
function array_diff($arraya, $arrayb)
{
$diffs = array();
foreach ($arraya as $keya => $valuea)
{
$equaltag = 0;
foreach ($arrayb as $valueb)
{
if ($valuea == $valueb)
{
$equaltag =1;
break;
}
}
if ($equaltag == o)
{
$diffs[$keya]=$valuea;
}
}
return $diffs;
} //couldn't be worse than this
Does anyone know a better solution?
EDIT #animuson:
function array_diff($arraya, $arrayb)
{
foreach ($arraya as $keya => $valuea)
{
if (in_array($valuea, $arrayb))
{
unset($arraya[$keya]);
}
}
return $arraya;
}
user187291's suggestion to do it in PHP via hash tables is simply great! In a rush of adrenaline taken from this phantastic idea, I even found a way to speed it up a little more (PHP 5.3.1):
function leo_array_diff($a, $b) {
$map = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
With the benchmark taken from user187291's posting:
LEO=0.0322 leo_array_diff()
ME =0.1308 my_array_diff()
YOU=4.5051 your_array_diff()
PHP=45.7114 array_diff()
The array_diff() performance lag is evident even at 100 entries per array.
Note: This solution implies that the elements in the first array are unique (or they will become unique). This is typical for a hash solution.
Note: The solution does not preserve indices. Assign the original index to $map and finally use array_flip() to preserve keys.
function array_diff_pk($a, $b) {
$map = array_flip($a);
foreach($b as $val) unset($map[$val]);
return array_flip($map);
}
PS: I found this while looking for some array_diff() paradoxon: array_diff() took three times longer for practically the same task if used twice in the script.
UPDATE
see below for faster/better code.
array_diff behaviour is much better in php 5.3.4, but still ~10 times slower than Leo's function.
also it's worth noting that these functions are not strictly equivalent to array_diff since they don't maintain array keys, i.e. my_array_diff(x,y) == array_values(array_diff(x,y)).
/UPDATE
A better solution is to use hash maps
function my_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
foreach($map as $val => $ok) if($ok) $out[] = $val;
return $out;
}
$a = array('A', 'B', 'C', 'D');
$b = array('X', 'C', 'A', 'Y');
print_r(my_array_diff($a, $b)); // B, D
benchmark
function your_array_diff($arraya, $arrayb)
{
foreach ($arraya as $keya => $valuea)
{
if (in_array($valuea, $arrayb))
{
unset($arraya[$keya]);
}
}
return $arraya;
}
$a = range(1, 10000);
$b = range(5000, 15000);
shuffle($a);
shuffle($b);
$ts = microtime(true);
my_array_diff($a, $b);
printf("ME =%.4f\n", microtime(true) - $ts);
$ts = microtime(true);
your_array_diff($a, $b);
printf("YOU=%.4f\n", microtime(true) - $ts);
result
ME =0.0137
YOU=3.6282
any questions? ;)
and, just for fun,
$ts = microtime(true);
array_diff($a, $b);
printf("PHP=%.4f\n", microtime(true) - $ts);
result
ME =0.0140
YOU=3.6706
PHP=19.5980
that's incredible!
The best solution to know how it works it to take a look at its source-code ;-)
(Well, that's one of the powers of open source -- and if you see some possible optimization, you can submit a patch ;-) )
For array_diff, it should be in ext/standard -- which means, for PHP 5.3, it should be there : branches/PHP_5_3/ext/standard
And, then, the array.c file looks like a plausible target ; the php_array_diff function, line 3381, seems to correspond to array_diff.
(Good luck going through the code : it's quite long...)
It seems you can speed it up a good deal more by using another array instead of unsetting. Though, this uses more memory, which might be an issue depeding on the use-case (I haven't tested actual differences in memory allocation).
<?php
function my_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
foreach($map as $val => $ok) if($ok) $out[] = $val;
return $out;
}
function leo_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
function flip_array_diff_key($b, $a) {
$at = array_flip($a);
$bt = array_flip($b);
$d = array_diff_key($bt, $at);
return array_keys($d);
}
function flip_isset_diff($b, $a) {
$at = array_flip($a);
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
function large_array_diff($b, $a) {
$at = array();
foreach ($a as $i)
$at[$i] = 1;
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
$functions = array("flip_array_diff_key", "flip_isset_diff", "large_array_diff", "leo_array_diff", "my_array_diff", "array_diff");
#$functions = array_reverse($functions);
$l = range(1, 1000000);
$l2 = range(1, 1000000, 2);
foreach ($functions as $function) {
$ts = microtime(true);
for ($i = 0; $i < 10; $i++) {
$f = $function($l, $l2);
}
$te = microtime(true);
$timing[$function] = $te - $ts;
}
asort($timing);
print_r($timing);
My timings are (PHP 5.3.27-1~dotdeb.0):
[flip_isset_diff] => 3.7415699958801
[flip_array_diff_key] => 4.2989008426666
[large_array_diff] => 4.7882599830627
[flip_flip_isset_diff] => 5.0816700458527
[leo_array_diff] => 11.086831092834
[my_array_diff] => 14.563184976578
[array_diff] => 99.379411935806
The three new functions were found at http://shiplu.mokadd.im/topics/performance-optimization/
As this has been brought up (see #BurninLeo's answer), what about something like this?
function binary_array_diff($a, $b) {
$result = $a;
asort($a);
asort($b);
list($bKey, $bVal) = each($b);
foreach ( $a as $aKey => $aVal ) {
while ( $aVal > $bVal ) {
list($bKey, $bVal) = each($b);
}
if ( $aVal === $bVal ) {
unset($result[$aKey]);
}
}
return $result;
}
After performing some tests, results seem to be acceptable:
$a = range(1, 10000);
$b = range(5000, 15000);
shuffle($a);
shuffle($b);
$ts = microtime(true);
for ( $n = 0; $n < 10; ++$n ) {
array_diff($a, $b);
}
printf("PHP => %.4f\n", microtime(true) - $ts);
$ts = microtime(true);
for ( $n = 0; $n < 10; ++$n ) {
binary_array_diff($a, $b);
}
printf("binary => %.4f\n", microtime(true) - $ts);
$binaryResult = binary_array_diff($a, $b);
$phpResult = array_diff($a, $b);
if ( $binaryResult == $phpResult && array_keys($binaryResult) == array_keys($phpResult) ) {
echo "returned arrays are the same\n";
}
Output:
PHP => 1.3018
binary => 1.3601
returned arrays are the same
Of course, PHP code cannot perform as good as C code, therefore there's no wonder that PHP code is a bit slower.
From PHP: "Returns an array containing all the entries from array1 that are not present in any of the other arrays."
So, you just check array1 against all arrayN and any values in array1 that don't appear in any of those arrays will be returned in a new array.
You don't necessarily even need to loop through all of array1's values. Just for all the additional arrays, loop through their values and check if each value is in_array($array1, $value).

Categories