Store array data in single line PHP - php

Hello i want to store my array data into variable.
$array = array('0' => "14254",'1' => "145245");
// I want to store this array value into normal single variable
// store array data to $string.
// something like this >> $string is 14254,145245

Use array's implode() method.The implode() function returns a string from the elements of an array.
<?php
$array = array('0' => "14254",'1' => "145245");
$script = implode(',',$array);//outputs 14254,145245
echo $script;
?>
For more see manual PHP Implode

Look at 'serialize' function in PHP. It will provide you array as a string that later could be converted back to array with 'unserialize'

Related

How to store $_Post values separated by &

I did
file_put_contents("x.txt", print_r($_POST, true));
And I got the below output
Array
(
[merchant_id] => ece6ba4ecx24d070b4c13b
[invoice_id] => 1772fb6eafda04ceb11c77cf719a347f
[invoice_created] => 1634467789
[invoice_expires] => 1634469589
[invoice_amount] => 10
)
How do I store $_POST values only (not keys) separated by & in form of a string like the one below:
Will this work
echo implode("&",$_POST);
Output example:
ece6ba4ec435986db93924d070b4c13b&1772fb6eafda04ceb11c77cf719a347f&1634467789&1634469589&10
Use implode() and set & as the separator:
$string = implode('&', $_POST);
You can use the built-in function of PHP array_values() to get only the values from the $_POST array.
Then, use the built in function implode, to combine the whole array into one string seprated by & sign.
$values = array_values($_POST);
$string_values = implode("&",$values);

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 )

jQuery to PHP Array and string

I'm trying to get the checked values into a PHP array so that I am able to loop through the array but I can't seem to convert it to an array.
jQuery Ajax Posts
checked=28,24
PHP
$checked = $_POST['checked'];
$arr = array($checked);
print_r($arr);
OUTPUT
Array
(
[0] => 28,24
)
Use explode function to convert comma separated string to an array,
$arr = explode(",",$checked);

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

Convert Array into Key Value Pair array

I have a comma separated string which i explode into an array. If the array is of un-known length and i want to make it into a key value pair array where each element in the array has the same key, how do i do this? i'm assuming i'd have to use array_combine? can anyone give me an example using the array bellow? :
for instance:
array([0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape)
into:
array([animal]=>zebra, [animal]=>cow, [animal]=>dog, [animal]=>monkey, [animal]=>ape)
You can't use the same key for each element in your array. You need a unique identifier to access the value of the array. When you use animal for all, what value should be used? What you can do is to make a 2 dimensional array that you have an array inside an array:
array(
[animals] => array(
[0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape
)
)
this can be used with $array['animals'][0]
But still you need numbers or unique identifiers to access the values of the array.
Something like this:
$string = 'zebra,cow,dog,monkey,ape';
$array = explode(',', $string);
$arrayReturn['animals'] = $array;
print_r($arrayReturn);
u cant have same key for all the values but u can do this
lets say your string is
$a = 'dog,ant,rabbit,lion';
$ar = explode(',',$a);
$yourArray = array();
foreach($ar as $animals){
$yourArray['animals']=$animals;
}
Now it doesnot matter how long your string is you will have you array as
$yourArray['animals'][0]='dog'
$yourArray['animals'][1]='ant'
....... so on ......

Categories