i want get all objects with owner = true
i have made code that get specific data from json array
so when i print id to find it give me the last one i want it print all owner value = true
i did try foreach but give me error
so any one can help me
{
"owner":true,
"number":2146958847,
"ser":"333",
"id":"505417057861500987",
"name":" test"
},
{
"owner":true,
"number":2146958847,
"ser":null,
"id":"506154122521608196",
"name":"test1"
}
]
my code
<?php $o1 = json_encode($_SESSION['guilds']);
$arr = json_decode($o1,true);
$username = array();
$len = count($arr);
for($i = 0; $i < $len; $i++){
$id = $arr[$i]["id"];
$owner = $arr[$i]["owner"];
$isowner[$owner] = $id;
}
$thisisowner = true;
if(isset($isowner[$thisisowner]) ){
$idToFind = $isowner[$thisisowner];
echo $idToFind;
}else{
echo "this owner doesn't exist";
}
?>
output for this code = 505417057861500987
i want to be like this =
id: 505417057861500987 - Number : 2146958847 | name : test1 ,id : 506154122521608196 | Number : 2146958847| name : test
not one
Related
Ok so I have a log file that contains info like below
7:04:49 | Player "PLAYER A"(id=BC843EF4A109D1FE413E69FCCD789A2044224192) has been
disconnected
07:05:03 | Player "PLAYER B" is connected (id=0EFCEFBB9F30A04B0BAA08E97766C368F9652642)
07:05:27 | ##### PlayerList log: 9 players
07:05:27 | Player "PLAYER B" (id=72C79B428E4A6DA8E1A915A20BEC65A3B993B072 pos=<2046.0, 14926, 439.1>)
Now I am searching the log for the player is connected lines
This works and heres the code
$coordpattern = '/^(.*?)Player (.*?) is connected/m';
if(preg_match_all($coordpattern, $file, $items))
{
$logins = array();
$i = 0;
foreach($items[$i] as $item=>$val) {
preg_match('/"([^"]+)"/',$val,$player);
$time = substr($val,0,8);
$offset = strpos($file,$val);
//echo "<pre>";
//echo $time.' - '.$player[0];
//echo "</pre>";
$logins[] = array("Time"=>$time,"Player"=>$player[0],"offset"=>$offset);
$i++;
}
}
This is then building an array containing the Time, Player Name and strpos of the start of the line ( I assume )
What I want to then do is to search the log for any lines after the player is connected
that match the player name.
I'm trying to check the time from the start of the line and if its greater than the time in the logins array then just echo "found"
However its just not returning anything..
Heres my code
foreach($logins as $playername){
//echo $playername["Player"].'<br>';
//var_dump($logins);
//echo $playername[$l]['Player']."<br>";
$player = str_replace('"','',$playername["Player"]);
$logintime = $playername["Time"];
$pospattern = '/^(.*?)Player \"'.$player.'\" \(id=(.*?) pos=<(.*?),(.*?),(.*?)\)/';
if(preg_match_all($pospattern, $file, $positions))
{
$s = 0;
foreach($positions[$s] as $pos=>$val) {
$login = intval(str_replace(":","",$logintime));
preg_match('/"([^"]+)"/',$val,$player);
$time = substr($val,0,8);
$actualtime = intval(str_replace(":","",$time));
if($actualtime > $login)
{
echo "found";
}
$s++;
}
}
}
Your code is not working, I've rewritten it here:
<?php
$file = <<<FILE
7:04:49 | Player "PLAYER A"(id=BC843EF4A109D1FE413E69FCCD789A2044224192) has been
disconnected
07:05:03 | Player "PLAYER B" is connected (id=0EFCEFBB9F30A04B0BAA08E97766C368F9652642)
07:05:27 | ##### PlayerList log: 9 players
07:05:27 | Player "PLAYER B" (id=72C79B428E4A6DA8E1A915A20BEC65A3B993B072 pos=<2046.0, 14926, 439.1>)
FILE;
$coordpattern = '/^(.*?)Player (.*?) is connected/m';
if(preg_match_all($coordpattern, $file, $items))
{
$logins = array();
$i = 0;
foreach($items[$i] as $item=>$val) {
preg_match('/"([^"]+)"/',$val,$player);
$time = substr($val,0,8);
$offset = strpos($file,$val);
//echo "<pre>";
//echo $time.' - '.$player[0];
//echo "</pre>";
$logins[] = array("Time"=>$time,"Player"=>$player[0],"offset"=>$offset);
$i++;
}
}
foreach($logins as $playerData){
$playername = $playerData["Player"];
$logintime = $playerData["Time"];
$pospattern = '/(.*?)Player '.$playername.' \(id=(.*?) pos=<(.*?),(.*?),(.*?)\)/';
if(preg_match_all($pospattern, $file, $positions))
{
$s = 0;
foreach($positions[$s] as $pos=>$val) {
$login = intval(str_replace(":","",$logintime));
preg_match('/"([^"]+)"/',$val,$player);
$time = substr($val,0,8);
$actualtime = intval(str_replace(":","",$time));
if($actualtime > $login) {
echo "found";
}
$s++;
}
}
}
There were two issues. Logins is an array of arrays. The foreach in your original post has it as $logins[1] which will never work.
Then, when you're assigning the player name with $logins[] = array("Time"=>$time,"Player"=>$player[0],"offset"=>$offset);, "Player" actually contains the double quotes as well. So your regex was like:
"/(.*?)Player ""PLAYER B"" \(id=(.*?) pos=<(.*?),(.*?),(.*?)\)/"
There is no entry for ""PLAYER B"". Removing the extra double quote from your $posplayer would fix it.
Try to debug the regex pattern next time. Whenever you extract something with regex and use it again, always dump the concatenation result.
Basically I want to balance an array.
The array I want to balance is retrieved from DB and can be unknown
Here is sample array retrieved from DB, it can vary
<?php
$arr_from_DB = array(
'1'=>NULL,
'2'=>1,
'3'=>1,
);
$node_width_from_DB = 3;
$node_depth_from_DB = 2;
?>
//Max Capacity was caclulated from $node_width_from_DB, $node_depth_from_DB AND WAS ALSO SAVED in DB , simulated below for width = 3 , depth = 2
$max_cap_of_array = 13;
//With $arr_from_DB fed to the display fxn on frontend, this is the result
//What I want to achieve, I want to transform $arr_from_DB to below ARray, All Array index with negative is seen to be dynamically generated
<?php
$formated_arr = array(
'1'=>NULL,
'2'=>1,
'3'=>1,
'-1'=>1,
'-2'=>2,
'-3'=>2,
'-4'=>2,
'-5'=>3,
'-6'=>3,
'-7'=>3,
'-8'=>-1,
'-9'=>-1,
'-10'=>-1,
);
?>
//With $formated_arr fed to the display fxn on frontend, this is the result
WHAT I TRIED SO FAR
<?php
$arr_filled = RECfillMissingDownlineArr($arr_from_DB);
var_dump($arr_filled);
function RECfillMissingDownlineArr($selectedArr, $pointer=1){
$node_width_from_DB = 3;
$node_depth_from_DB = 2;
//Max Capacity was caclulated from $node_width_from_DB, $node_depth_from_DB AND WAS ALSO SAVED in DB , simulated below for width = 3 , depth = 2
$max_cap_of_array = 13;
//var_dump($Allarr);
if($pointer>= $max_cap_of_array){
return $selectedArr;
}
$array_worked = array_count_values($selectedArr);
$array_worked3 = array_keys($selectedArr);
$array_worked2 = array_values($array_worked3);
//$key_p = $array_worked3[$pointer];
foreach($array_worked as $key=>$value){
//echo 'Value nm'.$value.'<br>';
if($value<$node_width_from_DB){
$key_p = $key;
break;
}
}
$array_worked1 = array_keys($selectedArr, $key_p);
for ($i=0; $i <$width ; $i++) {
if(empty($array_worked1[$i])){
$key_pointer = $pointer * -1;
$selectedArr[$key_pointer] = $key;
$pointer++;
return RECfillMissingDownlineArr($selectedArr,$pointer);
}
}
}
OUTPUT NULL, It's not what I want, it's returning null alltogether
?>
I am using the following PHP code to fetch some data from my database. It contains chats and messages between users in those chats. I want to return the information of both users plus the messages they exchanged. My test data has two chats with ID's 1 and 2. There are two messages, both in chat 1, however for some reason they are returned for both chats 1 and 2. I'm not sure what the problem in my code is.
$response = array();
$myArray = array();
while($row = $user_chats->fetch_array())
{
$myArray["chatId"] = $row["chat_id"];
$myArray["user1_id"] = $row["user1"];
$myArray["user2_id"] = $row["user2"];
$myArray["user1_name"] = $user1_name;
$myArray["user2_name"] = $user2_name;
$myArray["user1_profile_pic"] = $result_user1["profile_pic"];
$myArray["user2_profile_pic"] = $result_user2["profile_pic"];
$messages = array();
$chat_idd = $row["chat_id"];
$chat_messages = mysqli_query($conn,"SELECT * FROM messages WHERE chatID = '$chat_idd' ORDER BY timestamp ASC");
$count = 1;
while($roww = $chat_messages->fetch_array()) {
if ($row["chat_id"] == $roww["chatID"]) {
$messages["message_id"] = $roww["message_id"];
$messages["sender"] = $roww["sender"];
$messages["chatId"] = $roww["chatID"];
$messages["text"] = $roww["text"];
$messages["timestamp"] = $roww["timestamp"];
$myArray["message"][$count] = $messages;
$count = $count + 1;
}
else {
$myArray["message"]= 0;
}
}
$response[] = $myArray;
}
echo json_encode($response);
produces the following response:
[{"chatId":"1","user1_id":"32132132","user2_id":"2121","user1_name":"dwqd",
"user2_name":"dqdwdw","user1_profile_pic":"http:\/\/graph.facebook.com\/dwqwqdqdwdw\/picture?type=large","user2_profile_pic":"WDQdwqwqddqwdqwdq","message":{"1":{"message_id":"24242241","sender":"32132132","chatId":"1","text":"hello i am",
"timestamp":"2016-05-24 17:13:08"},"2":{"message_id":"421421","sender":"32132132",
"chatId":"1","text":"great","timestamp":"2016-05-24 17:15:08"}}},{"chatId":"2","user1_id":"23413524635","user2_id":"32132132","user1_name":false,
"user2_name":"dwqd","user1_profile_pic":
WDQdwqwqddqwdqwdq" ,"user2_profile_pic":"http:\/\/graph.facebook.com\/32132132\/picture?type=large",
"message":{"1":{"message_id":"24242241","sender":"32132132","chatId":"1","text":"hello i am",
"timestamp":"2016-05-24 17:13:08"},"2":{"message_id":"421421","sender":"32132132","chatId":"1",
"text":"great","timestamp":"2016-05-24 17:15:08"}}}]
You need to initialize $myArray at each iteration through the loop, e.g.
while($row = $user_chats->fetch_array()) {
$myArray = array();
I am trying to include Elastic search into my application. In a scenario a user can select field type and enter the searchable value.
Let say, Search using First Name and Value is 'ABC'.
I have tried this using bool query,but How i can use 'more_like_this' query method?.
This what I have done so far.
$searchParams['index'] = 'articles';
$searchParams['type'] = 'article';
$searchParams['body']['query']['match']['body'] = 'ABC';
$queryResponse = $es->search($searchParams);
but if i put more_like_this into the query field,data cannot be selected.
any help please?
Thanks in advance
This is the edited code
$searchParams['index'] = 'articles';
$searchParams['type'] = 'article';
$searchParams['body']['query']['more_like_this']['fields'] = array('body','title');
$searchParams['body']['query']['more_like_this']['like_text'] = $q;
$searchParams['body']['query']['more_like_this']['min_term_freq'] = 1;
$searchParams['body']['query']['more_like_this']['percent_terms_to_match'] = 1;
$searchParams['body']['query']['more_like_this']['min_doc_freq'] = 1;
Can you tell me what this lines for?
$searchParams['body']['query']['more_like_this']['min_term_freq'] = 1;
$searchParams['body']['query']['more_like_this']['percent_terms_to_match'] = 1;
$searchParams['body']['query']['more_like_this']['min_doc_freq'] = 1;
You need to do an array, that will look like a json to ES:
{
"query": {
"more_like_this": {
"fields": [
"text"
],
"like_text": "apple",
"min_term_freq": 1,
"percent_terms_to_match": 1,
"min_doc_freq": 1
}
}
}
So in PHP it will be:
$searchParams['body']['query']['more_like_this']['fields'] = array('body');
$searchParams['body']['query']['more_like_this']['like_text'] = 'apple';
$searchParams['body']['query']['more_like_this']['min_term_freq'] = 1;
$searchParams['body']['query']['more_like_this']['percent_terms_to_match'] = 1;
$searchParams['body']['query']['more_like_this']['min_doc_freq'] = 1;
I have a PHP MySQL fetch while loop as shown below in my script:
$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
$sn = $row2211['sn'];
$allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
}
$prodObj2 = new ProductDetails();
$prodObj2->productname = $row2211['productname'];
$prodObj2->price = $row2211['productprice'];
$prodObj2->discount = $row2211['discount'];
$prodObj2->discountprice = $row2211['discountprice'];
$prodObj2->imageURL = $row2211['productimageurl'];
$prodObj2->category = $row2211['productcat'];
$prodObj2->configurablepone = $subproa;
$prodObj2->configurable = 'yes';
array_push($totArr, $prodObj2);
}
In that I have a problem. I get the result as like below (JSON):
[
{
"productname":"Veg.Pizaa",
"price":"350",
"discount":"",
"discountprice":"350",
"imageURL":"http:\/\/farm8.staticflickr.com\/7154\/6694188161_9ee692d854_s.jpg",
"category":"Pizaa",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
"configurable":"yes"
},
{
"productname":"Core i7 Pc",
"price":"48000",
"discount":"2",
"discountprice":"47040",
"imageURL":"http:\/\/www.4to40.com\/images\/science\/Basic_Computer_Parts\/Computer.jpg",
"category":"Pc",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"},{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
"configurable":"yes"
}
]
But I need the result as like below:
[
{
"productname":"Veg.Pizaa",
"price":"350",
"discount":"",
"discountprice":"350",
"imageURL":"http:\/\/farm8.staticflickr.com\/7154\/6694188161_9ee692d854_s.jpg",
"category":"Pizaa",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
"configurable":"yes"
},
{
"productname":"Core i7 Pc",
"price":"48000",
"discount":"2",
"discountprice":"47040",
"imageURL":"http:\/\/www.4to40.com\/images\/science\/Basic_Computer_Parts\/Computer.jpg",
"category":"Pc",
"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
"configurable":"yes"
}
]
As you can see configurablepone JSON is getting repeated for every loop so I am getting the value of the first product in second product also but I need to seperate like below:
First Product As Like Below
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}]
Second Product As Like Below
"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}]
I have tried changing the loop but I haven't found any solutions. Kindly help me to solve this.
I think, your problem in line $subpro1[] = substr($subpro, 0, -1)."}";.
So, first call of this line save data from "Veg.Pizaa" into $subpro1[0].
Second call of this line save data from "Core i7 Pc" into $subpro1[1].
Then, line $subproa = "[".implode(",",$subpro1)."]"; merged all array elements.
Just Use unset($subpro1); after the array_push($totArr, $prodObj2);.
Below Is An example Source
Try This One This May Work
$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
$sn = $row2211['sn'];
$allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
}
$prodObj2 = new ProductDetails();
$prodObj2->productname = $row2211['productname'];
$prodObj2->price = $row2211['productprice'];
$prodObj2->discount = $row2211['discount'];
$prodObj2->discountprice = $row2211['discountprice'];
$prodObj2->imageURL = $row2211['productimageurl'];
$prodObj2->category = $row2211['productcat'];
$prodObj2->configurablepone = $subproa;
$prodObj2->configurable = 'yes';
array_push($totArr, $prodObj2);
unset($subpro1);
}
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
$subpro1 = array();
}
try this in second while loop