How to create integer out of array? - php

Is it possible to convert array values into one single integer. For example, I have array with numbers
$array = array(7,4,7,2);
Is it possible to get integer value 7472 from this array?

Simple use implode as
$array = array(7,4,7,2);
echo (int)implode("",$array);// 7472

Use implode, which creates a string from an array. http://php.net/manual/en/function.implode.php
echo implode($array);

Use implode function as it create a string out of array and try this :
echo implode("",$array);

Use implode, along with (int) to convert the string result to an integer:
$a = [7,4,7,2];
$res = (int) implode('', $a);
P.S. Since PHP 5.4 you can also use the short array syntax, which replaces array() with [].

function digitsToInt($array) {
$nn = 0;
foreach ( $array as $digit) {
$nn = $nn * 10 + intval($digit);
}
return $nn;
}
var_dump( digitsToInt(array(7,4,7,2)) ); # int(7472)

Related

How to put PHP variable in array

$booked_seat=array(1,2);
if(in_array($seat,$booked_seat)){
$booked="red"; $book_seat="data-book='1'";
} else {
$booked=""; $book_seat="";
}
I want to put PHP variable in array
$booked_seat=array($id);
Is this possible in any way define variable in array?
You can do like this using explode() & array_shift() in php :
$str = '1,2,3';
$arr = [explode(",", $str)];
echo "<pre>";
print_r(array_shift($arr));
If you want a string instead of an array of numbers, you can use the join method http://php.net/manual/en/function.join.php.
$str = join(',', array(1, 2, 3));
If you are wanting to create an array out of a string like '1,2,3' you could use the explode method http://php.net/manual/en/function.explode.php.
$arr = explode(",", "1,2,3");

how to find the largest and smallest value in a string

Hi I have a string and I want to find about the smallest and the largest value from this string and store in a variable.
Array
(
[0] => 21,50
)
You should use max and min function.
$arr = ['21,50'];
echo max(explode(',',$arr[0])); // 50
echo min(explode(',',$arr[0])); // 21
Hope it will help you :)
If you have a string $a, then you can explode it, to get an array and then you can use the build in function min() and max()
$a = "21,50";
$arr = explode($a);
$maxValue = max($arr);
$minValue = min($arr);
You can also explode an array key to achieve this.
$myArr = array("21,50");
$tmpArr = explode($myArr[0]);
$maxVal = max($tmpArr);
$minVal = min($tmpArr);

Add a string to multidimensional array

I have a function that returns an multidimensional array() I want to concatenate a string to every value of that array. How can I do this
for example my string:
$this->$string = 'helloAddMeToArray';
and my array is:
array(array('url' => 'PleaseAddAStringToMeIAmLonely'));
So i need my array value to be like this: helloAddMeToArrayPleaseAddAStringToMeIAmLonely
I tried concatenating these with '.' but does not allow me
$oldArray = array(array('url' => 'PleaseAddAStringToMeIAmLonely'));
$newArray = array();
$this->string = 'helloAddMeToArray';
foreach($oldArray as $o) {
$newArray[] = array('url' => $this->string . $o['url']);
}
Try this:
First get string from your multidimentional Array, and type cast it.
$myString2 = (string)$myArray[0]->url;
Now use concatination: $string.$myString2;
Assuming your array could look like :
[
"key"=>[
"keyK"=>"val"
],
"key2"=>"val2"
]
and you want to concatenate a string to every value from that array you should use array_walk_recursive function .
This is a short snnipet doing this job :
$stringToConcatenate=$this->$string = 'helloAddMeToArray';
$callback($val,$key) use ($stringToConcatenate){
$val.=$val.$stringToConcatenate;
}
array_walk_recursive($youArray,$callback);
I hope it helps you .

removing quotes from array value

I have the following array value
0 => "2"
Now since im using an API it is very important that there are no qoutes (as the call will mis interperate it)
so i tried the following:
stripslashes($string);
However this did not help any ideas?
$yourArray = array_map('intval', $yourArray);
if you want to convert all values
or
intval($string) on a string
You can just iterate over the array and convert them to integer
$count = count($arr);
for($i=0;$i<$count;$i++) {
$arr[$i] = (int)$arr[$i];
}

array_map and htmlentities

I've been trying using array_map to convert characters to HTML entities with htmlentities() like this:
$lang = array_map('htmlentities', $lang);
My array looks like this:
$lang = array();
$lang['var_char1']['varchar2'] = 'Some Text';
But I keep getting this errors:
Warning: htmlentities() expects parameter 1 to be string, array given
in /home/user/public_html/foo/lang/en.inc.php on line 1335
Does anyone know what could be the problem? Thank you!
Use array_walk_recursive. array_map doesn't work with multidimensional arrays:
array_walk_recursive($lang, function (&$value) {
$value = htmlentities($value);
});
Because $lang is a two dimensional array, so it won't work
For two dimensional array you need to use for loop
foreach($$lang as &$l):
$l = array_map('htmlentities', $l);
}
$lang['var_char1']['varchar2'] defines a multidimensional array, so each element of $lang is also an array. array_map() iterates through $lang, passing an array to htmlentities() instead of a string.
array_map() doesn't work recursively. If you know your array is always two levels deep you could loop through it and use array_map on the sub-arrays.
if you like quotes
function stripslashes_array(&$arr) {
array_walk_recursive($arr, function (&$val) {
$val = htmlentities($val, ENT_QUOTES);
});
}
multiple array in post, get, dll
stripslashes_array($_POST);
stripslashes_array($_GET);
stripslashes_array($_REQUEST);
stripslashes_array($_COOKIE);
Each element in $lang is an array, so the function you pass to array_map should take an array as an argument. That is not the case for 'htmlentities' which takes a string.
You can:
$map_htmlentities = function(array) { return array_map('htmlentities', array); };
and then
$lang = array_map($map_htmlentities, $lang);
From PHP 7.4 on you can use lambdas:
$lang = array_map(fn($arr) => array_map('htmlentities', $arr), $lang);

Categories