$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");
Related
I have a string like this:
$string = '[miniTrack, boxTrack]'
I want to turn it into an array of variables like this:
$array = [$miniTrack, $boxTrack];
So that I can perform loops and iterations through the variable. And access the variables they are referencing.
I tried eval but I can't figure out how to convert it to a PHP Variable.
I KNOW THAT THIS IS NOT IDEAL, THE STRUCTURE OF THE DATABASE THIS IS PULLING FROM CAN'T BE ADJUSTED UNFORTUNATELY
Your question starts unusually, because you show an array containing a single string that is comma-separated, rather than an array of individual strings.
You could try something like the following:
$arr = [];
$string = ['miniTrack, boxtrack'];
//split the one string into an array, trimming spaces
$vars= array_map('trim', explode(',', $string[0]));
foreach($vars as $var) {
$arr[] = $var; //add the var to the array
}
print_r($arr);
Array
(
[0] => miniTrack
[1] => boxtrack
)
And if you need to create a variable for each item, you can create "variable variables":
foreach($vars as $var) {
$my_var = $$var; //variable variable
}
It should be as easy as the following:
preg_match_all('~(\w+)~','[miniTrack, boxTrack]', $matches);
foreach($matches[1] as $var)
{
print $$var;
}
You can convert your strings to array like this. It may be not ideal but you can use try this.
$string = '[miniTrack, boxTrack]';
$string = str_replace(array('[',']'),array('', '' ), $string);
$array = explode(',',$string);
Here you can iterate your array whatever you want.
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)
How does one correctly/properly convert an array like this?
For example, if I do, print_r($array);
It would print out a result like,
Array([0] => Array([0] => 5))
How did that array come to be?
I know how to convert a single array to string by using implode(). It however, doesn't work on an array inside an array.
I don't think using implode() twice will do the trick. Does anyone have any idea?
if you want to get the array as string, use print_r with the second parameter true
$string = print_r($array, true);
or serialize
$string = serialize($array);
or json_encode
$string = json_encode($array);
And if you want to use implode, use this with array_walk_recursive
function test_print($item, $key)
{
if (is_array($item))
{
echo implode(',', $item);
}
}
array_walk_recursive($array, 'test_print');
Why not just loop it using a foreach construct ?
Something like this..
<?php
$arr = array(0=>array(0=>5),1=>array(0=>6));
foreach($arr as $arr1)
{
$str.=implode(' ',$arr1).",";
}
echo rtrim($str,','); //"prints" 5,6
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);
I have searched the PHP.net site and originally thought of some use for the list() function but doesn't seem to accomplish the goal:
I have an unknown number of values stored in a single array
$array1 = array(1,2,3,4,5);
or
$array1 = array(1,2,3);
I want to be able to echo (or print_r) the values contained within an array to screen and separated only by commas and spacing.
For example:
the 'idea' is to have echo $array1 to display:
1,2,3
from the second example above.
http://us.php.net/manual/en/function.implode.php
echo implode(", ", $array);
You can simply use PHP's implode function for this purpose as follows:
$string = implode(',', array(1,2,3,4,5));
You can make use of list() function if you know there are only 3 values.
$array = array('Life', 'living', 'Health');
list($life, $living, $health) = $array;
echo $life;
echo $living;
echo $health;
Note - you can make use of implode function as well.