Add key and value to Zend MultiCheckbox dynamically - php

i have retrieved courseList and courseId from databse like this
foreach ($courses as $item) {
$checkBoxText = '';
$checkBoxText .= $item['courseRubric']. "-". $item['courseNumber']. " ". $item['courseTitle']. " [".$item['semester']. " " . $item['year']. "]";
$this->courseList[] = $checkBoxText;
$checkboxId = '';
$checkboxId .= $item['id'];
$this->courseId[] = $checkboxId;
}
Now, I want to add these array items to Zend_MultiCheckbox,
foreach ($this->courseId as $key => $value) {
$courseId[$value]= $value;
}
foreach ($this->courseList as $key => $value) {
$element->addMultiOptions(array(
$courseId[$key] => $value
));
}
This logic is not working.Can anyone suggest me how I can get
Course
Thanks

You have 2 solutions:
1 :
foreach ($this->courseList as $key => $value) {
$element->addMultiOption("$courseId[$key]", "$value");
}
2:
$opions = array();
foreach ($this->courseList as $key => $value) {
$options[$courseId[$key]] = $value;
}
$element->addMultiOptions($options);
I think, the 2nd is better.
Good luck.

Related

iterate associate array in php does not print out values

I'm trying out the following code for arrays in php, I create a associate array, print out the values and add one more to the array - print out again. This works, but if I try the foreach ($MovieCollection as $key => $value) it does not print out the values. Why does it not do that?
$myArray = array("Star Wars", "The Shining");
foreach ($myArray as $val)
{
echo("Movie: " . $val ."<br>");
}
$MovieCollection = array();
$MovieCollection[] = array('title' => 'Star Wars', 'description' =>'classic');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
$MovieCollection[] = array('title' => 'The shinning', 'description' =>'creepy');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
echo("<br><br>");
// This does not print the values?
foreach ($MovieCollection as $key => $value)
{
echo($key .": " . $value ."<br>");
}
That is because in this part $MovieCollection is an array of arrays and if you want to echo the $value which is an array, you will do an Array to string conversion which does not work.
What you might do is use another foreach to show the values per array:
foreach ($MovieCollection as $value) {
foreach ($value as $k => $v) {
echo($k .": " . $v ."<br>");
}
}
See a Php demo

PHP read value form multidimensional array

its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO

php associative array: no array build with key from variable name

this code works as expected
$fruits = array("d" => "Zitrone", "b" => "Banane", "c" => "Apfel");
$fruits["a"] = "Orange";
//print_r($fruits);
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
ok, now I would like to do the same programmatically:
$collection = array();
foreach ($xml->abschnitte[0]->abschnitt as $sec) {
//echo $sec['strecke']; // this works, the strings are printed out
$collection[$sec['strecke']] = $sec['id'];
}
//print_r($collection); // nothing to see here
//ksort($collection);
foreach ($collection as $key => $val) {
echo $key . " = " . $val . "\n";
}
it seems that there no collection will be build. but there must be a way to build the key up from variables. what do i miss? thanks in advance, mischl

Extract values in one line based on key json decode and foreach

I need to work out how i can get showBtn(3) to match up against the first result in every other key.
ShowBtn/3
btnMenulink/101
btnArticleLink/2
btnPhone/036244789
btnUrl/
btnName/Office
PHP:
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach ($array as $key => $value) {
foreach ($value as $next_key => $next_value) {
echo $key . ":" . $next_key . ":" . $next_value . "\n";
}
}
I want this:
if (showBtn == 3) {
echo '<a href='tel:btnPhone'>btnName</a>';
}
the result would be
Office Mobile
I almost have it!
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
echo 'Showbtn: '.$parsed['showBtn'][0].' Phone: '.$parsed['btnPhone'][0].' Name: '.$parsed['btnName'][0];
echo '<hr/>Showbtn: '.$parsed['showBtn'][1].' Phone: '.$parsed['btnPhone'][1].' Name: '.$parsed['btnName'][1];
Now i just gotta get that [0] [1] into the loop somehow
I have it but can it be done better?
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
$i=0;
foreach ($parsed as $key => $value) {
if ($parsed['showBtn'][$i] == 3) {
echo 'Showbtn: '.$parsed['showBtn'][$i].' Phone: '.$parsed['btnPhone'][$i].' Name: '.$parsed['btnName'][$i].'<hr/>';
}
$i++;
}
This'll work for you. You need to work it as this way
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach($array['showBtn'] as $key => $value){
if($value == 3){
echo ''.$array['btnName'][$key].'<br>';
}
}
Output:
Office
Mobile

PHP Merge Similar Objects In A Multidimensional Array

I have a multidimensional array in PHP, something that looks like:
array(array(Category => Video,
Value => 10.99),
array(Category => Video,
Value => 12.99),
array(Category => Music,
Value => 9.99)
)
and what I would like to do is combine similar categories and output everything into a table, so the output would end up being:
<tr><td>Video</td><td>23.98</td></tr>
<tr><td>Music</td><td>9.99</td></tr>
Any suggestions on how to do this?
EDIT:
I can have these in two different arrays if that would be easier.
A simple loop will do:
$array = [your array];
$result = array();
foreach ($array as $a) {
if (!isset($result[$a['Category']])) {
$result[$a['Category']] = $a['Value'];
} else {
$result[$a['Category']] += $a['Value'];
}
}
foreach ($result as $k => $v) {
echo '<tr><td>' . htmlspecialchars($k) . '</td><td>' . $v . '</td></tr>';
}
$result = array();
foreach ($array as $value) {
if (isset($result[$value['Category']])) {
$result[$value['Category']] += $value['Value'];
} else {
$result[$value['Category']] = $value['Value'];
}
}
foreach ($result as $category => $value) {
print "<tr><td>$category</td><td>$value</td></tr>";
}

Categories