$var = {"4":true,"6":true,"8":true}
In The above string I want to get numbers into array.
Need: $var2 = [[0]=>4, [1]=>6, [2]=>8];
All response will be appreciated.
You should use json_decode and array_keys to accomplish it:
array_keys(json_decode($var, true));
First you decode the string using json_decode, the second argument means that the function should return an associative array and non an array of objects. This willl help up get the array keys.
$decoded = json_decode($var, true);
You get the array keys with this loop and place them in $var
foreach($decoded as $key => $value){
$var2[] = $key;
}
As i comment, use array_keys, and json_decode.
I don't believe that this question has an answer, So i did't answer
it. But i did it later.
You have an json, so you need to use json_decode now you jave an array where your keys are the desired value. so use array_keys.
$var = '{"4":true,"6":true,"8":true}';
$arr = json_decode($var, true);
echo '<pre>';
print_r(array_keys($arr));
Result:
Array
(
[0] => 4
[1] => 6
[2] => 8
)
Related
How to get particular value in this array in PHP
[{"key1":"value1","key2":"value2"}]
You can use to find specific key with value by using array_search function
$json = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($json, true);
echo array_search("value2", $array[0]); //key2
echo $array[0]["key2"] //value2
You can get specific value from the array by using PHP array_search() Function. Below is an example code that you need to follow,
Since provided PHP array is in JSON format you first need to decode it as follows,
$jsonArray = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($jsonArray, true);
So the decoded array will be like this,
Array
(
[0] => Array
(
[key1] => value1
[key2] => value2
)
)
And then you can use array_search() Function,
<?php
$jsonArray = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($jsonArray, true);
echo array_search("value1", $array[0]); // Search an array for the value "value1" and return its key. In this case the key will be "key1"
?>
Note:
array_search() function is the best bet. But you also can use a for-loop to iterate all values and check if matches the value that you need. But I recommend using array_search() function.
Hope this helps you!
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 a 2D array like
attendee_programs = [1 =>[100,101],
2 =>[100,101,102]
];
I want to get array_values() and array_unique() but only for the nested elements (sorry, not sure what the terminology is) ...I.E.
programs = [100,101,102];
Is there a php function for this? or do I need to loop through and assemble it manually?
Edit: All of the answers are very helpful and have shown me something new. Sorry I can only accept one.
You could use a clever combination of array_unique, array_reduce and array_merge to achieve this:
$a = array_unique(array_reduce($attendee_programs, 'array_merge', []));
Doing this might be end in an array with some gaps in the indizes - if you need gaples array keys, you have to add array_values at the end
$a = array_values($a);
You can use:
call_user_func_array('array_merge', array_values($attendee_programs));
to get values of nested array.
array_unique(call_user_func_array('array_merge', array_values($attendee_programs)));
to get unique values.
RecursiveIteratorIterator
RecursiveArrayIterator
Solution:
function flatten($array)
{
$rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
return iterator_to_array($rit, true);
}
echo '<pre>';
print_r(flatten($attendee_programs));
Result:
Array
(
[0] => 100
[1] => 101
[2] => 102
)
Yet another option:
$flat = array();
array_walk_recursive($attendee_programs, function($value) use (&$flat) {
$flat[] = $value;
});
$flat = array_unique($flat);
My String is
["Alchemy","Alchemy-w","Alchemy-b"]
How can I made it to like
Array (
[0] => Alchemy
[1] => Alchemy-w
[2] => Alchemy-b
}
Please help me, How I Can get this output in a Stranded manner else I have to cut it using sub-string or any other ordinary php functions.
the string is a valid json-string. so you could use json_decode:
$json = '["Alchemy","Alchemy-w","Alchemy-b"]';
var_dump(json_decode($json));
Your string is json. just do this:
<?php
$json = '["Alchemy","Alchemy-w","Alchemy-b"]';
$array = json_decode($json, true); //When TRUE, returned objects will be converted into associative arrays.
echo "<pre>"; //to get it displayed nicely
print_r($array);
?>
Your string is json format only, you can use following mathod.
$resp = '["Alchemy","Alchemy-w","Alchemy-b"]';
print_r(json_decode($resp));
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