This question already has answers here:
PHP search array in array
(3 answers)
Closed 8 years ago.
I have an array:
$members = array(
'Group1' => array(
'user1',
'user2'
),
'Group2' => array(
'user3',
'user4'
),
'Group3' => array(
'user5'
));
How can I search group name for specific user ?
Simplest (logic wise) way is a quick foreach loop with an in_array() check to find whether or not your name is in the sub array:
$search = 'user4';
foreach($members as $group_name => $names)
if(in_array($search, $names))
echo $search . ' is in ' . $group_name; // user4 is in Group2
Example: https://eval.in/148332
Assuming a user may occur in multiple groups:
$groups = array();
foreach ($members as $group_name => $group_members) {
if (in_array('user4', $group_members)) {
$groups[] = $group_name;
}
}
The variable $groups will contain all the group names that matched, though in the above example that's only "Group2".
Related
This question already has answers here:
How to display an Array under alphabetical letters using PHP?
(6 answers)
Closed 8 months ago.
I have an array something like this:
$array = array(
'Actual Hours' => 'http://www.example.com/actual',
'Algorithm' => 'http://www.example.com/algorithm',
'Time Clock App' => 'http://www.example.com/time',
)
And I want the output something like this:
Array
(
[A] => [
[Actual Hours] => http://www.example.com/actual
[Algorithm] => http://www.example.com/algorithm
],
[T] => [
[Time Clock App] => http://www.example.com/time
],
)
So basically I want something like this ..
As you can see, I want the first letter of the array key and want to sort it by adding a new key and group it that way.
Here you go !
$array = array(
'Actual Hours' => 'http://www.example.com/actual',
'Algorithm' => 'http://www.example.com/algorithm',
'Time Clock App' => 'http://www.example.com/time',
);
$new_array = [];
foreach( $array as $key => $value ) {
$char = strtoupper(substr($key,0,1));
if(!array_key_exists( $char, $new_array)) {
$new_array[$char] = [];
}
$new_array[$char][$key] = $value;
}
$array = $new_array;
Loop, get the first letter 0 of the key string and add to that with the whole key:
foreach($array as $k => $v) {
$result[strtoupper($k[0])][$k] = $v;
}
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I am selecting values from my database, when I dump the $results I get;
array (
0 =>
(object) array(
'FieldName' => 'certification_name',
'FieldValue' => 'White Belt',
),
1 =>
(object) array(
'FieldName' => 'certification_name',
'FieldValue' => 'Yellow Belt',
),
)
I want to display the following on my page;
Certification List: White Belt, Yellow Belt
I have created a loop but when I echo the result I get this;
Certification List: Array Array
My PHP code;
foreach ($results as $result) {
$name = $result->FieldName;
$value = $result->FieldValue;
$items[] = array(
'name' => $name,
'value' => $value
);
}
$items = implode("\n", $items);
echo 'Certification List: ' .$items;
What do I need to change in order to get this working?
You shouldn't push arrays into $items, just push the values.
foreach ($results as $result) {
$items[] = $result->FieldValue;
}
$item_list = implode(", ", $items);
echo "Certification List: $item_list";
You can also replace the loop with:
$items = array_column($results, 'FieldValue');
This question already has answers here:
Array containing keys and associative array, how to make a relation
(3 answers)
Closed 5 years ago.
I have this array :
$firstArray = [
'location', 'address', 'streetNumber'
];
Extracted from that string variable :
$string = "location.address.streetNumber"
I would like get a value dynamically in an other array with these variables :
$value = $secondArray[$firstArray[0]][$firstArray[1]][$firstArray[2]];`
But without using keys directly ([0], [1], ...).
Is it possible ?
Thanks a lot !
will this do the work for you ?
$string = "location.address.streetNumber";
$firstArray = explode('.', $string);
$secondArray = ['location' => ['address' => ['streetNumber' => 'street 854']]];
$ref = &$secondArray;
foreach($firstArray as $val){
$ref = &$ref[$val];
}
$value = $ref;
unset($ref);
echo $value // street 854
This question already has answers here:
Turning multidimensional array into one-dimensional array [duplicate]
(11 answers)
Closed 4 years ago.
I need to flatten an array while making sure that there are no duplicate keys.
For instance let's say I have this:
$arr = array(
$foo = array(
'donuts' => array(
'name' => 'lionel ritchie',
'animal' => 'manatee',
)
)
);
I need a flattened array that looks like this:
$arr = array(
'donuts name' => 'lionel ritchie',
'donuts animal' => 'manatee',
);
It needs to work even if we have more than 1 parent keys.
I have the following code, but I am not sure I can work with this.
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array1)) as $k=>$v){
$array1c[$k] = $v;
}
It's very simple to do, just make it like this:
$arr = array(
$foo = array(
'donuts' => array(
'name' => 'lionel ritchie',
'animal' => 'manatee',
)
)
);
// Will loop 'donuts' and other items that you can insert in the $foo array.
foreach($foo as $findex => $child) {
// Remove item 'donuts' from array, it will get the numeric key of current element by using array_search and array_keys
array_splice($foo, array_search($findex, array_keys($foo)), 1);
foreach($child as $index => $value) {
// Adds an array element for every child
$foo[$findex.' '.$index] = $value;
}
}
Result of var_dump($foo); will be:
array(2) {
["donuts name"]=>
string(14) "lionel ritchie"
["donuts animal"]=>
string(7) "manatee"
}
Just try :)
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 6 months ago.
I don't think this can be done (with out my own function), however I'll ask any way.
I have a array in PHP like
array(
'item1' => array(
'Hello',
'Good',
),
'item2' => array(
'World',
'Bye',
),
)
The individual fields come from a web form
I want to rearrange it to the following
array(
array(
'item1' => 'Hello',
'item2' => 'World',
),
array(
'item1' => 'Good',
'item2' => 'Bye',
),
)
Make an array of objects from the field arrays
I could write a function to do this.
However I was wondering can one of the built-in array functions achieve this for me?
There is no built in function to do this - but here is a user-defined one that will:
function convert_array ($array) {
$result = array();
foreach ($array as $key => $inner) {
for ($i = 0; isset($inner[$i]); $i++) {
$result[$i][$key] = $inner[$i];
}
}
return $result;
}
See it working
Most certainly not the most efficient, but as close to "one function" as you'll get. ;)
array_map(function ($i) use ($array) { return array_combine(array_keys($array), $i); }, call_user_func_array('array_map', array_merge(array(function () { return func_get_args(); }), $array)));
See http://codepad.viper-7.com/xIn3Oq.
No. There is no built in function that would do this.