Create Multi Array Json File in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In My Web Application I am Using Json. I Need to create following Format of Json File in PHP.
{
"title": "A cool blog post",
"clicks": 4000,
"children": null,
"published": true,
"comments": [
{
"author": "Mister X",
"message": "A really cool posting"
},
{
"author": "Misrer Y",
"message": "It's me again!"
}
]
}

Here is the solution:
Open Google
Type PHP JSON
hit Enter
Click the 1st result
Replace the values from PHP examples with json_encode with your own values

json_encode will encode a PHP array as a JSON array.
As the exmaple on that link shows:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
// {"a":1,"b":2,"c":3,"d":4,"e":5}

Related

Php - json_decode returns null [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to decode json string that looks like the one below with json_decode php function:
"{id:"1",fields:[{id:"1",value:"asdasd"},{id: "2",value:"asdasd"},{id:"3",value:"asdasd"}]}"
I have tried various options from this question. Like:
$foo = utf8_encode($json_string);
$data = json_decode($foo, true);
Or:
json_decode(str_replace ('\"','"', $json_string), true);
Even with:
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
But, everything I try I always get null.
Why is that?
First of all, your JSON is invalid. You should always first validate your JSON before asking with any JSON lint tool, which would tell you exactly where and what the error is.
Also, you should always check for errors when handling JSON in PHP. You can check out the json_last_error function's official documentation on how to properly perform the validation.
$json = 'your_json_string';
$array = json_decode($json, true);
if (json_last_error()) {
die('Invalid JSON provided!');
}
printf('<pre>Valid JSON output: %s</pre>', print_r($array, true));
Worth mentioning: since PHP 7.3 a JSON_THROW_ON_ERROR option was added so the code can be wrapped around with a try-catch block:
try {
$array = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
printf('Valid JSON output: %s', print_r($array, true));
} catch (\JsonException $exception) {
printf('Invalid JSON input: %s', print_r($exception, true));
}
Working example.
<?php
$data='{
"id": "1",
"fields": [{
"id": "1",
"value": "asdasd"
}, {
"id": "2",
"value": "asdasd"
}, {
"id": "3",
"value": "asdasd"
}]
}';
$dataNew=json_decode($data,true);
echo '<pre>';
print_r($dataNew);
Your json was not valid. The json-keys need to be inside double-quotes. After that json_decode will work fine.
Output is:
Array
(
[id] => 1
[fields] => Array
(
[0] => Array
(
[id] => 1
[value] => asdasd
)
[1] => Array
(
[id] => 2
[value] => asdasd
)
[2] => Array
(
[id] => 3
[value] => asdasd
)
)
)
Your JSON returns Null because in JSON, the keys should be string.Your JSON format is incorrect and hence returns Null.
Try rewriting it as:
{
"id":"1",
"fields":[
{
"id":"1",
"value":"asdasd"
},
{
"id": "2",
"value":"asdasd"
},{
"id":"3",
"value":"asdasd"
}
]
}

Confusion in conversion of array to json in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array $arr_name which prints out
{"name" => "tom", "age" => 10}, {"name" => "jack", "age" => 11}
how do i get it to print the desired output using php ?
Desired Output in JSON is as follows
{
"Students" : [ {"name" => "tom", "age" => 10}, {"name" => "jack", "age" => 11}
]
}
Sorry but i find this very very confusing and have spent the last 2 hrs trying to get "Students" key asssigned to have a value with the contents of $arr_name
PHP already has a built in function to convert an array into JSON:
json_encode
Here simple example given below like:
Put your array place of $put_your_array in loop:
$output=array();
foreach ($put_your_array as $key => $value) {
$output['Students'][]=array(
'name'=>$value['name'],
'age'=>$value['age']
);
}
echo json_encode($output);
And get output would you seem like json format...
the easiest way to do that, loop through the array then assign it to new array
Ex:
$new_array=array();
foreach(your array as $key => $val)
{
$new_array['Students'][] = $val;
}
return json_encode($new_array);

Concatenate URL and display the image in the URL PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
my question is how do you concatenate para1 and para2, and displays the image that is in the url for each of the data in the web1 array in PHP?
Here is an example of what I wanted.
From url1: www.example.com/jferf0923i092eijodsojs
Please keep in mind that these urls are just an example of what I want to achieve.
{"Web 1": {
"url1": {
"para1": "www.example.com",
"para2": /jferf0923i092eijodsojs,
},
"url2": {
"para1": "www.example.com",
"para2": asjdoisadj829332oijd,
},
"url3": {
"para1": "www.example.com",
"para2": assasdijoj21389445,
}
}}
I appreciate your help! Cheers
I assume this is roughly what you are looking for, at least it should give you a start:
<?php
$catalog = [
"Web 1" => [
"url1" => [
"para1" => "www.example.com",
"para2" => "jferf0923i092eijodsojs",
],
"url2" => [
"para1" => "www.example.com",
"para2" => "asjdoisadj829332oijd",
],
"url3" => [
"para1" => "www.example.com",
"para2" => "assasdijoj21389445",
]
]
];
foreach ($catalog as $entry) {
foreach ($entry as $url) {
$imageUrl = sprintf('https://%s/%s', $url['para1'], $url['para2']);
echo '<img src="' . $imageUrl . '">'."\n";
}
}
The output is valid html and should visualize the image when used inside an html page:
<img src="https://www.example.com/jferf0923i092eijodsojs">
<img src="https://www.example.com/asjdoisadj829332oijd">
<img src="https://www.example.com/assasdijoj21389445">

PHP generate json - list of categories [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I need to generate json code with php like this http://api.androidhive.info/contacts/
You are looking for json_encode(). Passing an associative array to it will return a JSON representation.
http://us1.php.net/json_encode
You can use an multi-dimensional array with the json_encode function to achieve this structure.
$var = array(
"contacts" => array (
array(
"id" => 1,
"name" => "foo"
),
array(
"id" => 2,
"name" => "bar"
)
)
);
echo json_encode($var);

comparing two arrays in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array in PHP $array which has elements like
$array['id'].. $array['name'] $array['class']
I have another array called $array1 which has element only $array1['uid'].
I want to match these two array on the basis of $array['id'] and $array['uid']
such that I want to get elements $array['id'] not equal to $array['uid']`
Is there any builtin function in PHP, I can do that in for each loop with my custome function , but is there any function?
Input is if $array has id=2,4,5,6 and $array has uid=2,4 then I should get $array id=5,6
Data in $array looks like this
{
"name": "abc",
"id": "37402526"
},
{
"name": "def",
"id": "506768590"
},
{
"name": "hij",
"id": "526405977"
}
And $array 1 like this
{
"id": "37402526"
},
{
"id": "506768590"
},
{
"
"id": "526405977"
}
If you can rewrite your code, to have the id's in array keys, than you could use array_diff_key():
$array = array(
'12' => array('name' => 'abc'),
'34' => array('name' => 'def')
);
$array2 = array('12' => true);
$result = array_diff_key($array, $array2);
Otherwise you can use array_udiff():
function my_id_cmp($a, $b) {
return strcmp($a['id'], $b['id']);
}
$result = array_udiff($array, $array1, 'my_id_cmp');
If your input is just in the format you used as example, then it is simple :
$array['id'] = implode(',', array_diff(explode(',', $array['id']), explode(',', $array1['uid'])));
Regards.

Categories