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

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

Related

json_encode() is not returning a json array [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 months ago.
Improve this question
I am trying to encode an associative array in php
$data = array(
"firstName" => $_POST["firstName"],
"lastName" => $_POST["lastName"],
"email" => $_POST["email"],
"telephone" => $_POST["telephone"]
);
however the output is in string format and not in array format.
string(98) "{"firstName":"rob","lastName":"shelford","email":"some#domain.co.uk","telephone":"01245454545"}"
The output needs to have the square brackets so the receiving server can read the data correctly.
Is there another syntax for the $data array I need to use?
EDIT 1
The offical PHP documentation https://www.php.net/manual/en/function.json-encode.php
states that
echo "Normal: ", json_encode($a), "\n";
can be used to output
Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
however when I try to use the "\n" flag, I receive an error
EDIT 2
The entire data I wish to convert
$data = array(
"applicants" => array(
"firstName" => $_POST["firstName"],
"lastName" => $_POST["lastName"],
"email" => $_POST["email"],
"telephone" => $_POST["telephone"]
),
"buyerType" => $_POST["buyerType"],
"organisationId" => "xxxxxxxxxx",
"introducerId" => "0",
"introducerBranchId" => "0",
"introducerNegotiatorId" => "0",
);
EDIT 3
I have made it work by passing this into json_encode() :
$data = array(
"applicants"=> [
array(
"email"=> $_POST["email"],
"firstName"=> $_POST["firstName"],
"lastName"=> $_POST["lastName"],
"telephone"=> $_POST["telephone"]
)
],
"buyerType" => $_POST["buyerType"],
"organisationId" => "xxxxxxxxxx",
);
I have also removed the unnecessary lines in the above example
EDIT 4
The actual output I was expecting, and actually got, after implementing EDIT3
string(138) "{"applicants":[{"email":"some#domain.co.uk","firstName":"rob","lastName":"test","telephone":"01200000000"}],"buyerType":"Purchase"}"
If you want nested array to be encoded with square brackets surrounded, just put it in an array in php.
$data['applicants'] = [$data['applicants']];
echo json_encode($data);
FYI indexed array [1,2] will be encoded to json list [1,2], and associative array ['x'=>1,'y'=>2] will be encoded to json object {"x":1,"y":1}.

convert array into two different string php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have an array with following elements:
$arr = array(
'nick' => "blabla",
'pass => "blabla2"'
);
I would like to convert it somehow to strings, the first string would be the value of nick - "blabla", the second string would be the value of pass - "blabla2"
Thank you.
If you want to convert elements of the array to separate string variables you can use extract function to import elements from an array to variables. For example:
$arr = array(
'nick' => "blabla",
'pass' => "blabla2"
);
extract($arr);
echo $nick, ' ', $pass;
$arr = [
'nick' => "blabla",
'pass' => "blabla2"
];
$string_one=$arr['nick'];
$string_two=$arr['pass'];

Create Multi Array Json File 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 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}

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