PHP: Convert array string to an array in php - php

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));

Related

How to get particular value in this array in php

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!

convert json Array to single Json Object

I was wonder whether it is possible to convert a json Array to a single json object in php?
I mean below json Array:
[{'x':'y','k':'l'}, {'q':'w', 'r':'t'}]
Can be converted to:
{'0':{'x':'y','k':'l'}, '1':{'q':'w', 'r':'t'}}
Use json_encode with the JSON_FORCE_OBJECT parameter
$json = "[{'x':'y','k':'l'}, {'q':'w', 'r':'t'}]";
$array = json_decode($json, true); // convert our JSON to a PHP array
$result = json_encode($array, JSON_FORCE_OBJECT); // convert back to JSON as an object
echo $result; // {"0":{"x":"y","k":"l"},"1":{"q":"w","r":"t"}}
JSON_FORCE_OBJECT
Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0.
Please try below code.
$array[] = array('x' => 'y','k' => '1');
$array[] = array('q' => 'w','r' => 't');
echo json_encode($array,JSON_FORCE_OBJECT);

How can make array of string

$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
)

PHP Unserialize custom format & compare

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 )

PHP json_encode issue

I have a bunch of values and a PHP array and I need to convert it to a JSON value for posting via CURL to parse.com
The problem is that PHP arrays are converted to JSON objects (string as key and value, vs string as just value)
I end up with
{"showtime":{"Parkne":"1348109940"}}
Rather then
{"showtime":{Parkne:"1348109940"}}
And parse complains that this is a object not an array and therefore won't accept it.
As
{"showtime":{"Parkne":"1348109940"}}
is a JSON object (key = a string)
Is there anyway to do this using json_encode? Or some solution?
That's the JSON spec: Object keys MUST be quoted. While your first unquoted version is valid Javascript, so's the quoted version, and both will parse identically in any Javascript engine. But in JSON, keys MUST be quoted. http://json.org
Followup:
show how you're defining your array, unless your samples above ARE your array. it all comes down to how you define the PHP structure you're encoding.
// plain array with implicit numeric keying
php > $arr = array('hello', 'there');
php > echo json_encode($arr);
["hello","there"] <--- array
// array with string keys, aka 'object' in json/javascript
php > $arr2 = array('hello' => 'there');
php > echo json_encode($arr2);
{"hello":"there"} <-- object
// array with explicit numeric keying
php > $arr3 = array(0 => 'hello', 1 => 'there');
php > echo json_encode($arr3);
["hello","there"] <-- array
// array with mixed implicit/explicit numeric keying
php > $arr4 = array('hello', 1 => 'there');
php > echo json_encode($arr4);
["hello","there"] <-- array
// array with mixed numeric/string keying
php > $arr5 = array('hello' => 'there', 1 => 'foo');
php > echo json_encode($arr5);
{"hello":"there","1":"foo"} <--object
Blind shot... I have the impression that your PHP data structure is not the one you want to begin with. You probably have this:
$data = array(
'showtime' => array(
'Parkne' => '1348109940'
)
);
... and actually need this:
$data = array(
array(
'showtime' => array(
'Parkne' => '1348109940'
)
)
);
Feel free to edit the question and provide a sample of the expected output.
You can convert your array to JSON using json_encode
assuming your array is not empty you can do it like this
$array=();
$json = json_encode($array);
echo $json;
It sounds like you need to take your single object and wrap it in an array.
Try this:
// Generate this however you normally would
$vals = array('showtime' => array("Parkne" => "1348109940"));
$o = array(); // Wrap it up ...
$o[] = $vals; // ... in a regular array
$post_me = json_encode($o);

Categories