Count how many of the same element in json - php

Im parsing json with php in this simple piece of code
<?php
$json = file_get_contents('list.json');
$data = json_decode($json,true);
$records=$data['records'];
foreach($records as $record)
{
echo $record['number']." ".$record['opened_at'];
}
?>
But i want to echo how many 'number' element are in my json file exemple bellow
{
"records":[
{
"number":"INC018****",
},
{
"number":"INC018****",
},
{
"number":"INC018****",
},
{
"number":"INC018****",
},

<?php
$json = file_get_contents('list.json');
$data = json_decode($json,true);
$records=$data['records'];
$numberCount = 0;
foreach($records as $record)
{
$numberCount += isset($record['number']); // Here we go
echo $record['number']." ".$record['opened_at'];
}
echo $numberCount;
?>

This function might be helpful: http://www.php.net/manual/en/function.array-count-values.php
$counts = array_count_values($records);
$numberCount = $counts['number'];

Related

Get data from json file without index in php

I have a JSON file like this
[
{
"item_id": "342",
"item_title": "James"
},
{
"item_id": "374",
"item_title": "Fred"
}
]
And I would like to get the item_title in PHP but using the item_id.
How would I go about this?
I have
$item_id = 374;
$url = 'file.json';
$data = file_get_contents($url);
$items = json_decode($data);
Thank you.
You can use array_search() and array_column() if you know that key will always exist
echo $items[array_search($item_id,array_column($items,'item_id'))]['item_title'];
https://3v4l.org/Qddcj
Otherwise go for functional approach
$items = json_decode($data,true);
function getItemTitle($array,$item_id){
$key = array_search($item_id,array_column($array,'item_id'));
if($key !==false && isset($array[$key]['item_title'])){
return $array[$key]['item_title'];
}else{
return "no title found for given id ".$item_id;
}
}
echo getItemTitle($items,$item_id);
Output:- https://3v4l.org/ikKWY
In case if same id can be repeated multiple time
$items = json_decode($json,true);
foreach($items as $value){
if($value['item_id']==$item_id){
echo $value['item_title'].PHP_EOL;
}
}
Output:- https://3v4l.org/7oPHk
Some explanation: you should use foreach loop with if condition
Here is the sample code
<?php
$json='[
{
"item_id": "342",
"item_title": "James"
},
{
"item_id": "374",
"item_title": "Fred"
}
]';
$item_id = 374;
$items = json_decode($json);
//print_r($items);
foreach($items as $value){
if($value->item_id==$item_id){
echo $value->item_title;
}
}
You can check the desired output here

PHP Echo json_encode

I am having a small problem, I am showing json using PHP, I have this code:
foreach($result as $r){
$returnEcho["Id"] = $r["id"];
$returnEcho["Username"] = $r["username"];
$returnEcho["Email"] = $r["email"];
$returnEcho["Info"] = $r["Info"];
echo json_encode($returnEcho);
The problem with this code is, it will display the JSON like this:
{"Username":"X","Email":"X","Info":"X"}
{"Username":"X","Email":"X","Info":"X"}
{"Username":"X","Email":"X","Info":"X"}
But what I want is something like this:
[
{
"Username":"X",
"Email":"X",
"Info":"X"
},
{
"Username":"X",
"Email":"X",
"Info":"X"
}
]
How can I do this?
Thanks.
You have to build everything in an array and echo the json only after foreach finish. Like this:
$return = [];
foreach($result as $r){
$returnEcho["Id"] = $r["id"];
$returnEcho["Username"] = $r["username"];
$returnEcho["Email"] = $r["email"];
$returnEcho["Info"] = $r["Info"];
$return[] = $returnEcho;
}
echo json_encode($return);

Reading item in PHP Multi Array

I would like to read the following JSON info in PHP using a foreach. Im new to this and need some help. I have only included a small sample of the data for privacy reasons.
The end goal is to get all the "id" and the "real_address" in the server array.
[
{
"id":"d87df8g7sdfg89",
"status":false,
"servers":[
{
"status":false,
"platform":null,
"server_id":"adsfasdfasdfasdf",
"virt_address6":"fd00:c0a8:f800:0:192:168:248:5",
"virt_address":"192.168.248.5",
"name":"Private",
"real_address":null,
"connected_since":null,
"id":"aasdfasdfasdfsafde",
"device_name":null
}
],
},
{
"id":"asd89asd8f",
"status":true,
"servers":[
{
"status":true,
"platform":"linux",
"server_id":"fasdsdfasdfasdf",
"virt_address6":"fd00:c0a8:f800:0:192:168:248:3",
"virt_address":"192.168.248.3",
"name":"Private",
"real_address":"5.5.5.52",
"connected_since":1447406908,
"id":"asdfasdfasdfasdf",
"device_name":"thriving-fields-2667"
}
],
}
]
You can use json_decode
$data = json_decode($json, true);
foreach ($data as $value) {
foreach ($value['servers'] as $server) {
echo $server['real_address'];
}
}
something like this ...
$items = json_decode($json);
foreach($items as $item){
foreach($item->servers as $server)
{
echo $server->server_id . " - ". $server->status;
}
}
If you have PHP >= 5.5.0 for array_column() you can try this:
$array = json_decode($json, true);
foreach (array_column($array, 'servers') as $server) {
echo $server['real_address'];
}

JSON parsing with PHP - most efficient way

I send JSON object to server. On server side I have to parse this obj with PHP.
I'm stuck in the loop. I don't know how to proceed inside loops.
Im looking for most efficient way to parse this object and save all variables into DB.
[
{"ADI":{"id":-1,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ALE":{"_id":1,"_name":"Milk","contain":false}},
{"ALE":{"_id":2,"_name":"cfg","contain":false}},
{"ALE":{"_id":4,"_name":"Lakt","contain":false}},
{"PRO":{"image":"","code":"123456","name":"jfbj"}},
{"USER":{"email":"spam#spam.com"}}
]
For now I have done this:
$string = file_get_contents('php://input');
$array = json_decode($string, true);
//print_r($array);
foreach ($array as $t => $index) {
foreach ($index as $vas => $r) {
//Here I'm stuck!!!
}
}
you can grab values from json
<?php
$string = '[
{"ADI":{"id":-1,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ALE":{"_id":1,"_name":"Milk","contain":false}},
{"ALE":{"_id":2,"_name":"cfg","contain":false}},
{"ALE":{"_id":4,"_name":"Lakt","contain":false}},
{"PRO":{"image":"","code":"123456","name":"jfbj"}},
{"USER":{"email":"spam#spam.com"}}
]';
$array = json_decode($string, true);
echo "<pre>";
//print_r($array);exit;
for ($i=0;$i<=(count($array)-1);$i++){
//print_r($array[$i]);
if (array_key_exists("ADI",$array[$i])) {
$ArrVal = $array[$i]['ADI'];
$id = $ArrVal['id'];
$danger = $ArrVal['danger'];
echo "$id,$danger ";
}
}
?>
check this out :
echo "<pre>";
print_r($array);
for ($i=0; $i < count($array); $i++) {
if(isset($array[$i]["ADI"])){
print_r($array[$i]["ADI"]);
}
if(isset($array[$i]["ALE"])){
print_r($array[$i]["ALE"]);
}
}
echo "</pre>";

Create foreach loop from JSON array

I'm having problem with creating a foreach loop out of a JSON, I can't get the values out of the array correct, what am I doing wrong?
JSON:
[
{"Pages":{
"name":"Name 1",
"id":"3342939832994"
}
},
{"Pages":{
"name":"Name 2",
"id":"289051164453763"
}
}
]
PHP:
$json = $_POST['Publish'];
$json = $json->Pages
foreach($json as $key => $items) {
$id = $items->id;
$name = $items->id;
}
Do it like this
$json = json_decode($_POST['Publish']);
json_decode - Takes a JSON encoded string and converts it into a PHP variable.
You can use this code
<?php
$array = json_decode($_POST['Publish'], true);
foreach($array as $item) {
$id= $item['Pages']['id'];
$name = $item['Pages']['name'];
echo "id: $id <br/> name: $name <br/><br/>";
}
?>

Categories