This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How to filter an array by a condition
(9 answers)
Closed 5 years ago.
How can I remove the key "src" from this JSON if it is found in the array?
$json = '[{"label":"360","file":"http://aaa","src":"http://aaa"},
{"label":"480","file":"http://bbb","src":"http://bbb"},
{"label":"720","file":"http://ccc","src":"http://ccc"},
{"label":"1080","file":"http://ddd","src":"http://ddd"}]';
The desired end result is this:
$json = '[{"label":"360","file":"http://aaa"},
{"label":"480","file":"http://bbb"},
{"label":"720","file":"http://ccc"},
{"label":"1080","file":"http://ddd"}]';
Well assuming your json was correct (the above isn't as it's missing [] around it), corrected below.
$json = '[{"label":"360","file":"http://aaa","src":"http://aaa"},{"label":"480","file":"http://bbb","src":"http://bbb"},{"label":"720","file":"http://ccc","src":"http://ccc"},{"label":"1080","file":"http://ddd","src":"http://ddd"}]';
$decoded = json_decode($json, true);
foreach($decoded as $id => $row) {
unset($row[$id]['src']);
}
$json = json_encode($decoded);
Related
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
I need convert from data = "[[0,1], [1,1]]" to data = [[0,1], [1,1]] in javascript, remove double quotes . It's possible?
Thanks
Just use the JSON object:
data = JSON.parse("[[0,1], [1,1]]"); // data === [[0,1], [1,1]]
This question already has answers here:
Send JavaScript variable to PHP variable [duplicate]
(3 answers)
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 3 years ago.
I am trying to integrate the php and javascript. I send the javascript variable into php variable. And printed it out in the browser using echo command. It outputs the exact same result that I am expecting. I write following command;
<script>
var p1 ='Lohuti'
</script>
<?php
$p1="<script>document.write(p1);</script>";
$db_connection = pg_connect("host=localhost dbname=postgres user=postgres password=admin");
$sql = "SELECT * FROM public.flood WHERE jamoat= '".$p1."'";
echo $sql;
$result = pg_query($db_connection, $sql);
while ($row = pg_fetch_row($result)) {
$row[0]
}
?>
The echo $sql prints outs SELECT * FROM public.flood WHERE jamoat= 'Lohuti'. But this query unable to fetch the data from a database. If I just replace the php variable $p1 with the name Lohuti, It works fine, I can fetch the data. In my case, I am planning to add the dynamic javascript variable into php variable and query the required result. Is there any solution to this type of problem?
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have a JSON file, but I can't decode it like any other JSON file.
Where am I making a mistake?
$jsonData = '[[[],[{"file_id":"2_U_3","sub_file_id":"2_U_3_1","option_file":[0,3.44827586207],"file_votes":1}],[],[],[],[]],{}]';
$jsonDec = json_decode($jsonData, TRUE);
echo $jsonDec["file_id"] . " = " . $jsonDec["option_file"][2];
Result: empty screen
This question already has answers here:
Why json_decode doesn't work for me?
(6 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
Disclaimer: I have read and searched, quite a lot.
I am building my data to send via AJAX like this:
var plugin_data = [
plugin_action_button.attr('data-action-to-take'),
plugin_action_button.attr('data-plugin-slug')
];
requestPluginAction( JSON.stringify(plugin_data) );
If I am to console.log, then it translates to:
["activate","handle"]
If I am to return what PHP is seeing, here's what it is:
[\"activate\",\"handle\"]
Now, running a simple json_decode on that string:
check_ajax_referer( 'plugin_routines', 'security' );
$data = sanitize_text_field( $_POST['plugin_install_request_data'] );
wp_send_json( json_decode( $data ) );
Returns null.
I've tried a lot of things. json_decode with true set on, simply returning the unfiltered value I got from AJAX, etc.
But none works.
I'm ultimately looking to convert that JSON to a PHP array.
This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 7 years ago.
I need to be able to grab some data from a string using PHP.
I got an API from the games website and need to break it down.
The string I need to break down is this:
http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513
I need to get the small icon image from that string which is the first url in the string, and the current price, which in the string is 887.
So, where it states this:
"current":{"trend":"neutral","price":887}
I need to grab the 887 and put it into a variable.
I'm using PHP,
thanks in advance if anyone can help :)
Use json_decode() for this purpose:
$item = json_decode($json)->item;
$price = $item->current->price;
$icon = $item->icon;
Download the JSON and convert to an object.
$item = json_decode(file_get_contents('http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513'))->item;
$name = $item->name;
// etc.
Here's a function you should keep handy. It is useful for printing formatted objects and arrays, which makes it much easier to examine their structures.
function debug($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}