How to implement the request and response in php - php

I have URL like this, how we recive the value
like "abc.org/result.php?rq=[1,2,3,4]&rq1=[5]" etc
Can any one help me to convert rq into array when recive the value in result.php

You can try using explode() & array_map()
$rq = $_GET['rq'];
$arr = array_map(function($v){ return trim($v, ' []');}, explode(',', $rq));
print '<pre>';
print_r($arr);
print '</pre>';

You can use json_decode to convert into array
$a = json_decode($_GET['rq']);
print_r($a);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

You can use get request also:
Abc.com?req[]=1&req[]=2
$_GET['req']
Return
(1,2)

Related

PHP insert keys in string

I have the following string:
{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}
How can I get rid of the numbers preceding "http..." and transform this same numbers in array keys?
Like this:
[0] => "http...",
[1] => "http...",
[2] => "http...",
That looks like a JSON string, so you could decode it.
You could try
$array = json_decode($string, true);
You may also need to reindex the array so it is 0 based; so something like
$array = array_values(json_decode($string, true));
you are having an array data in JSON formation. you have to use a PHP function json_decode to get an result.
$php_array = json_decode($your_array[0], true);
//to see your array
print_r($php_array);
You are missing something I think.
Look at this snippet:
$a=[
0 => '{
"1":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari1.jpg",
"2":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari2.jpg",
"3":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari3.jpg",
"4":"http:\/\/localhost:8888\/classecar\/uploads\/dropzone\/ferrari4.jpg"
}'
];
echo "<pre>";
print_r(json_decode($a[0],TRUE));
it returns:
Array
(
[1] => http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg
[2] => http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg
[3] => http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg
[4] => http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg
)
This will work considering that the array value is a "string" containing a json object.
A var_dump on your array will give you an exact idea on what type the array value is.
Hope this helps:
<?php
//you might want to convert the JSON string to an array:
$json = '{"1":"http://localhost:8888/classecar/uploads/dropzone/ferrari1.jpg","2":"http://localhost:8888/classecar/uploads/dropzone/ferrari2.jpg","3":"http://localhost:8888/classecar/uploads/dropzone/ferrari3.jpg","4":"http://localhost:8888/classecar/uploads/dropzone/ferrari4.jpg"}';
$array = json_decode($json);
var_dump($array);
// here you already have the json converted to an php array
$arrayWithoutStrangeIndexes = [];
foreach($array as $index => $content){
$arrayWithoutStrangeIndexes[]= $content;
}
// here is just your array with plain data
var_dump($arrayWithoutStrangeIndexes);

Implode an array in php with specified type

I have this array :
Array
(
[0] =>
[1] =>
[2] => test1
[3] => test2
)
Now I make :
if(!empty($a_data)){
$a_return = array(implode(',"', array_filter($a_data)));
}
And I get this :
aReturn": [
"test1,\"test2"
]
But I want to get :
aReturn": [
"test1","test2"
]
Can you help me please ? Thx in advance and sorry for my english
$value = array_values(array_filter($a_data));
var_dump($value);
Here no need to store comma separated values again in an array you can use array_values() function here which will return the all values inside the array than you just need to use json_encode() for your desired output as:
<?php
$a_data = array('','','test','test2');
$a_return = array_values(array_filter($a_data));
echo json_encode($a_return);
?>
Output: ["test","test2"]
And if you still want to use your code than you just need to use stripslashes() for strip slashes as:
<?php
$a_data = array('','','test','test2');
$a_return = array(implode('","', array_filter($a_data)));
echo stripslashes(json_encode($a_return));
?>
Output: ["test","test2"]

PHP: How to implode these array values?

I have these code:
$arr = array($check);
When I try to
echo "<pre>";
die(print_r($arr));
it gives me result in the Firebug
<pre>Array
(
[0] => Array
(
[0] => 2
[1] => 1
)
)
Now, I want an output that will give me '2,1'.
How could I do that in PHP? Do I need to use implode?
Yes you need to implode:
echo implode(',', $check);
You don't need to put it inside an array:
$arr = array($check); // no need

How to format my data in php

I have an array in php, with print_r it looks like this
Array (
[0] => Array ( [0] => 2 [1] => 3 [2] => 3 [3] => 1 [4] => 1 [5] => 4 [6] => 1 )
[1] => Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 1 [4] => 1 [5] => 1 [6] => 1 )
)
when I do json_encode($mydata) it looks like this
[["2","3","3","1","1","4","1"],["1","2","2","1","1","1","1"]]
but I need this without the quotes, as the next processing simply needs a dataset like this:
var dataset = [ 5, 10, 15, 20, 25 ];
the dataset was never intended to use json standards, I simply need to get my array formatted correctly.
so the question is either "how do I remove quotes from json" which is simply nonstandard invalid json and won't get me the responses I'd need
OR
"how do I format this php array to be like the dataset".
it either involves a different kind of print function in php, or it involves some forloop regex in javascript
You need to use the JSON_NUMERIC_CHECK flag, this will force all numeric strings to be converted to numbers.
Like so:
json_encode($array,JSON_NUMERIC_CHECK);
Please note that this flag is only available from PHP version 5.3.3
Ehhh you could try this:
$d = array();
foreach($myData as $data) {
$d[] = "[" . implode(",", $data) . "]";
}
echo "[" . implode(",", $d) . "]";
Demo: http://codepad.org/nTveAGWm
Although echoing out json_encode($myData); worked as well: http://codepad.org/KTfQHz6s
The reason json_encode is putting quotes is because the data is a string.
Try this example to view the difference:
$arr = array(array(4,5,6), array(8,10, "11"));
print_r($arr);
echo "<br>";
echo json_encode($arr);
You can try to use intval to parse it as int before json_encoding
you could try this
$c = array(1,2,3);
echo "Non-associative array output: ", json_encode($c), "\n";
return this:
Non-associative array output: [1,2,3]
using your example string u can do smth like this
$arr = json_decode('[["2","3","3","1","1","4","1"],["1","2","2","1","1","1","1"]]');
foreach ($arr as &$val)
foreach ($val as &$item)
$item = (int)$item;
var_dump(json_encode($arr));
finally i get
string(33) "[[2,3,3,1,1,4,1],[1,2,2,1,1,1,1]]"

PHP two dimensional array to Javascript array

I had php multi dimensional array
Array
(
[0] => Array
(
[WorkHrs] => 9826
[Focus_Date] => 2010-02-10
)
[1] => Array
(
[WorkHrs] => 9680
[Focus_Date] => 2010-02-11
)
)
and I want to convert it in Javascript to
myArray = [['2010-02-10', 9826],['2010-02-11', 9680]];
$jsArray = array();
foreach($myArray as $array) {
$jsArray[] = array($array['Focus_Date'], (int) $array['WorkHrs']);
}
echo json_encode($jsArray);
echo json_encode(array_map(array_values, $arr));
EDIT: To get it in the specified order:
function to_focus_work_array($arr)
{
return array($arr['Focus_Date'], $arr['WorkHrs']);
}
echo json_encode(array_map('to_focus_work_array', $arr));
json_encode
That's pretty much exactly what json_encode does. Input is a PHP-array (other datatypes accepted), output is what you describe.
have you tried the json_encode()?
refer to http://php.net/manual/en/function.json-encode.php

Categories