What would be the most simple way to convert an Array Integer to separate numbers?
Example:
array(2,4,6)
should result in:
num1=2,num2=4,num3=6
You have tried "list"?
list($num1, $num2, $num3) = $myArray;
See http://php.net/list for more details.
If you have associative array than You can use php built in function extract().
Example:
$abc = array('var1'=>2, 'var2'=>4, 'var3'=>6);
extract($abc);
echo $var1.$var2.$var3; //Outputs 246
Related
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 have multiple strings that looks like the following: 11-16, 16-12, 14-16
I will have multiple of these, and I need to store them in JSON. I need to store it in the following format:
[
{
"score_1": 11,
"score_2": 16
},
{
"score_1": 16,
"score_2": 12
},
{
"score_1": 14,
"score_2": 16
}
]
with score_1 being the first number, and score_2 being the second number. How will I be able to do this in PHP?
Thanks
First create an array. Next create an object, then explode the string on the hyphen and store the intval'd first and second number in the object. Push that object into your array. Repeat for as many strings as you have. Finally, use json_encode to get a JSON-encoded string.
$arr = [];
function addString($str, &$arr) {
$str = explode("-", $str);
$obj = new stdClass();
$obj->score_1 = intval($str[0]);
$obj->score_2 = intval($str[1]);
$arr[] = $obj;
}
addString("11-16", $arr);
addString("16-12", $arr);
echo json_encode($arr);
Output:
[{"score_1":11,"score_2":16},{"score_1":16,"score_2":12}]
Edit: Updated the above code to use intval as the OP has integers in his object in the expected output.
Two steps:
Convert the strings to the according data structure (e.g. array of stdClass objects).
Convert that data structure to JSON.
Note: Since you already know the output you want, you could parse that output to get an idea how its representation in PHP would look like, as a suggestion for the results of the first step.
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'm not quite sure what I'm doing wrong, but it appears I'm having myself a brain fry trying to comprehend it..
$cards = array(range(1,52));
shuffle($cards);
echo $cards[0];
I get a array to string conversion error.
I've also tried a custom function to echo dependent on the input value and that isn't working either.
You're creating an array of arrays. range() already returns an array:
$cards = range(1,52);
shuffle($cards);
echo $cards[0];
The range function returns an array (http://php.net/manual/en/function.range.php), so the statement $cards = array(range(1,52)); has set $cards to be an array with exactly one element - an array containing the range of values from 1 to 52.
Thus when you try to echo $cards[0], you are trying to echo an element which is an array, which produces the error.
What you want to do is this:
$cards = range(1, 52);
shuffle($cards);
echo $cards[0];
Get rid of the array, range returns an array:
$cards = range(1,52);
Range already creates an array. You have create an array with one element that contains the array.
$cards =range(1,52);
shuffle($cards);
echo $cards[0];
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.