I have the array below and I can't seem to figure out how to pull out the information from the array Team_1 and echo out the 0 - 5 values inside it.
array (size=3)
'Team_1' =>
array (size=5)
0 => string '1199' (length=4)
1 => string '1182' (length=4)
2 => string '1105' (length=4)
3 => string '1212' (length=4)
4 => string '891' (length=3)
'Team_2' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'Team_3' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
The key is Team_1 so you can reference it directly and do a foreach loop to do the echo (you can add any html you want to echo to format the values).
foreach ($arr['Team_1'] as $val) {
echo $val.'<br>';
}
If your intention is to loop through all teams and echo values
foreach ($teams as $team => $vals) {
echo $team;
foreach ($vals as $val) {
echo $val;
}
}
$output1 = $array['Team_1'][0]; // Should output 1199
echo $output1;
OR
foreach ($array['Team_1'] as $data){
echo $data.'<br/>';
}
Related
I have an array that has this structure:
array (size=6)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
I have values from a selection that is Dorado->32GB->Madera and I need find this value in the array; if its true that it exists, then take the other values like a vlr, pcost, etc.
What is the best method to find that, remembering that the values with number are dynamic, sometimes with 1 or 5 example:
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
0 => string 'Azul' (length=6)
1 => string '32GB' (length=4)
Can somebody explain how I could do this?
I suppose, this is a code to start with:
$searchValue1 = 'Dorado';
$searchValue2 = '32GB';
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if ($item[0] === $searchValue1 && $item[1] === $searchValue2 && $item[2] === $searchValue3) {
print_r($item);
}
}
A bit sophisticated version with checking whether $searchValue_ is not empty is:
$searchValue1 = 'Dorado';
$searchValue2 = null;
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if (
(!empty($searchValue1) && $item[0] === $searchValue1)
&&
(!empty($searchValue2) && $item[1] === $searchValue2)
&&
(!empty($searchValue3) && $item[2] === $searchValue3)
) {
print_r($item);
}
}
update: with unknown number of search parameters you can:
$searchValues = ['Dorado', 'Plastico'];
foreach ($yourArray as $item) {
// find intersection of two arrays
$intersect = array_intersect($item, $searchValues);
// if size of intersection is same as size of `$searchValues`
// then you can be sure that all elements of `$searchValues` are in `$item`
if (count($intersect) === count($searchValues)) {
print_r($item);
}
}
can i showing only one value in foreach array having multiple same value, without grouping the array firstly in the query like this :
0 =>
array (size=10)
'id' => string '1' (length=1)
'questionname' => string 'question 01' (length=36)
'answerspossible' =>
array (size=3)
0 =>
array (size=2)
...
1 =>
array (size=2)
...
2 =>
array (size=2)
...
'answer' => string 'YES' (length=3)
'answer2' => string '' (length=0)
1 =>
array (size=10)
'id' => string '1' (length=1)
'questionname' => string 'question 01' (length=36)
'answerspossible' =>
array (size=3)
0 =>
array (size=2)
...
1 =>
array (size=2)
...
2 =>
array (size=2)
...
'answer' => string 'YES' (length=3)
'answer2' => string 'test answer' (length=0)
Result i want in the view is to group by the questioname inside the foreach :
question 01 :
- answer & answer 2
- answer & answer 2
My code is :
foreach ($Questions as $Key => $Question) {
echo $question['questionname'];
echo $Question['answer']." & ".$Question['answer2'];
}
thnx for help :)
$justblank = ''; // just a blank variable we will use it later.
foreach ($Questions as $Key => $Question) {
echo $question['questionname'];
$questionanswers = $Question['answer']." & ".$Question['answer2'];
if($justblank == $questionanswers){
break;
}else{
echo $questionanswers;
}
$justblank .= $questionanswers;
}
Hi Mohammed, I hope this help you :).
a blank var work
$justblank = -1;
foreach ($Questions as $Key => $Question) {
if($Question['id']!=$justblank){
echo $Question['questionname'];
$justblank=$Question['id'];
}
....
}
Right now i'm trying to validate some postdata with filter_var(). I want to get the filter related to each input from my database. So if the input should be filtered by EMAIL, the variable would contain FILTER_VALIDATE_EMAIL. This would then be passed like so:
foreach($this->postdata as $key => $input){
if(!((empty($requirements[$key][1])) || $requirements == 'allowed')){
if(filter_var($input, $requirements[$key][1]) === false){
$errors = true;
}
}
}
The $postdata looks like this:
array (size=4)
'personer_navn' =>
array (size=1)
0 => int 0
'personer_alder' =>
array (size=1)
0 => int 1
'personer_kon' =>
array (size=2)
0 => int 2
1 => int 3
'personer_by' =>
array (size=1)
0 => int 4
And the $requirements looks like this:
array (size=4)
'personer_navn' =>
array (size=4)
0 => string 'string' (length=6)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
'personer_alder' =>
array (size=4)
0 => string 'int' (length=3)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
'personer_kon' =>
array (size=4)
0 => string 'allowed' (length=7)
1 => string 'allowed' (length=7)
2 => string 'allowed' (length=7)
3 => string 'allowed' (length=7)
'personer_by' =>
array (size=4)
0 => string 'string' (length=6)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
Again the problem seems to ba passing $requirements[$key][1] to the filter_var() function.
Any help is appreciated.
FILTER_VALIDATE_EMAIL is not to be used as a string. Try using it without the quotes. Examples can be found in the documentation
A simple change that will fix the bug
foreach($this->postdata as $key => $input){
if(!((empty($requirements[$key][1])) || $requirements == 'allowed')){
if(filter_var($input, constant( $requirements[$key][1]) ) === false){
$errors = true;
}
}
}
The constant function returns the (integer) value of the filter that is string.
read P.P-s answer too.
I have an array in loop while like this when I var_dump it.
array (size=73)
0 => string '1' (length=1)
'address' => string '1' (length=1)
1 => string '2' (length=2)
'street_no' => string '2' (length=1)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
I want disply data on screen for first data is
`AddressMain: 1
streetMain: 1
.....
AddressLeft: vbfgh
street1Left: fgfd
.....
AddressRight: vbfgh
streetRight: fgfd
.....
AddressCenter: vbfgh
streetCenter: fgfd
.....`
How I can change my label like this if this code in loop while ?
And this is my code
while( $row = pg_fetch_array($result)){
echo "AddressMain:".$row['address'];
echo "streetMain:".$row['street_no'];
echo "<br/>";
echo ".......";
}
Plz help me how I can change my label in this loop ?
thanks
Use an array to define labels, then loop around it.
$labels = array('Main','Left','Right','Center');
$i = 0;
while( $row = pg_fetch_array($result)){
echo "Address".$labels[$i].":".$row['address'];
echo "street".$labels[$i].":".$row['street'];
echo "<br/>";
$i++;
}
I have following PHP code
$selected_items = explode(',' , $vars['value']);
foreach($vars['options'] as $option) {
$selected = "";
if(in_array($option,$selected_items)){
$selected = " selected = 'selected'";
}
echo "<option" . $selected . ">" . $option . "</option>";
}
$selected_items:
array
0 => string 'Html' (length=4)
1 => string ' Css' (length=4)
2 => string ' HTML5' (length=6)
3 => string ' CSS3' (length=5)
4 => string ' Javascript' (length=11)
$vars['options']:
array
0 => string 'Html' (length=4)
1 => string 'Css' (length=3)
2 => string 'HTML5' (length=5)
3 => string 'CSS3' (length=4)
4 => string 'Javascript' (length=10)
5 => string 'Dhtml' (length=5)
6 => string 'Actionscript' (length=12)
7 => string 'Javafx' (length=6)
8 => string 'Flex' (length=4)
9 => string 'VisualBasic' (length=11)
10 => string 'Ajax' (length=4)
11 => string 'ASP.NET' (length=7)
12 => string 'Java' (length=4)
13 => string 'Php' (length=3)
14 => string 'Perl' (length=4)
15 => string 'Python' (length=6)
16 => string 'J2ME' (length=4)
17 => string 'VB.NET' (length=6)
18 => string 'C#' (length=2)
19 => string 'ASP' (length=3)
20 => string 'JSP' (length=3)
21 => string 'J2EE' (length=4)
22 => string 'C++' (length=3)
In $selected_items have an array. So i have to check whether the value in $option is in $selected_items. But its working for first time. But second time the value is in array but the condition get false.
Any idea where is the problem ?
I thing it should work if that string present in array.
If you have problem with above one you can try this:
$selected_items = explode(',' , $vars['value']);
foreach($vars['options'] as $option) {
$selected = "";
foreach($selected_items as $selecteditems){
if($option == $selecteditems)){
$selected = " selected = 'selected'";
}}
echo "<option" . $selected . ">" . $option . "</option>";
}
Just remove the space before string in array $selected_items.