Fastest way to convert php associate array to string [duplicate] - php

This question already has answers here:
Convert flat array to a delimited string to be saved in the database
(13 answers)
Closed 6 years ago.
What will be the fastest and best way to convert the following PHP associate array to a string without json.
array
Array ( [0] => A [1] => S [2] => G)
to a string, exactly like
String = "A, S, G";
Most of the solutions available are using JSON, that I don't want to use. plus I want to know the "Fastest" and "Best" way.

Use the implode function:
$str = implode(array('a', 'b', 'c'));
http://php.net/manual/en/function.implode.php

Use
implode()
And read more here http://php.net/manual/en/function.implode.php

One way is using print_r(array, true) and it will return string representation of array

Related

I want to convert this array like this [duplicate]

This question already has answers here:
Convert flat array to a delimited string to be saved in the database
(13 answers)
Closed 10 months ago.
I want to convert this array ["Sleep","Wake"] to string like this Sleep Wake .
What is the way to do this?
Use implode,
$array = ['Sleep', 'Wake'];
var_dump(implode(" ", $array));

Turning PHP key=>value into sub array: [key,value] [duplicate]

This question already has answers here:
reformat an php array [duplicate]
(2 answers)
Closed 10 months ago.
I'm having following data:
Array ( [one] => this [two] => this2 )
Which I want to convert to json type which looks like:
data:{[one,this],[two,this2]}
How can I get through this in effecient manner?
Edit:
I've tried a lot of things This is actual data which I need to make datatable compatible:
{"draw":0,"recordsTotal":null,"recordsFiltered":null,"data":‌​[[{"first":[""],"sec‌​ond":[""],"third":["‌​"],"fourth":[""],"fi‌​fth":[""],"sixth":["‌​value"]}]]}
as the data here is in key=>value form json is not compatible for datatables (PHP)
You can use array_map and array_keys:
$result = array_map(null, array_keys($array), $array);
$json = json_encode($result);
Here is working demo.

How to filter a string with php? [duplicate]

This question already has answers here:
What kind of string is this? How do I unserialize this string? [duplicate]
(2 answers)
Closed 5 years ago.
I have a plugin exporting a bunch of strings from a database for me. The data returned is in a format like this
a:3:{i:0;s:8:"Skeletal";i:1;s:6:"Cardio";i:2;s:8:"Muscular";}
a:3:{i:0;s:14:"Access to care";i:1;s:15:"Confidentiality";i:2;s:20:"Consent to treatment";}
I can apply a php function to filter the data, how would I get it to return like this, using a function.
Skeletal, Cardio, Muscular
Access to care, Confidentiality, Consent to treatment
Those strings are serialized variables. Arrays specifically. use unserialize() to get the array and then join() to comma separate it the way you want.
$unserialized_array = unserialize($string);
$comma_separated = join(', ', $unserialized_array);
echo $comma_separated;
edit: I think this is the simplest solution, but Obsidian Age's answer below provides a regex that will also do what you want (and you did ask for a regex);
Those are serialized arrays, you need to unserialize them
You can do this with a really simple preg_match_all() of /".*?"/:
<?php
$string = 'a:3:{i:0;s:8:"Skeletal";i:1;s:6:"Cardio";i:2;s:8:"Muscular";}';
preg_match_all('/".*?"/', $string, $matches);
print_r($matches);
Now $matches contains an array that you can simply access the indexes of:
Array
(
[0] => "Skeletal"
[1] => "Cardio"
[2] => "Muscular"
)
I've created a working sample of this here.
Hope this helps! :)

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

What does => mean in php? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
I'm learning about arrays and am not certain what => means or does.
Assignment of a value to a named key.
http://www.php.net/manual/en/language.operators.assignment.php
$example = array('color' => 'blue');
echo $example['color']; //prints blue
I believe it is just syntax for writing array literals that behave like maps / dictionaries.
$mydict = array('hello' => 'world');
The above is an array with one element. But instead if indexing that element with an integer index, you use the key 'hello':
echo $mydict['hello']

Categories