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>";
}
Related
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
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
Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}
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
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.