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
Related
$a = ['Ava', 'Emma', 'Olivia']; $b = ['Olivia', 'Sophia', 'Emma'];
I want the output to be ['Emma', 'Olivia', 'Ava', 'Sophia'] in any particular order without using array functions.
This is what i tried
<?php
//function unique_names($a,$b){
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$z= $a;
$c = count($b);
$d = count($a);
//loop for b
$e = 0;
for($i=0;$i<$c;$i++){ //b
for($j=0;$j<$d;$j++){
if($b[$i] != $a[$j]){
$z[$d+1] = $b[$i];
break;
}else{
//$z[$e] = $a[$j];
}
}
}
echo"<pre>ans";print_r($z);
die;
//return $z;
//}
//echo"<pre>ans"; print_r(unique_names($a,$b));
?>
Also i made it work using in_array but was later told that even that function is not allowed.
<?php
function unique_names($a,$b){
$z= $a;
$c = count($b);
$d = count($a);
for($i=0;$i<$c;$i++){
if(! in_array($b[$i], $a)){
$z[$d+1] = $b[$i];
}
}
return $z;
}
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
print_r(unique_names($a,$b));
?>
You can use next code:
<?php
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$values = [];
// Add values from first array
foreach($a as $v) {
$values[$v] = true;
}
// Add values from second array
// all exists names will be overwrited
// new values will be addded
foreach($b as $v) {
$values[$v] = true;
}
// Transform keys to plain result
foreach($values as $key=>$val) {
$result[] = $key;
}
var_dump($result);
Execute PHP online
$string = 'aaaaa,val1,1111; ddddd,val2,2222; gggg,val3,3333;';
$string = rtrim($string, ";");
$one = explode(';', $string);
$array = array();
$i = 0;
foreach($one as $o)
{
$two = explode(',', $o);
$name = $two[0];
$value = $two[1];
$price = $two[2];
$array[$i]['name'] = $name;
$array[$i]['value'] = $value;
$array[$i]['price'] = $price;
$i++;
}
echo '<pre>';
print_r($array);
usort($array, 'sort_by_order');
function sort_by_order ($a, $b)
{
return $a['price'] - $b['price'];
}
print_r($array);
If you copy my codes above you can see the display right away.
I am trying to make my usort() work. Sort based on price. I follow a tutorial on usort() I do not understand how it works. How does the $a and $b comes into play?
Thanks guys but I had made it work in descending order
return $b['price'] - $a['price'];
For example, I have a function like this:
function loopValues()
{
$a = array('a','b','c');
foreach($a as $b)
{
$c = $b.'e';
echo $c;
}
}
How could I return its value aebece in an array like ('ae','be','ce')?
$a = array('a','b','c');
$b = array_map(function($ele) {
return $ele .= 'e';
}, $a);
See it in action
Try
function loopValues()
{
$a = array('a','b','c');
$result = array();
foreach($a as $b){
$result[] = $b.'e';
}
return $result;
}
$r = loopValues();
print_r($r);
See demo here
Simple, try this:
function loopValues()
{
$a = array('a','b','c');
$r = array();
foreach($a as $b)
{
$c = $b.'e';
$r[] = $c;
}
return $r;
}
function loopValues(){
$a = array('a','b','c');
for($i=0;$i<count($a);$i++){
$a[$i] .= 'e';
}
return $a;
}
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';
}
Instead of just 1, how can I pick the 4 highest values from an array using max()?
You could use an SplMaxHeap
function maxN(array $numbers, $n)
{
$maxHeap = new SplMaxHeap;
foreach($numbers as $number) {
$maxHeap->insert($number);
}
return iterator_to_array(
new LimitIterator($maxHeap, 0, $n)
);
}
Usage (demo):
print_r( maxN( array(7,54,2,4,26,7,82,4,34), 4 ) );
You could try this:
$a = array(3,5,6,1,23,6,78,99);
asort($a);
var_dump(array_slice($a, -4));
HTH.
This will do it in Θ(n) time:
$a = $b = $c = $d = null;
foreach($array as $v) {
if(!isset($a) || $v > $a) {
$d = $c;
$c = $b;
$b = $a;
$a = $v;
}elseif(!isset($b) || $v > $b) {
$d = $c;
$c = $b;
$b = $v;
}elseif(!isset($c) || $v > $c) {
$d = $c;
$c = $v;
}elseif(!isset($d) || $v > $d) {
$d = $v;
}
}
$result = array($a, $b, $c, $d);
function maxs($ar, $count=4)
{
$res = array();
foreach ($ar as $v)
{
for ($i = 0;$i < $count;$i++)
{
if ($i >= count($res) || $v > $res[$i])
{
do
{
$tmp = $res[$i];
$res[$i] = $v;
$v = $tmp;
$i++;
}
while ($i < $count);
break;
}
}
}
return $res;
}
A simple method using php predefined functions.
<?php
$arr = array(6, 8, 3, 2, 7, 9);
rsort($arr);
$first = array_shift($arr);
$second = array_shift($arr);
$third = array_shift($arr);
echo $first; // print 9
echo $second; // print 8
echo $third; // print 7
?>
While storing itself you can maintain another array as soon as the new item is inserted check with the max value in the inner array if the item being inserted is greater insert this item. During the item pop do viceversa. From the inner maintained array you can get as many max numbers as possible.