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];
}
Related
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
Need a sanity check on this one, I think I just confused myself. I am trying to loop throught the below array and get the fields back for multiple people that are register.
{
[12]=>
array(79) {
["Event Number"]=> int(466226)
["Event Info"]=> string(134) “Event ABC”
["Event Acct Code"]=> NULL
["email"] => string(12)"email#email.com
}
}
How can i best accomplish this?
This is what I tried
$associativeEventInfo=[];
foreach ($res as $eventInfo)
{
$associativeEventInfo[]=$eventInfo;
}
var dumping $associativeEventInfo returns the results
Id like to get each field out according to the array, for example
["EvenNumber"] =466226 so that I can then pass that over to database and do stuff.
So the goal is to remap the data to presumably database fields. First off, accessing the fields is quite easy:
// Assuming $returnedData is an array of assoc array containing the data;
foreach ($returnedData as $eventData)
{
$eventNumber = $eventData['Event Number'];
$eventInfo = $eventData['Event Info'];
// And so on
}
Remapping it is as simple as creating a new array with the correct format (one can also use array_map but I leave that as a readers exercise):
$remappedData = [];
// Assuming $returnedData is an array of assoc array containing the data;
foreach ($returnedData as $eventData)
{
$remappedData[] = [
'eventNumber' => $eventData['Event Number'],
'eventInfo' => $eventData['Event Info'],
// And so on
];
$eventNumber = $eventData['Event Number'];
$eventInfo = $eventData['Event Info'];
}
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
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I wanted to output a 3 dimensional array that will come from the Database. This is my database looks like:
Basically, I wanted my first array will be the header_name and under the header_name the sub_header_name will come then underneath is the name
eg:
User Role Management => array(
'' => array (
'Create User Role'
)
),
Config Management => array(
'Organisation' => array('Create Country','Create
Organisation'),
'Site' => array('Create Site','Edit Site')
)
and here are my codes:
$getAllPermission = Permission::get();
$arrHeader = array();
$arrSubHeader = array();
$arrPermissions = array();
// $x = 0;
foreach($getAllPermission as $value){
$title = $value->header_name;
$sub_header_name = $value->sub_header_name;
$permission_name = $value->name;
if ($sub_header_name == ""){
$sub_header_name = 0;
}
array_push($arrPermissions,$permission_name);
$arrHeader[$title] = array($sub_header_name => array($arrPermissions));
//$x++;
}
and my output was like this:
You're pushing onto the same $arrPermissions array every time through the loop, that's why each of the roles gets a longer and longer copy of the permissions array.
You're overwriting $arrHeader[$title] each time through the loop instead of adding a new key to it.
Your desired output has a key of '' for the empty sub_header_name, so I don't see why you have the if that sets $sub_header_name = 0;.
It should simply be:
foreach ($getAllPermissions as $value) {
$arrHeader[$value->header_name][$value->sub_header_name][] = $value->name;
}
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"
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array with different extension's values, Now i want check whether '.csv' is IN this Array or not . If it is then whats the name.
Ex: - Array
(
[0] => xyz.mp4
[1] => bulk_sample.csv
[2] => abc.avi
[3] => pqr.3gp
)
Here 'abc.csv' is available in array. and name should be in $name='abc.csv';
Simple "one-line" solution using preg_grep function:
$names = preg_grep("/\.csv$/i", $val);
print_r($names);
The output:
Array
(
[3] => abc.csv
)
Note, that hypothetically there could be multiple .csv items
Try this code :
In this foreach loop is checking for sub string which is .csv for all elements in array.
<?php
$array =array('test.mp4','abc.avi','xyz.3gp','abc.csv');
$flag=false;
$filename="";
foreach($array as $check) {
$place = strpos($check, ".csv");
if ($place>0) {
$flag=true;
$filename=$check;
break;
} else {
$flag=false;
}
}
if($flag){
echo "File Name:".$filename;
}else{
echo "not found any .csv in array"."<br>";
}
?>
Note: it will return first found name with .csv extension.