Separate the key from the value [duplicate] - php

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

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:

proper syntax for accessing values from dictionary [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
php parsing json data
(4 answers)
Closed 1 year ago.
I have the following code that generates a dictionary of key/value pairs in php:
$json = file_get_contents('php://input');
$obj = json_decode($json);
print_r(json_encode($obj));
print_r produces the following output:
{"email":"someemail#gmail.com","password":"compas"}
how do you output just the value of the email key? Why doesn't $obj["email"] work?
To turn the json_decode to an arry use following form see manual
$myjson = '{"email":"someemail#gmail.com","password":"compas"}';
$obj = json_decode($myjson,true);
print_r($obj[email]);
returns someemail#gmail.com

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.

Extract the values from the array which is inside the object [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
if i solve this i will have my desired result for any expert this will take few minutes to guess and i am stuck in this from last 2 hours searching and googling but couldn't found the one i want. Okay so here is the thing ...
I am sending data through ajax to my php by doing JSON.stringify and receving that that to my php in the form of this
{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}
All i want to do is to get the values and insert them to the separate variables.
You need to use json_decode to convert the string to array. You can pass the second parameter to this method to convert this to array instead of object. Then you can retrieve the data like
$string = '{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}';
$array = json_decode($string, true);
print_r($array[0][0]);
Here's the example of it https://repl.it/HhI8/1

How to convert string to PHP array? [duplicate]

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().

Categories