How to convert string to PHP array? [duplicate] - php

This question already has answers here:
Is there a PHP function to convert a query string to an array?
(2 answers)
Convert backslash-delimited string into an associative array
(4 answers)
Closed 6 years ago.
I have the following string
sender=48&destination=51&message=hi+good&sender=48&destination=49&message=good+boy
Please help me convert that into PHP array as following
array = array(
'sender'=>48,
'destination'=>51,
'message'=>hi+good,
'sender'=>48,
'destination'=>49,
'message'=>good+boy
);
Note: Its not PHP GET.

This should work as inteded, to solve this problem, you just need to use explode() correctly, otherwise it's easy.
Here you go :
$firstarr=explode('&',$yourstring);
$desiredarr=array();
foreach($firstarr as $pair)
{
$exp_pair=explode('=',$pair);
$desiredarr[$exp_pair[0]]=$exp_pair[1];
}
print_r($desiredarr);

If it is from query string then you can just use $_REQUEST otherwise you need to explode() string using & as separator. Then for each item in array that explode() generate, you split with = and add it to final array
or using parse_str().

Related

Convert js stringified string to multidimensional array in php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I get this kind of string from JS to PHP.
[[1,"10.00","10.00","A"],[2,"20.00","25.00","B"],[3,"10.00","25.00","C"],[6,"10.00","25.00","D"]]
How can I convert it to a php multidimensional array?
Note:
I tried json_encode without getting the desired result.
<?php
$string = '[[1,"10.00","10.00","A"],[2,"20.00","25.00","B"],[3,"10.00","25.00","C"],[6,"10.00","25.00","D"]]';
$result = json_decode($string);
var_dump($result);
?>
You should use json_decode instead of json_encode for convert JSON to array PHP
Result:

php array count returns 1 [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Why does counting an empty string return 1?
(3 answers)
Count() returning 1 with comma separated string with explode [duplicate]
(2 answers)
Closed 3 years ago.
i have this data which when tested with direct input it works and file_put_contents confrims that data is exactly same but when i try to get the value through site it gives only 1
i tried to declare array but everytime it gives 1 only
this array count gives count as 6
$total_id_counttt = count(array(13068,13067,13066,13065,13064,13063));
echo $total_id_counttt;
but when i use this here it return 1
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = array($str_finalppo);
$total_id_counttt = count($schools_array);
it returns 1 can anyone tell where i am doing mistake
First of all, Convert the string into an array. Look it in this code you may get the idea.
Use explode function to convert string to array.
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
$total_id_counttt = count($schools_array);
Check more about explode
In PHP, value in quotes('') consider as string.
So when you try to count this is always returning the 1.
Try to convert the string into an array by using explode().
for example:
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
echo $total_id_counttt = count($schools_array);
this will give you the right answer.
And if you still confuse try to print #schools_array before using dd($schools_array);
It will definitely remove your confusion.

Separate the key from the value [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I set up a new site and want a workaround for the problem.
I have this text in the mysql table
{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}
I want to fetch the ID and its value by php
For example
$id[200] = 5
You can use json_decode() to convert JSON string into an array. Since your JSON string contains numeric indexes so you need to use curly brackets to fetch the values
$str = '{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}';
$array = json_decode($str);
echo $array->{'200'};
Or you can add second parameter in json_decode as true to fetch the value as normal array
$array = json_decode($str,true);
echo $array[200];
Here is the live demo

PHP: How to make an array by a comma separated string? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 5 years ago.
I have this code: 37,40,42,46,49,54,56,57 now I wanna separate each number and convert all numbers to an Array.
The array that I want is:
array(37,40,42,46,49,54,56,57)
You can use explode() like this:
$string = "37,40,42,46,49,54,56,57";
$array = explode("," , $string);

How can I parse string php [duplicate]

This question already has answers here:
Parsing JSON object in PHP using json_decode
(7 answers)
Closed 8 years ago.
how can I parse this string into an array using php?.
[[1406906881,9.3],[1406907001,9.5],[1406907061,9.5],[1406907121,9.8],[1406907181,9.4],[1406907241,9.6],[1406907361,9.3],[1406907421,9.3],[1406907481,9.1],[1406907541,9.2],[1406907601,9],[1406907661,9],[1406907781,8.9],[1406907841,9],]
Looks like you have a trailing comma
[1406907661,9],[1406907781,8.9],[1406907841,9],]
^
Not sure if that's what you're asking about.
You can parse this using json_decode() function in PHP. That string looks like json array.
Also, fix that trailing comma in the string (as mentioned by Alf).
Working example:
<pre>
<?php
$jsonArr = json_decode('[[1406906881,9.3],[1406907001,9.5],[1406907061,9.5],[1406907121,9.8],[1406907181,9.4],[1406907241,9.6],[1406907361,9.3],[1406907421,9.3],[1406907481,9.1],[1406907541,9.2],[1406907601,9],[1406907661,9],[1406907781,8.9],[1406907841,9]]');
print_r($jsonArr);
?>
Outputs:

Categories