Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
I have a line of code to get attributes :
$attributes = $as->getAttributes();
When i use print_r
$results = print_r($attributes);
This is what i get :
Array
(
[http://schemas.microsoft.com/2012/12/certificatecontext/extension/subjectkeyidentifier] => Array
(
[0] => username
)
[http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress] => Array
(
[0] => email#mail.com
)
[http://schemas.xmlsoap.org/claims/CommonName] => Array
(
[0] => User lastname
)
[http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn] => Array
(
[0] => email#mail.com
)
[uid] => Array
(
[0] => user
)
)
How can i display those results with a foreach ?
To get all the informations you can use
foreach($attributes as $url=>$info)
{
echo $url; //example : http://schemas.microsoft.com/2012/12/certificatecontext/extension/subjectkeyidentifier
foreach($info as $key=>$attr)
{
echo $key; //example : 0
echo $attr; //example : username
}
}
Per your answers to the comments, you want to be able to format each row differently. Therefore, you could do something like the following:
<?php
foreach($attributes as $key => $value){
echo $key; // the key of the array
echo $value; // the value of the array row
echo '<br />'; // if you want a new line
}
However, if you want a common format, such as a list, you could use implode to do it for you (docs).
<?php
echo '<ul><li>'; // Open list and add first open tag
echo implode('</li><li>', $attributes);
echo '</li></ul>'; //Close last item and list element
And this would generate an HTML formatted list of the elements, though you certainly could do any delimiter that worked for your project.
Related
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 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.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem is actually, there is an array containing a number of arrays. I have to print the elements of one of the sub arrays named 'list'. But I have a problem to fetch the subarray while using foreach loop.
The following code is:
foreach($arr as $key => $value)
{
$arr1=$value[$list];
echo $arr1;
}
Your code should be:-
// declare second array outside loop
$arr1 = [];
foreach($arr as $key => $value)
{
// list is a key in $value array then you can access via below way
$arr1[] = $value['list'];
}
// print Array using print_r() function outside loop
print_r($arr1);
Hope it will help you :)
If your array like in below format
<?php
$arr = array(
'key' => array('array elements here'),
'list' => array('array elements here'),
'key' => ........
);
?>
you can use below to get only list array
<?php
foreach($arr as $key => $value)
{
if($key == 'list') {
$arr1=$value['list'];
break;
}
}
print_r($arr1);
?>
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 8 years ago.
Improve this question
I have two lists of words in two arrays
first array
Array
(
[0] => make
[1] => break
[2] => buy
)
second array
Array
(
[0] => home
[1] => car
[2] => bike
)
and I want to display all possible combinations but make sure the first array is always the first word and 2nd array is always the second word:
The above two arrays should display the following list:
make home
make car
make bike
break home
break car
break bike
buy home
buy car
buy bike
Thank you in advance.
Hi Saikios,
thanks for your reply.
This is what I have which seems very similar to what you have posted:
$list1 = array("make","break","buy");
$list2 = array("home","car","bike");
for($a=0; $a<3; $a++){
for($b=0; $b<3; $b++){
echo($list1[$a].$list2[$b]);
echo("<br />");
}
}
Just was wondering if there is a better way. Both of my lists have about 200 words.
For a correct answer I would need to know what you are trying or how,
but a solution for that problem is as simple as
<?php
$array1 = array("make", "break", "buy");
$array2 = array("home", "car", "bike");
foreach ($array1 as $i => $value) {
foreach ($array2 as $j => $value2) {
echo $value.' '.$value2.'<br />';
}
}
?>
foreach($firstarray as $f)
{
foreach($secondarray as $s)
{
echo $f.' '.$s;
}
}
To make sure the first array is always the first word we use it as the primary loop. In essence we are iterating through each item and for each item we find in the array we are then looping through the second array so it creates all the word combinations for each item in the first array.
This example is more efficient that the previously chosen answer since pulling the array key out of each item is not needed and therefore not the most efficient answer to the question.
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 8 years ago.
Improve this question
I have a function that searches through an array of vegetable varieties to see if it matches an ID:
// my function
function findVariety($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, findVariety($subarray, $key, $value));
}
return $results;
}
// function call
$picks = findVariety($veg,id,$sf->spring_choice);
when successful, it returns something like this:
// returned from print_r($picks);
Array ( [0] => Array ( [id] => 2 [variety] => Royal Burgundy (bush) ) )
All I'm missing is how to add the variety to an echo that I'm sending to my page, Ex:
echo '<td height="90px">'.$picks['variety'] .'<br />add plants</td>';
As of now, I have been stuck on this last step! Any help would be amazing...
Your returned array is nested so to access the variety you'd need to do this:
echo $picks[0]['variety']