Check if a json array value exits on php or not - php

Hi everyone
I´m making a project where i need to check if a 'x' value, which is being defined on a PHP variable, is founded or not inside the JSON steamid array...
Here is a example of how JSON structure looks like:
{
"users": [
{
"steamid": "[X:X:XXXXXXXXX]",
"teamspeakid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"nicknames": [
"XXXXXXXXX"
],
"addresses": [
"XX.XX.XXX.XX"
]
},
{
"steamid": "[Y:Y:YYYYYYYYY]",
"teamspeakid": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"nicknames": [
"YYYYYYYYY"
],
"addresses": [
"YY.YY.YYY.YY"
]
},
{
"steamid": "[Z:Z:ZZZZZZZZZ]",
"teamspeakid": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
"nicknames": [
"ZZZZZZZZZ"
],
"addresses": [
"ZZ.ZZ.ZZZ.ZZ"
]
}
]}
The PHP function structure that I have on my mind is similar to this:
if (x value is founded on json->users steamid array) {
$verified = 1;
$teamspeakid = the founded steamid->teamspeakid;
$nicknames = the founded steamid->nicknames;
$addresses = the founded steamid->addresses;
} elseif (steamid value is not founded on json array) {
$verified = 0;
}
I hope you can help me :-)

You need to decode the json array then use strpos to check the string or character exists or not.Do like below:
$jsonarr='{
"users": [
{
"steamid": "[X:X:XXXXXXXXX]",
"teamspeakid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"nicknames": [
"XXXXXXXXX"
],
"addresses": [
"XX.XX.XXX.XX"
]
},
{
"steamid": "[Y:Y:YYYYYYYYY]",
"teamspeakid": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"nicknames": [
"YYYYYYYYY"
],
"addresses": [
"YY.YY.YYY.YY"
]
},
{
"steamid": "[Z:Z:ZZZZZZZZZ]",
"teamspeakid": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
"nicknames": [
"ZZZZZZZZZ"
],
"addresses": [
"ZZ.ZZ.ZZZ.ZZ"
]
}
]}';
$arr=json_decode($jsonarr,true);
$searchstring='X';
foreach($arr['users'] as $key=>$value){
if (strpos($value['steamid'], $searchstring) !== false) {
$verified = 1;
}else{
$verified = 0;
}
}
or
if(array_search($searchstring, array_column($arr['users'], 'steamid')!== false) {
$verified = 1;
}else{
$verified = 0;
}

Related

ORM: List of related items

I tried to use ORM to find list of provinces and their cities.
the result is same as this list:
"provinces": [
{
"name": "Prov.1",
"Cities": {
"name": "City.1"
}
},
{
"name": "Prov.1",
"Cities": {
"name": "City.2"
}
},
]
what is expected:
"provinces": [
{
"name": "Prov.1",
"Cities": [
{
"name": "City.1"
},
{
"name": "City.2"
}
]
},
]
used ORM:
Provinces::find('all', [
'with' => ['Cities']
]);
Something like
$array = [];
foreach($provinces as $province){
$key = $province['name'];
if(!isset($array[$key])){
$array[$key] = [
'name' => $province['name'],
'Cities' => [
[ 'name' => $province['Cities']['name']];
]
];
}else{
$array[$key]['Cities'][] = ['name' => $province['Cities']['name']];
}
}
//strip keys
$array = array_values($array);
Because you don't have an actual PHP array posted I'm just guessing at what it should be.

I wanted to check login users follow the search user or not

i want to check login users, follow the search user or not, if follow, true if not,false show in output i need help php cod
MY CODE IS
$sql="SELECT * FROM `users` where user_id='$userid' AND session_id='$session_id'";
$result=mysqli_query($conn,$sql);
if($result->num_rows>0)
{
//user_array
$user_query="SELECT * FROM users";
$user_query_result=mysqli_query($conn,$user_query);
if($user_query_result->num_rows>0)
{
$user_array=array();
while ($row_users = $user_query_result->fetch_assoc())
{
$user_id=$row_users['user_id'];
$user_name=$row_users['fast_name']." ".$row_users['last_name'];
$user_pic_path=$row_users['user_pic_path'];
$follower=array();
$followers=array();
$followed_query="SELECT * FROM followers where follower_id=".$user_id;
$followed_result=mysqli_query($conn,$followed_query);
while($followed_row=$followed_result->fetch_assoc())
{
$follower=$followed_row['user_id'];
if($follower==$user_id)
{
$followers=TRUE;
}
//array_push($followers,$followed_row['user_id']);
}
$images=array();
$images_query="SELECT * FROM image where user_id=".$user_id;
$image_result=mysqli_query($conn,$images_query);
while($image_row=$image_result->fetch_assoc())
{
$images[]=$image_row['image_path'];
}
$user[] = array(
/*"user_id"=>$user_id,*/
"user_name"=>$user_name,
"user_pic_path"=>$user_pic_path,
"follower"=>$followers,
"images"=>$images
);
//add in user_array
array_push($user_array,$user);
}
$response['session']=TRUE;
$response['status']=TRUE;
$response['msg']="user data find successfully";
$response['user_detail']=$user;
echo json_encode($response);
}
MY OUTPUT IS
// 20180104175035
// http://localhost/1111.php?userid=2&session_id=953455543
{
"session": true,
"status": true,
"msg": "user data find successfully",
"user_detail": [
{
"user_name": "asad ali",
"user_pic_path": "localhost/uploads/image/9152108abc",
"follower": [
],
"images": [
"localhost/uploads/image/7947861Discover All In One.png"
]
},
{
"user_name": "asim kabeer",
"user_pic_path": "localhost/uploads/image/1952108xyz",
"follower": [
],
"images": [
"localhost/uploads/image/1787860Discover All In One.png",
"localhost/uploads/image/2152108Mart Zone - All in one.png"
]
},
{
"user_name": "saad hussain",
"user_pic_path": "localhost/uploads/image/5152108ytp",
"follower": [
],
"images": [
"localhost/uploads/image/1474008Mart Zone - All in one.png",
"localhost/uploads/image/6547860Discover All In One.png"
]
},
{
"user_name": "nazeer hussain",
"user_pic_path": "localhost/uploads/image/7352108trp",
"follower": [
],
"images": [
"localhost/uploads/image/1547860Discover All In One.png"
]
},
{
"user_name": "asad zahoor",
"user_pic_path": "localhost/images/ftftft",
"follower": [
],
"images": [
"localhost/uploads/image/1081552Mart Zone - All in one.png"
]
},
{
"user_name": "ali hussain",
"user_pic_path": "localhost/images/sduhsud.png",
"follower": [
],
"images": [
"localhost/uploads/image/5547860Discover All In One.png"
]
}
]
why follower does not show the value i want true or false if follow then show true my follower table schema are following
user_id=>1,2,3,4,5,6
follower_user_id=>2,3,5,3,4,1
I'm almost sure, that below condition is failing
if($follower==$user_id)
{
I'm not sure about the data, so I can't comment much. But, I would suggest to debug your SQL one more time and check, if you are getting expected output from SQL
$followed_query="SELECT * FROM followers where follower_id=".$user_id;
$followed_result=mysqli_query($conn,$followed_query);
while($followed_row=$followed_result->fetch_assoc())
{
$follower=$followed_row['user_id'];
if($follower==$user_id)
Side note : - why can't you simply use follower_id=user_id in SQL ?
$followers=array();
if(1==1)
{
$followers=TRUE;
}
$user[] = array("follower"=>$followers,);
print_r ($user);
Output :
[0] => Array
(
[follower] => 1
)
Whereas, below case return empty array, which is actually your case
if(1==2)
{
$followers=TRUE;
}
$user[] = array("follower"=>$followers,);
print_r ($user);
Output :
[0] => Array
(
)
while ($row_followed = $result_followed->fetch_assoc())
{
//array_push($followers,$row_followed['user_id']);
$age=array($userid=>$row_followed['follower_id']);
foreach($age as $x => $value)
{
if($x==$value)
{
$followers=TRUE;
}
elseif($x!==$value)
{
$followers=FALSE;
}
}
}

'Array' with php file

I'm having a problem with this php file. When nicknames or addresses are values that i want to display, it appears a error called Array. I want to be able to return all user information on php variables.
<?php
$str = '{
"users": [
{
"steamid": "[X:X:XXXXXXXXX]",
"teamspeakid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"nicknames": [
"XXXXXXXXX"
],
"addresses": [
"XX.XX.XXX.XX"
]
},
{
"steamid": "[Y:Y:YYYYYYYYY]",
"teamspeakid": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"nicknames": [
"YYYYYYYYY"
],
"addresses": [
"YY.YY.YYY.YY"
]
},
{
"steamid": "[Z:Z:ZZZZZZZZZ]",
"teamspeakid": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
"nicknames": [
"ZZZZZZZZZ"
],
"addresses": [
"ZZ.ZZ.ZZZ.ZZ"
]
}
]
}';
$json = json_decode($str);
foreach($json->users as $item)
{
if($item->steamid == "[X:X:XXXXXXXXX]")
{
echo $item->addresses;
}
}
?>
addresses is array by one item.
Edit to:
foreach($json->users as $key=>$item)
{
if($item->steamid == "[X:X:XXXXXXXXX]")
{
echo $item->addresses[0];
}
}

Build Json from database with php

I've written a simple PHP file, which pull my Data from the Database and return it with $row["fieldname"]. So now i need to know how i return is as json like below.
Normally i would start it with creating a array
$arr = [
'Name' => $row["_LSMOUSER"],
'Pass' => $row["_LSMOPASSWORT"] ];
And would encode it with
json_encode($arr, JSON_PRETTY_PRINT);
But, how i can add the Points "Rights, Masks & Key like below?
{
"Users": [
{
"Name": "user",
"Pass": "passwort",
"Rights": {
"Masks": [
{ "Key": "FAQ", "Access": true },
{ "Key": "Hilfebereich", "Access": false }
]
}
},
{
"Name": "user2",
"Pass": "passwort2",
"Rights": {
"Masks": [
{ "Key": "FAQ", "Access": true },
{ "Key": "Hilfebereich", "Access": true }
]
}
}
]
}
Something like this:
$arr = array(
'Name' => $row["_LSMOUSER"],
'Pass' => $row["_LSMOPASSWORT"],
'Rights'=>array(
'Masks'=>array( array('Key'=>'FAQ', 'Access'=>true),
array('Key'=>'Hilfebereich', 'Access'=>false)
)
)
);
// OutPut
"Name": "user",
"Pass": "passwort",
"Rights": {
"Masks": [
{ "Key": "FAQ", "Access": true },
{ "Key": "Hilfebereich", "Access": false }
]
}
then json_encode($arr)

Parse Huge GeoJSON file and get polygon coordinates of a specific property

How to get the polygon coordinates of a specific property. It's very a huge file so the time to parse the file is a factor.
Is there a library to do that?
Sample of the geojson:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::37001" } },
"features": [
{ "type": "Feature", "properties": { "HOOD_ID": 2799.000000, "HOOD_NAME": "Overtown", "MARKET_ID": "MK1245000", "MARKET": "Miami", "STATE": "12", "STATENAME": "Florida", "LATITUDE": 25.784659, "LONGITUDE": -80.202625, "AREA": 1.495920, "HLEVEL": 2.000000, "DATE_ADDED": "2012\/08\/04", "FLAG1": 0, "OB_GEO_ID": "NH2799" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -80.21463341110001, 25.782154451300002 ], [ -80.21588353300001, 25.782696872700001 ], [ -80.217973576800006, 25.7833078056 ], [ -80.219539583200003, 25.784199528800002 ], [ -80.211531118000011, 25.787386122500003 ], [ -80.20836940560001, 25.789128957700001 ], [ -80.206422272200001, 25.789848709300003 ], [ -80.2060101207, 25.7907922853 ], [ -80.206013661300005, 25.793566844899999 ], [ -80.206013794, 25.7968569831 ], [ -80.202368489099996, 25.796952708299997 ], [ -80.202379, 25.797313 ], [ -80.199836, 25.797309 ], [ -80.199819759600004, 25.7970196375 ], [ -80.1993398571, 25.797032239699998 ], [ -80.193583490500004, 25.797234161599999 ], [ -80.193806159800005, 25.796203267299997 ], [ -80.194272724399994, 25.7951752727 ], [ -80.193944, 25.795182 ], [ -80.194266, 25.793434 ], [ -80.195336, 25.789592 ], [ -80.195534, 25.787847 ], [ -80.195514, 25.778409 ], [ -80.195969425200005, 25.778397321299998 ], [ -80.19557104899999, 25.773179598799999 ], [ -80.195360063199999, 25.768486166300001 ], [ -80.196768768399991, 25.7682545324 ], [ -80.198226099099998, 25.768721241800002 ], [ -80.199164023899996, 25.769800189500003 ], [ -80.199997701599997, 25.770738292499999 ], [ -80.200414826200003, 25.772286616100001 ], [ -80.200936435800003, 25.773272690900001 ], [ -80.202343232900006, 25.7749143389 ], [ -80.204375245, 25.776884093299998 ], [ -80.205990323199998, 25.777259031 ], [ -80.206835373600001, 25.777897973199998 ], [ -80.207587, 25.777601 ], [ -80.210881, 25.78 ], [ -80.21463341110001, 25.782154451300002 ] ] ] } },
{ "type": "Feature", "properties": { "HOOD_ID": 2169.000000, "HOOD_NAME": "Church District", "MARKET_ID": "MK1235000", "MARKET": "Jacksonville", "STATE": "12", "STATENAME": "Florida", "LATITUDE": 30.332174, "LONGITUDE": -81.660212, "AREA": 0.131745, "HLEVEL": 1.000000, "DATE_ADDED": "2012\/08\/04", "FLAG1": 0, "OB_GEO_ID": "NH2169" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -81.664799, 30.331204 ], [ -81.663868, 30.334826 ], [ -81.655617, 30.333239 ], [ -81.656717, 30.329439 ], [ -81.664799, 30.331204 ] ] ] } }
}
Large files can best be parsed using an event-based JSON parser (here I use one by kuma-giyomu). The idea is to use callbacks when a certain token is encountered, so that the processing of the data can be done in between parsing.
In the following code, the property "coordinates" is used to trigger the creation of a new polygon object, and then the start function of the array handler to start a new coordinate array and it is submitted to the polygon object when an array end token is encountered.
<?php
include "JSONParser.php";
class Polygon {
public $coordinates = array();
}
$coords = null;
$polygons = array();
$polygon = null;
$j = new JSONParser();
$j->setPropertyHandler(function($value, $property) {
global $polygons, $polygon;
if ($value != "coordinates") {
if (!is_null($polygon)) {
$polygons[] = $polygon;
$polygon = null;
}
return;
}
if (is_null($polygon)) {
$polygon = new Polygon;
}
});
$j->setArrayHandlers(function($value, $property) {
global $coords, $polygon;
if (!is_null($polygon)) {
$coords = array();
}
}, function($value, $property) {
global $coords, $polygon;
if (!is_null($coords)) {
if (!is_null($polygon)) {
$polygon->coordinates[] = $coords;
}
$coords = null;
}
});
$j->setScalarHandler(function($value, $property) {
global $coords;
if (!is_null($coords)) {
$coords[] = $value;
}
});
try {
$j->parseDocument("test.json");
} catch (JSONParserException $e) {
}
if (!is_null($polygon)) {
$polygons[] = $polygon;
$polygon = null;
}
print_r($polygons);
outputs
Array
(
[0] => Polygon Object
(
[coordinates] => Array
(
[0] => Array
(
[0] => -80.21463341110001
[1] => 25.782154451300002
)
[1] => Array
(
[0] => -80.21588353300001
[1] => 25.782696872700001
)
[...]

Categories