'Array' with php file - php

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];
}
}

Related

not able to get array in the correct shape

I am trying to create an elasticsearch query and for that I need to get my array in a particular shape but I am not able to get it.
desired output
"query": {
"bool": {
"must": {
"bool": {
"must": [
"match": {
"active": 1
},
"bool": {
"should": [
{
"match": {
"workspace_type.keyword": "lounge"
}
},
{
"match": {
"workspace_type.keyword": " cafe"
}
}
]
}
]
}
}
}
}
My source code
$query = [
'query' => [
'bool' => [
'must' => [
'bool' => [
'must' => []
]
]
]
]
];
$active_query = [
'match' => [
"active" => 1
]
];
$query['query']['bool']['must']['bool']['must'][] = $active_query;
$workspace_type_query = [
'match' => [
"workspace_type.keyword" => "lounge"
]
];
$workspace_type_object = (object) $workspace_type_query;
$query['query']['bool']['must']['bool']['must']['bool']['should'][] = $workspace_type_object;
only issue with this is I am not able to get must[] (the one above match and active ) and it was either coming as an indexed object like
{
"0": {
"match": {
"active": 1
}
}
}
or just an object like this
{
"match": {
"active": 1
}
}
but both these cases are not acceptable for the elasticsearch query that I am trying to generate .. any assistance would be highly appreciated.
I think the extra index that you create for must is wrong.
When you change it to
$query['query']['bool']['must']['bool']['must'] = $active_query;
it seems to work.
The result is
{
"query": {
"bool": {
"must": {
"bool": {
"must": {
"match": {
"active": 1
}
}
}
}
}
}
}

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

Check if a json array value exits on php or not

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

Change the JSON format

I am working with drupal 8. I am trying to get the JSON of all nodes of the content type. I got a json as given bellow. But Now I want to change the Following JSON to
[
{
"nid": [
{
"value": "17"
}
],
"uuid": [
{
"value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
}
],
"vid": [
{
"value": "17"
}
],
"type": [
{
"target_id": "resume_creator"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "uyi"
}
],
"uid": [
{
"target_id": "1"
}
],
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1452060690"
}
],
"changed": [
{
"value": "1452060709"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "0"
}
],
"revision_timestamp": [
{
"value": "1452060709"
}
],
"revision_uid": [
{
"target_id": "1"
}
],
"revision_log": [],
"revision_translation_affected": [
{
"value": "1"
}
],
"default_langcode": [
{
"value": "1"
}
],
"path": [],
"field_communication_address": [
{
"value": "rtyrtytr\r\nuu;\r\nsdgfdh"
}
],
"field_education": [
{
"value": "ytutyuii"
}
],
"field_emails": [
{
"value": "gtf#fgfg.com"
}
],
"field_experiece": [
{
"value": "fghtutyu"
}
],
"field_name": [
{
"value": "ytt"
}
]
}
]
to a format of
[
{
"nid":"17",
"uuid":"3614e0c8-88d4-4e8d-a732-5089698556d5",
"vid": "17",
"type":"resume_creator",
"langcode":"en",
"title":"uyi",
"uid":"1",
"status":"1",
"created":"1452060690",
"changed":"1452060709",
"promote":"1",
"sticky":"0",
"revision_timestamp":"1452060709",
"revision_uid":"1",
"revision_log": [],
"path":[],
"field_communication_address":"rtyrtytr\r\nuu;\r\nsdgfdh",
"field_education":"ytutyuii",
"field_emails":"gtf#fgfg.com",
"field_experiece":"fghtutyu",
"field_name":"ytt"
}
]
using php. Then only I can manage a form angular js. Thanks in advance
Try this
$json = '{
"nid": [
{
"value": "17"
}
],
"uuid": [
{
"value": "3614e0c8-88d4-4e8d-a732-5089698556d5"
}
],
"vid": [
{
"value": "17"
}
],
"type": [
{
"target_id": "resume_creator"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "uyi"
}
],
"uid": [
{
"target_id": "1"
}
],
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1452060690"
}
],
"changed": [
{
"value": "1452060709"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "0"
}
],
"revision_timestamp": [
{
"value": "1452060709"
}
],
"revision_uid": [
{
"target_id": "1"
}
],
"revision_log": [],
"revision_translation_affected": [
{
"value": "1"
}
],
"default_langcode": [
{
"value": "1"
}
],
"path": [],
"field_communication_address": [
{
"value": "rtyrtytr\r\nuu;\r\nsdgfdh"
}
],
"field_education": [
{
"value": "ytutyuii"
}
],
"field_emails": [
{
"value": "gtf#fgfg.com"
}
],
"field_experiece": [
{
"value": "fghtutyu"
}
],
"field_name": [
{
"value": "ytt"
}
]
}';
$json = json_decode($json,true);
foreach ($json as $key => $value){
if(isset($json[$key][0]['value'])){
$json[$key] = $json[$key][0]['value'];
}
if(isset($json[$key][0]['target_id'])){
$json[$key] = $json[$key][0]['target_id'];
}
// $json[$key] = $json[$key][0]['value'];
}
$json = json_encode($json);
print_r($json);
It is simple.
<?php
$arr = array('nid' => 17, 'uuid' => '3614e0c8-88d4-4e8d-a732-5089698556d5', ...);
echo json_encode($arr);
?>
If you have some misunderstanding, ask me.

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)

Categories