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];
}
Related
I'm trying to make an associative array from my string, but nothing works and I don't know where the problem is.
My string looks like:
$string = "somethink;452;otherthink;4554;somethinkelse;4514"
I would like to make an associative array, where "text" is the key, and the number is value.
Somethink => 452 otherthink => 4554 Somethinkelse => 4514
I tried to convert the string into an array and then to the associative array but it's not working. I decided to use:
$array=explode(";",$string);
Then tried to use a foreach loop but it's not working. Can somebody help?
Using regex and array_combine:
$string = "somethink;452;otherthink;4554;somethinkelse;4514";
preg_match_all("'([A-Za-z]+);(\d+)'", $string, $matches);
$assoc = array_combine($matches[1], $matches[2]);
print_r($assoc);
Using a traditional for loop:
$string = "somethink;452;otherthink;4554;somethinkelse;4514";
$arr = explode(";", $string);
for ($i = 0; $i < count($arr); $i += 2) {
$assoc[$arr[$i]] = $arr[$i+1];
}
print_r($assoc);
Result:
Array
(
[somethink] => 452
[otherthink] => 4554
[somethinkelse] => 4514
)
Note that there must be an even number of pairs; you can add a condition to test this and use a substitute value for any missing keys, or omit them.
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)
I'd like to convert the following string to array
{"0":"7","1":"12","2":"14","3":"13"}
I tried str_replace'ing but this isn't a proper sollution by far.
Further I checked if php's unserialize() could do it but that was no luck either.
What is the best way to convert
{"0":"7","1":"12","2":"14","3":"13"}
To
7,12,14,13
Edit:
The complete script should compare 2 of these strings to check if one of the numbers are the same.
So let's say String A is:
7,12,14,13
And String B is
4,9,11,12,15
It should set a var to 'true' since 12 is found in both strings.
String A is formatted as above which needs to be unserialized
Thank you in Advance!
Looks like JSON to me.
Decode json with json_decode
parse all elements to an integer with intval
run implode on the array to convert it back to a string.
A quick one-liner would look like +/- this
implode(',', array_map("intval", json_decode('{"0":"7","1":"12","2":"14","3":"13"}', true)));
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.implode.php
Second problem
To know if any value appears in both string $A and string $B, array_intersect() can be used.
$var = count(array_intersect(explode(',', $A), explode(',' $B))) > 0;
or if $A and `$B are arrays
$var = count(array_intersect($A, $B)) > 0;
http://php.net/manual/en/function.array-intersect.php
You can use explode() function convert string to array.
$str={"0":"7","1":"12","2":"14","3":"13"}
$arr1=explode(",",$str);
$result=array();
foreach($arr1 as $substr)
{
$syn=explode(":"$substr);
$result[]=$syn[1];
}
print_r($result);
Your string look like JSON string. You can parse JSON string using json_decode in PHP. Then used implode function for get comma separated values.
$str = '{"0":"7","1":"12","2":"14","3":"13"}';
$final_str = implode(",",json_decode($str,true));
echo $final_str;
You can use json_decode, explode, implode functions
$string = '{"0":"7","1":"12","2":"14","3":"13"}';
$result_array = explode(",", implode(',', array_map("intval", json_decode($string, true))));
print_r($result_array);
Output:
Array ( [0] => 7 [1] => 12 [2] => 14 [3] => 13 )
I'm fairly new at php, but this seems to be me overlooking something completely basic?
I have some values in a database column, that are comma separated like so:
1,2,3
When I try to get the sum of the values, I expect the echo of array_sum to be 6, but I only get returned the first value ie. "1"
echo $amount; //Gives 1,2,3 etc.
$amount_array = array($amount);
echo array_sum($amount_array); //Only prints "1"
print_r($amount); // shows 1,2,3
print_r($amount_array); // shows Array ( [0] => 1,2,3 )
It's a string not an array, you have to split it using explode function:
$exploded = explode ( "," , $amount_array);
var_dump($exploded);
To use the array_sum the string needs to be converted to an array
You need to use the explode function:
$amount_array = explode(',', $amount);
So you total code should be like this:
$amount_array = explode(',', $amount);
echo array_sum($amount_array);
array_sum() works by adding up the values in an array. You only have one key=>value pair in your array: key 0 with a value of 1,2,3.
If you have a comma-separated list, and want that to be an array, I would use the explode() function to turn the list into the proper key=>value pairs that array_sum() would expect.
Try
$amount_array = explode(',',$amount);
You can not initialize an array the way you intend. You are passing in a comma-separated string, which is just a single argument. PHP doesn't automagically convert that string into separate arguments for you.
In order to convert a comma-separated string into an array of individual values you can break up the string with a function like explode(), which takes a delimiter and a string as its arguments, and returns an array of the delimiter-separated values.
$amount_array = explode( ',', $amount ); // now $amount_array is the array you intended
I have an array, and when i echo it i get the following output;
Array[{"name":"Kat","age":"10"}]
Now, i need to add an additional filed into this; so finally it should appear as;
Array[{"message":"Success","name":"Kat","age":"10"}]
if $arr is my array, how am i going to append "message":"Success" ?
Sorry, i don't have any code to demonstrate my workings, i am stuck here. i would appreciate it if anyone can help me.
Your array content looks like JSON to me. But, if your array are actually just PHP arrays, then do:
$arr = array('name' => 'Kat', 'age' => '10');
$arr['message'] = 'Success';
If it is a JSON encoded array:
$arr = json_decode('{"name":"Kat","age":"10"}' , true)); //true decodes to an array and not a standard object
$arr['message'] = 'Success';
echo $arr;
//If you want it back in JSON
$json = json_encode($arr);
echo $json;
Like Waygood said if you want to add a value to the end of an array just use:
$array[] = $value; or $array['somekey'] = $somevalue;
However, if you need to add a value to the beginning of the array (like your example) you can use:
array_unshift($array, $value);
Alternatively, if you need to add a key and a value to the beginning you can simply make an array with the key => value pair and merge the two arrays like so:
$firstArray = array("message" => "Success");
$newArray = array_merge($firstArray, $secondArray);
For reference, here are the links to the php.net documentation:
array_unshift
array_merge
what about $arr["message"]="Success";?
To add a named field, you can just use this:
$array['message'] = 'Success';
To add a unnamed field, this is the way to do it:
$array[] = $value;
There are multiple ways of doing it.
PHP array functions
http://php.net/manual/en/function.array-push.php
http://php.net/manual/en/function.array-merge.php
The + operator
Or like others mentioned using $arg['var'] =
Just define "message":"Success" as another array and use push, merge or +
Also looks like you have it Json encoded. You will need to handle that as well.
The array functions only work with regular arrays, not encoded strings.
Try this:
$arr = array("name"=>"Kat","age"=>"10");
print_r($arr);
$arr = array_merge(array("message" => "Success"), $arr);
print_r($arr);