add extra values from other variable in php - php

I have two valriables in
$a="1:2:3";
$b="1:3:4:5";
Is there any simple method to add 4 and 5 in variable $a. Means i want the value of variable to be
$a="1:2:3:4:5"

A one line solution:
$result = implode(':', array_unique(array_merge(explode(':', $a), explode(':', $b))));
An even shorter one would be:
$result = implode(':', array_unique(array_merge(explode(':', "$a:$b"))));

$a2 = explode(":" , $a);
$b2 = explode(":" , $b);
foreach($b2 as $val)
{
if(in_array($val , $a2))
//do what you want
}

try this
$a="1:2:3";
$b="1:3:4:5";
$a = explode(':', $a);
$b = explode(':', $b);
$c = array_unique(array_merge($a,$b));
$a = implode(':', $c);
echo $a;

I notice that $a is ordered, so you can apply sort to the new array
$sort = SORT_NUMERIC;
$a = implode(':',array_uniqe(array_merge(explode(':',$a),explode(':',$b)),$sort));
See array_unique to other possible sorts.

Related

number_format multiple vars at once in php

I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;

Adding up the numbers in a string in php

Adding a string with a variable $a = 'ABC-01-222222'; with $b = 1; and it should give $a = 'ABC-01-222223'
You can use explode() to split the value of $a into three parts. Then add $b to the third item of the array, and then re-join the parts using implode():
$a = 'ABC-01-222222';
$b = 1;
$parts = explode('-', $a);
$parts[2] += $b;
$a = implode('-', $parts);
echo $a;

PHP merging arrays uniquely

So i'm working on a small php applications that combines four arrays.
Now there is a possibility that some of the possible arrays will be null.
I tried the following solution to merge the four arrays uniquely.
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a) && is_array($b) && is_array($c) && is_array($d))
{
$new_array = array_unique(array_merge($a,$b,$c,$d));
}else if(is_array($a) && is_array($b) && is_array($c))
{
$new_array = array_unique(array_merge($a,$b,$c));
}else if(is_array($a) && is_array($b))
{
$new_array = array_unique(array_merge($a,$b));
}else{
$new_array = $a;
}
print_r($new_array);
?>
I soon realized my code was highly dysfunctional in that it does not cater for all possible combinations of arrays while excluding the null variables.
How do I solve this. Ensuring that all the variables that are arrays are merged a nd those that are not are discarded.
Thanks
how about this? putting all the array's in an array, so you can loop through them all easily, and use a custom in_array() function to check if they are already existing?
The good thing about this way is that it doesn't limit the script to just four array's, as it will loop through all the array's you get together.
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$array_stack = array($a, $b, $c, $d);
$new_array = array();
foreach($array_stack as $stack){
if(!is_array($stack)) continue;
foreach($stack as $value){
if(!in_array($value, $new_array)) $new_array[] = $value;
}
}
print_r($new_array);
maybe something like this :
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a)) $new_array = $a;
if(is_array($b)) $new_array = array_unique(array_merge($new_array,$b));
if(is_array($c)) $new_array = array_unique(array_merge($new_array,$c));
if(is_array($d)) $new_array = array_unique(array_merge($new_array,$d));
?>
Old question, but going to give my input anyways. A more universal approach:
function multiple_array_merge() {
$args = func_get_args();
$array = [];
foreach ($args as $i) {
if (is_array($i)) $array = array_merge($array, $i);
}
return array_unique($array);
}
$a = [1, 2, 3, 4, 5];
$b = null;
$c = [5, 4, 3, 2, 1];
$d = [1, 2];
$merged = multiple_array_merge($a, $b, $c, $d);

Passing variable names into unset function in PHP

I have this piece of code:
<?php
$a = "dog";
$b = "cat";
$c = "pig";
$params = array($a, $b, $c);
some_function($params);
unset($a, $b, $c);
//echo "<br>".$a."<br>".$b."<br>".$c;
?>
Is here any elegant way, how to pass only variable names (not evaluated variables) to unset function? Something like:
unset($params); //but this doesn't work
Instead of
unset($a, $b, $c);
Thanks for any help.
The only way is to define variable names instead of variable values, and unset them in $GLOBALS:
<?php
$a = "dog";
$b = "cat";
$c = "pig";
$myvar = 'another var';
$params = array('a', 'b', 'c', 'myvar');
myunset($params);
function myunset($params){
foreach($params as $v){
unset($GLOBALS[$v]);
}
}
var_dump($myvar); //NULL
?>

How to make a single/double legthed string to minmum of five character legthed in php?

For my site, I need to do the following
$value = "1|22";
$explode = explode("|",$value);
$a = $explode[0];
$b = $explode[1];
This will return a and b as 1 and 22 as expected. But I need them to return $a as 00001 and $b as 00022. So if they contain less than 5 characters, I want to add 0 as prefix to it to make it a five-digit number. How can I do that?
$a = str_pad($explode[0], 5, '0');
$b = str_pad($explode[1], 5, '0');
With PHP5.3 you can prepend the 0 like this
$explode = array_map (function ($entry) {
return str_pad($entry, 5, '0');
}, $explode);
and assigning
list($a, $b) = $explode;
At all (still php5.3)
list($a, $b) = array_map (function ($entry) {
return str_pad($entry, 5, '0');
}, explode('|', $value));
Another method.
$value = "1|22";
$explode = explode("|",$value);
$a = sprintf( '%1$05d', $explode[0] );
$b = sprintf( '%1$05d', $explode[1] );

Categories