I'm New in php My Simply Question I have a String then i want get in number. for example a=1,b=2,c=3, etc... I trying so many times I have Getting only strlen string count or length. Please Check My Example
$a = "abc" ;
echo $a // output 123
An alternative solution using array_keys()
$charmap = array(
"a" => "1",
"b" => "2",
"c" => "3"
);
$string = "abc";
$string = str_replace(array_keys($charmap), $charmap, $string);
$string="abc";
$list=[
"a"=>1,
"b"=>2,
"c"=>3
];
$keys = array_keys($list);
$values = array_values($list);
$new_string = str_ireplace($keys, $values, $string);
Something like this?
$charmap = array(
"a" => "1",
"b" => "2",
"c" => "3"
);
$string = "abc";
foreach($charmap as $char => $number){
$string = str_replace($char, $number, $string);
}
echo($string);
Related
There seems to be a bug with array_search, how do I work around it?
$arr = [
"0" => "Zero",
"1" => "One",
"2" => "Two",
];
$val = array_search("Zero", $arr, true);
echo gettype($val); // returns integer instead of string
echo $val;
I want to get "0" instead of 0. How do I go about doing that?
You can typecast if needs a string result:
$arr = [
"0" => "Zero",
"1" => "One",
"2" => "Two",
];
$val = (string) array_search("Zero", $arr, true);// You can typecast to String here
echo gettype($val); // this will return string now
echo $val;
I am trying to separate them all according to white space.
$string = "1 2 3 10 12";
According to this string, I want to separate like that.
$a = "1";
$b = "2";
$c = "3";
$d = "10";
$e = "12";
After that, I would like to check those strings are included as the key of an array. Here is my array,
$data = [
"1" => "Book",
"2" => "Paper",
"3" => "Pencil",
"10" => "Eraser",
"11" => "Ruler",
];
So, How can I separate an array and store the parts in each variable?
And How to check those variable are included in array as a key? Sorry for my English :D. Thanks.
Use the explode function to seperate the values and then check if the array key exists using array key exists.
$stringArray = explode(" ",$string);
foreach($stringArray as $stringPeice){
if(array_key_exists($stringPeice, $data)){
//do something
}
}
You can use explode to create an array of keys and then use array_diff to get an array of keys that are not in the $data:
$string = "1 2 3 10 12";
$keys = explode(' ', $string);
$data = [
"1" => "Book",
"2" => "Paper",
"3" => "Pencil",
"10" => "Eraser",
"11" => "Ruler",
];
$diff = array_diff($keys, array_keys($data));
Here is the demo.
If you want to get the entries from $data whose key exists in $string, you can use array_intersect_key(), and array_fill_keys() to create an array containing keys from the result of exploding $string:
$o = array_intersect_key($data, array_fill_keys(explode(" ", $string), ""));
Here's a demo
i have a problem with preg_replace: Because of multilanguage i have created an array for every value in the date() function:
$a = array( "d" => "19", "l" => "Saturday", "F" => "October", "Y" => "", ...);
now i want to have an string like $s = "{l}, the {d}. of {F} {Y}";
My question is, how can i use preg_replace or sth to replace the expressions in brackets with the values of my array? My problem is that i don't know how to get $a["Y"] based on the char within the { } brackets.
check serialize or json_encode , then replace strings.
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.serialize.php
This is possible using preg_replace_callback:
$a = array( "d" => "19", "l" => "Saturday", "F" => "October" , "Y" => "");
$s = "{l}, the {d}. of {F} {Y}";
function callback($m) {
global $a;
return $a[$m[1]];
}
echo preg_replace_callback("/\{(.)\}/", "callback", $s);
You're basically doing string replacement; regular expressions are quite overkill here. I would create a function like so
function insertTokens($string, $values)
{
foreach ($values as $key => $value)
{
$string = str_replace('{'.$key.'}', $value);
}
return $string;
}
$newString = insertTokens($s, $a);
I use this code :
$new = array(
"123" => "a",
"456" => "b"
);
$old = array(
"123" => "a",
"456" => "b"
);
then the $new array become like this:
$new = array(
"456" => "b",
"123" => "c",
"789" => "e"
);
as you see the count of $new array increased and the order of elements changed and the value at key 123 also changed. I need to compare the $new array against the $old array and catch only the change made on the value at key 123 without caring about the order and the count of elements. I tried:
$result = array_diff( $new, $old );
print_r( $result );
output :
Array ( [123] => c [789] => e )
UPDATE. quite confusing. now I think we got it
$old = array(
"123" => "a",
"456" => "b"
);
$new = array(
"456" => "b",
"123" => "c", // catch this (element in old array that is changed)
"789" => "e"
);
$new2 = array();
foreach ($new as $key => $new_val)
{
if (isset($old[$key])) // belongs to old array?
{
if ($old[$key] != $new_val) // has changed?
$new2[$key] = $new[$key]; // catch it
}
}
// output $new2:
array (
123 => 'c',
)
You first of all want to have those elements of $new that are changed compared to $old (see array_diff_assoc):
$changed = array_diff_assoc($new, $old);
Of that result you want to have only those elements that have their key in $old (see array_intersect_key):
$result = array_intersect_key($changed, $old);
And that's it. You can wrap that into each other if it helps:
array_intersect_key(array_diff_assoc($new, $old), $old);
Result is:
array(1) {
[123] =>
string(1) "c"
}
Full example (Demo):
$old = array(
"123" => "a",
"456" => "b"
);
$new = array(
"456" => "b",
"123" => "c", // catch only the change made on the value at key 123
"789" => "e"
);
$changed = array_diff_assoc($new, $old);
$result = array_intersect_key($changed, $old);
var_dump($result);
Just a final note: There are many array functions in PHP. It's worth to go through the list and look what is fitting because most often you only need one or two of them to get things like these done.
You use this code for your requirements
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>
I have a string that looks like:
KEY1,"Value"KEY2,"Value"Key3,"Value"
This string will always vary in the number of keys/values i need an associative array:
array (
'KEY1' => 'Value',
'KEY2' => 'Value',
'KEY3' => 'Value'
);
of the data contained in the string, a regular expression would be best I suppose?
Assuming your values don't contain a " in them you can do:
$str = 'KEY1,"Value1"KEY2,"Value2"Key3,"Value3"';
$pieces = preg_split('/(?<=[^,]")/',$str,-1,PREG_SPLIT_NO_EMPTY);
$result = array();
foreach($pieces as $piece) {
list($k,$v) = explode(",",trim$piece);
$result[$k] = trim($v,'"');
}
See it in action!
php> $str = 'KEY1,"Value"KEY2,"Value"Key3,"Value"';
php> $hash = array();
php> preg_match_all("/(.*?),\"(.*?)\"/", $str, $m);
php> foreach($m[1] as $index => $key) {
... $hash[$key] = $m[2][$index];
... }
php> var_dump($hash);
array(3) {
["KEY1"]=>
string(5) "Value"
["KEY2"]=>
string(5) "Value"
["Key3"]=>
string(5) "Value"
}
If the key changes between values then you'll need preg_split(). If the key is always the same then explode() should be more than adequate.