Implementation of Nearby Person on web page [closed] - php

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 5 years ago.
Improve this question
is there anyone knows how to implement Nearby Person on Web page ? Is it Possible ?

An implementation could be something like this:
<?php
$people =
[
'John',
'Adam',
'Terry',
'Gary',
'Wilbur'
];
$get_closest = function ($person) use ($people)
{
$closest = [];
$key = array_search($person, $people);
if($key !== false) {
$before = $key-1;
$after = $key+1;
if(isset($people[$before]))
$closest[] = $people[$before];
if(isset($people[$after]))
$closest[] = $people[$after];
}
return $closest;
};
var_dump($get_closest('Gary'));
Output:
array(2) {
[0]=>
string(5) "Terry"
[1]=>
string(6) "Wilbur"
}

Related

How to return array unique value? 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 3 years ago.
Improve this question
I need an array like this in PHP?
array = [mani , mani, nithi, nithi, basto]
return in
array = [basto]
not for other elements
Does anyone know how to solve this problem?
<?php
$items = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$counts = array_count_values($items);
// Remove elements that occur more than once.
$filtered = array_filter($items, function ($item) use ($counts) {
return $counts[$item] === 1;
});
var_export($filtered);
Output:
array (
4 => 'basto',
)
You may want to specify your question so users here can provide better assistance.
To your issue:
array_unique($array);
https://www.php.net/manual/en/function.array-unique.php
EDIT: you want to search all items by name, to do that you need this function: https://www.php.net/manual/en/function.array-search.php
with your example:
$array = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$basto = array_search('basto', $array);
Best,
Sebo

I can not create PHP array from JSON [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 5 years ago.
Improve this question
I have a complicated JSON and I need create array from this JSON.
I already parsed JSON and create a variablies like this:
$name = $json[response][docs][$i][name][0];
$osm_id = $json[response][docs][$i][osm_id][0];
$place = $json[response][docs][$i][place][0];
$population= $json[response][docs][$i][population][0];
now I need a array, with this variablies, where the $i is changing, like this:
$array = [array_1(name,osm_id,place,population),array_2(name_2,osm_id_2)]
Can you help me with the cycle to fill this array?
If my understanding is correct,
$expected_arr = array();
foreach($json[response][docs] as $inc => $values){
$data = array();
foreach($values as $key => $val){
$data[$key] = $val[0];
}
$expected_arr[$inc] = $data;
}
So you would get something like
array(0 => array( 'name'=>'xxx', 'osm_id'=>'yy',..), 1=> array('name'=>'',.. ,),...)

Can I do something like this 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 2 years ago.
Improve this question
Started learning using CodeIgniter and I am wondering what the best way is to re-do this loop to compare two variables please...
<?php
$user = array('name' => 'name', 'hasCat'=> 1, 'hasDog' => 0);
$pets = array('hasCat', 'hasDog');
foreach($pets as $pet) {
echo ($pet==$user->hasCat) ? 'checked' : '';
the hasCat ^ is the one I want to replace to $pets??
}
?>
You can't access the array value by doing $user->hasCat, that implies hasCat is a variable of the object $user. Try this:
<?php
$user = array('name' => 'name', 'hasCat'=> true, 'hasDog' => false);
$pets = array('hasCat', 'hasDog');
foreach($pets as $pet) {
if($user[$pet]) echo $pet;
}
?>

Create Json from a mysql Result Set Resembling a Tree [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
The problem is to get the following result set:
'org1', 'unit1', 'proj1'
'org1', 'unit1', 'proj2'
'org1', 'unit2', 'proj1'
'org1', 'unit2', 'proj2'
'org2', 'unit1', 'proj1'
'org2', 'unit1', 'proj2'
to the following in php:
[{"units": [{"name": "unit1", "projects": ["project1", "project2"]}, {"name": "unit2", "projects": ["project1", "project2"]}], "name": "org1"}, {"units": [{"name": "unit1", "projects": ["project1", "project2"]}], "name": "org2"}]
Any suggestions?
You can use something like
$final_array = array();
foreach($array as $row){
$final_array[$row[0]][$row[1]][] = $row[2];
}
Now this will "convert the SQL result array into a multidimentional array/tree" but not like what you want yet.
So we shall have to process the array again..
$final_array_2 = array(); // Lets go deep
foreach ($final_array as $name => $units) {
$quarterfinal_array = array(); // Not deep enough
$semi_final_array = array();
foreach ($units as $proj_name => $projects) {
$nano_final_array = array(); // Lets dig deeper ;)
$nano_final_array['name'] = $proj_name;
$nano_final_array['projects'] = $projects;
$semi_final_array[] = $nano_final_array;
}
$quarterfinal_array['units'] = $semi_final_array;
$quarterfinal_array['name'] = $name;
$final_array_2[] = $quarterfinal_array;
}
echo json_encode($final_array_2);
PS: Sorry my choice of variable names are not the most ideal, but they get the work done ;) This is a P.O.C, You can always improve on it.
DEMO

search string in comma separated array 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 9 years ago.
Improve this question
I need to return the array which contains the search string in the range .
Basically i want to search for string with pakistan1 and do not take above that that is it should ignore the array with key 0
second string bangladesh4 it should ignore any other keys below 4
Final return array should be
array(
[2]=>
[3]=>
[4]=>
)
There are two strings which needs to be searched. How I can go about ?
First : pakistan2
Second : bangladesh4
$input_arr= array(
0=>array(india0,srilanka1,pakistan0,banglades0),
1=>array(india1,srilanka1,pakistan1,bangladesh1),
2=>array(india2,srilanka2,pakistan2,bangladesh2),
3=>array(india3,srilanka3,pakistan3,bangladesh3),
4=>array(india 4,srilanka4,pakistan4,bangladesh4),
5=>array(india 5,srilanka5,pakistan5,bangladesh5),
);
I want to return the resulting array as :
$result_arr= array(
2=>array(india2,srilanka2,pakistan2,bangladesh2),
3=>array(india3,srilanka3,pakistan3,bangladesh3),
4=>array(india 4,srilanka4,pakistan4,bangladesh4)
)
EDITED
$first_str = "pakistan2";
$second_str = "bangladesh4";
$arr_output = array();
foreach($input_arr as $key=>$temp_arr)
{
if(in_array($first_str, $temp_arr) || in_array($second_str, $temp_arr) )
{
$arr_output[$key]=$temp_arr;
}
}
Not able to get
array 3
try this
$input_arr= array(
0=>array("india0","srilanka1","pakistan0","banglades0"),
1=>array("india1","srilanka1","pakistan1","bangladesh1"),
2=>array("india2","srilanka2","pakistan2","bangladesh2"),
3=>array("india3","srilanka3","pakistan3","bangladesh3"),
4=>array("india 4","srilanka4","pakistan4","bangladesh4"),
5=>array("india 5","srilanka5","pakistan5","bangladesh5"),
);
$first_str = "pakistan2";
$second_str = "bangladesh4";
$arr_output = array();
foreach($input_arr as $key=>$temp_arr)
{
if(in_array($first_str, $temp_arr) || in_array($second_str, $temp_arr) )
{
$arr_output[$key]=$temp_arr;
}
}
print_r($arr_output);
Try this
$result_arr= array();
for($i=0;$i<sizeof($input_arr);$i++)
{
if(in_array("searchstring",$input_arr[$i]))
if(in_array("searchstring2",$input_arr[$i]))
$result_arr = $input_arr[$i];
}

Categories