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.
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);
}
}
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/>';
}
---first array---
array (size=3)
0 =>
array (size=1)
0 => string ' doctor' (length=47)
1 =>
array (size=1)
0 => string ' Contact: Rob miller' (length=22)
2 =>
array (size=2)
0 => string ' Location: anywhere 23 ' (length=37)
1 => string ' ME 04848' (length=10)
--- second array---
array (size=2)
'notelp' => string '(xxx) xxx-xxxx ' (length=15)
'site' => string 'http://www.example.com/' (length=33)
what I want is:
array (size=2)
'notelp' => string '(xxx) xxx-xxxx ' (length=15)
'site' => string 'http://www.example.com/' (length=33)
'profession' => string ' doctor' (length=5)
'contact' => string ' Contact: Rob miller' (length=22)
'address' => string ' Location: anywhere 23 , ME 04848'
Use Implode to point the third key in second array
$secondArray['profession'] = $first_array[0][0];
$secondArray['contact'] = $first_array[1][0];
$secondArray['address'] = implode(', ', $first_array[2])
You can do the following
$second_array['profession'] = $first_array[0][0];
$second_array['contact'] = $first_array[1][0];
$second_array['address'] = $first_array[2][0];
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 was trying to make a schedule from the database that would look something like this:
I ended up using three loops for displaying. First for showing dates, second to separate every day into rows with overlapping events and third to print out the events in a row.
Before display I had two arrays: $overlap_array and $dates_unique. $overlap_array is the array with resolved rows with events that are overlapping. Here is the little snippet from $overlap_array:
array (size=7)
0 =>
array (size=2)
0 =>
array (size=7)
0 => string '2014-04-21' (length=10)
1 => string '10:00:00' (length=8)
2 => string '14:00:00' (length=8)
3 => string '36000' (length=5)
4 => string '14400' (length=5)
5 => string 'Listening Cities Presentations Session 1' (length=40)
6 => string '1' (length=1)
1 =>
array (size=7)
0 => string '2014-04-21' (length=10)
1 => string '14:00:00' (length=8)
2 => string '20:30:00' (length=8)
3 => string '50400' (length=5)
4 => string '23400' (length=5)
5 => string 'Listening Cities Presentation 2' (length=31)
6 => string '2' (length=1)
1 =>
array (size=3)
0 =>
array (size=7)
0 => string '2014-04-21' (length=10)
1 => string '15:30:00' (length=8)
2 => string '17:00:00' (length=8)
3 => string '55800' (length=5)
4 => string '5400' (length=4)
5 => string 'Listening Cities Performance at the DockBar: Part1' (length=50)
6 => string '3' (length=1)
And $dates_unique is an array with unique dates in the schedule:
array (size=3)
0 => string '2014-04-21' (length=10)
6 => string '2014-04-22' (length=10)
9 => string '2014-04-23' (length=10)
I added Tab.js from Twitter Bootsrap 3 for my tabs. Then I used those arrays to display the schedule inside 3 foreach loops:
echo '<div class="tab-content">';
$a = 1;
$class_active = 'tab-pane fade in active';
foreach ($dates_unique as $dates) {
echo '<div id="section' . $a . '" class="' . $class_active . '">';
$a++;
$class_active = "tab-pane fade";
foreach ($overlap_array as $row) {
if ($dates == $row[0][0]) {
echo '<div class="rows">';
foreach ($row as $event) {
$left = ((($event[3] - 36000) / 468)) + 0.1;
echo '<a href="event/' . $event[6] . '"><div style="width:' .
(($event[4] / 468) - 0.4) .'%; left: ' . $left . '%;" class="collumn">'.
$event[5] . ' ' . $event[1] . ' - ' . $event[2] .' '. $event[0] .
'</div></a>';
}
echo '</div>';
}
}
echo '</div>';
}
echo '</div>';
How could I reduce the number of foreach loops here or maybe I should use another way to display the schedule?