This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP : Create array for JSON
I have an array like this :
Array ( [0] => tag1 [1] => tag2 [2] => tags17c [3] => tags20 [4] => tags21)
I want put that array on javascript, and the format must be like this :
var Tag = ['tag1', 'tag2', 'tags17c', 'tags20', 'tags21'];
How to convert it?
please, help me out..
var Tag = <?php echo json_encode(array_map("htmlspecialchars", $your_array)); ?>;
My advice would be to use json for that, as Charly suggested, using json_encode. Though there is no real reason for using array_values() as far as I know,
var Tag = <?php echo json_encode($array); ?>;
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
So I got this array with associative object in PHP and I couldnot figure out how to get specific element
here is an array:
extra_fields => [
{"id":"1","value":"1055"},
{"id":"2","value":"Link"},
{"id":"3","value":"Name"}
]
I tried like this but it doesn't work
extra_fields[0]["value"]) and extra_fields[0]->value
Please help.
UPDATE:
full output code:
stdClass Object
(
[id] => 723
[title] => XXXXXXX
[alias] => XXXXXXX
[catid] => 50
[published] => 1
[introtext] =>
[fulltext] =>
[video] =>
[gallery] =>
[extra_fields] => [
{"id":"1","value":"1055"},
{"id":"2","value":"Link"},
{"id":"3","value":"Name"}
]
)
this is an $item coming out of Joomla CMS K2 plugin when I use print_r() command
I can access normal stuff like this $item->title and get XXXXXXX for my value, but could not figure out how to get items from extra_fields
as others mentioned, this doesn't seem valid PHP data, however if you would convert array of objects to multidimensional array, then do it this way:-
<?php
// new var that holds the arrays
$multiArray = [];
foreach ($extra_fields as $field){
$multiArray[] = (array) $field;
}
// now this var has the exact data that you want...
print_r($mulitArray);
Hope this helps.
The solution to my problem was pretty simple, I used json_decode() function to convert it too array
$someArray = json_decode($item->extra_fields, true);
print_r($someArray[0]["value"]);
Thank you all for your help
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 3 years ago.
I'm trying to slice a multi-array in php. I'm using google's geo-location api and I've managed to get it to return lat,lng and accuracy. I've used
$message = json_decode($result, true);
To convert the Json into an array.
The problem I have is that I have the results in this form:
Array ( [location] => Array ( [lat] => 10.1453652 [lng] => 0.2338764 ) [accuracy] => 33 )
and I can't figure out how to extract the lat and lon. Can anyone help?
Try the following code:
echo $message['location']['lat'];
It's simple, if not what you are expected, its straight forward
echo $message['location']['lat'].' '.$message['location']['lng'];
Working demo.
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];
This question already has answers here:
stdClass object array inside of another array
(3 answers)
Closed 5 years ago.
my code in PHP :
$stuff = array($this->complaint_model->get_reply($id)->result());
print_r($stuff);
result :
Array (
[0] => Array (
[0] => stdClass Object (
[id_customer] => 21
[id_complaint] => 2
[id] => 4
[nama_customer] => Muhammad Bima Zehansyah
[from] => Admin
[balasan] => coba update
)
)
)
my question is , how to get value [nama_customer] ? thx before guys
Try this
$stuff = array($this->complaint_model->get_reply($id)->result());
echo $stuffVal = $stuff[0][0]->nama_customer;
Get the value like this
$stuff[0][0]->nama_customer
Here you have multidimensional array object that's why you need to first Travers two array like $stuff[0][0] and then the object like $stuff[0][0]->nama_customer
Actually you do not need to put the result into additional array and it would be wise to check if the result is not empty.
So you just take a first item from your result (which is an object) and call the parameter nama_customer
$stuff = $this->complaint_model->get_reply($id)->result();
if (!empty($stuff[0]))
echo $stuff[0]->nama_customer;
This question already has answers here:
How to convert JSON string to array
(17 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"receiver_uid":[
"58a43a3e3fbbf3.61108490",
"58a43be07a3bc3.90311110",
"58da53ab5ce8d6.84754819"
]
}
This is the value I would like to convert to array. I know it's quite a simple question, but please can anyone help me?
Use json_decode
json_decode($your_jsonString,true)
You should use json_decode.
Try this:
$data = json_decode($your_json_string, TRUE);
The second parameter will make decoded json string into an associative arrays.
Example :-
$data = '{"receiver_uid":["58a43a3e3fbbf3.61108490","58a43be07a3bc3.90311110","58da53ab5ce8d6.84754819"]}';
$array = json_decode($data, true);
echo "<pre>";
print_r($array);
Output would be like
Array
(
[receiver_uid] => Array
(
[0] => 58a43a3e3fbbf3.61108490
[1] => 58a43be07a3bc3.90311110
[2] => 58da53ab5ce8d6.84754819
)
)