sorry for the title of my question i really don't know how to emphasize my problem but here is the sample array:
and here is my code
foreach ($_POST['noofguest'] as $keyg => $valueg) {
echo $valueg. "<br />";
}
foreach($_POST['room_no'] as $key => $value){
foreach($value as $key2 => $value2){
echo $value2 . " has " . $valueg . "<br />";
}
}
and the result of this is:
1
2
56 has 1
57 has 1
but this is not the result that i want, what i want is
1
2
56 has 1
57 has 2
is this possible??
For the second loop, you want to receive the same index of the room_no but only inside the noofguest array. So that's exactly what you should do:
foreach($_POST['room_no'] as $key => $value)
foreach($value as $key2 => $value2)
echo $value2 . " has " . $_POST['noofguest'][$key2] . "<br />";
Related
I have a card deck array:
$cards = array("2_of hearts" => 2, "3_of_hearts" => 3, "king_of_hearts" => 10);
And I want to echo the name of the card somewhere (example: 2_of_hearts) but also calculate something with the number attached to it, but I really can't seem to make it work. Also, I was unable to find a working answer for me.
Does anyone know how to do this?
If you use a foreach loop that provides you with the key and the value like this, you get both the 2_of hearts and the 2 as variables.
$cards = array("2_of hearts" => 2, "3_of_hearts" => 3, "king_of_hearts" => 10);
foreach ( $cards as $name => $value) {
echo $name . ' has a value of ' . $value.PHP_EOL;
$calc = $value + 5;
echo 'ADDED 5 - gives ' . $calc . PHP_EOL;
}
Result
2_of hearts has a value of 2
ADDED 5 - gives 7
3_of_hearts has a value of 3
ADDED 5 - gives 8
king_of_hearts has a value of 10
ADDED 5 - gives 15
Then you just do your calculation with the $value variable
using this syntax of foreach lets you get access to the keys of the array
$cards = array("2_of hearts" => 2, "3_of_hearts" => 3, "king_of_hearts" => 10);
foreach($cards as $key=> $value){
$key = explode("_",$key);
$cardname = $key[count($key)-1];
//echo $key[0] . " of " $cardname . "<br>";
echo $value . " of " $cardname . "<br>"; // edit after comments
}
NOTE: this is off-course if you sure your keys are always has the pattern #_of_cardname
I have a Json result like so
{"goals":["1","1"],"minutes":["12","34"],"player":["1","1"]}
And i am trying to create a table with rows displaying each stat like so
Goals Minutes Player
1 12 1
1 34 1
I currently have this code written up and although i am getting teh data its not quite working the way i want it to. Im not so clued up on Json and the displaying of it as you can see.
<?php
$jsonresult = $this->item->results;
$phpArray = json_decode($jsonresult, true);
foreach ($phpArray as $key => $value) {
foreach ($value as $k => $v) {
?>
<tr><td><?php echo $v; ?></td></tr>
<?php }} ?>
Cheers
Jonny
<?php
$json = '{"goals":["1","1"],"minutes":["12","34"],"player":["1","1"]}';
$array = json_decode($json,true);
print_r($array);
print "Goals, Minutes, Player\n";
foreach($array['goals'] as $key => $value) {
print $value . ','
. $array['minutes'][$key] . ','
. $array['player'][$key] . "\n";
}
Output
Goals, Minutes, Player
1,12,1
1,34,1
I have two arrays like this.
$array1=array(1,2,3,4,5,7);
$array2=array(1,2,3,4,5,6);
So, the output should bring the difference in both arrays.
The output should be.
1,2,3,4,5 -> These numbers exist in both arrays, so these should be ignored.
7 and 6 -> These numbers are the un-common in both arrays, so I need these values in array.
The output should be 7 & 6.
Help me out. I have tried array_diff and other array elements.
try this
array_merge(array_diff($array1,$array2),array_diff($array2,$array1))
foreach($array1 as $key => $value) {
if($value != $array2[$key]) {
echo "\$array1[" . $key . "] (" . $value . ") is different to \$array2[" . $key . "] (" . $array2[$key] . "<br />";
}
}
This is probably so newbie. Basically im l
$count_versions = array();
while ($row = mysql_fetch_array($result))
{
$email_build = $row1['build_info'];
$count_versions[] = $email_build;
}
Now when I use print_r I get this
Array ( [2660] => 8 [2662] => 6 [2655] => 6 [2666] => 1 )
Which is perfect, now all I want to do is to output those values like
2660 - 8 votes
2662 - 6 votes
2655 - 6 votes
2666 - 1 votes
When I try this it seems to break up the values back into a full array which undoes my array_count_values but I am stumped
I realize this foreach loop makes no sense but its as close as I can get, any ideas how I can basically print it out like print_r does it so i can put it in a table later
$i=0;
foreach ($count_versions as $version => $nums)
{
$i++;
echo "Version: " . $nums . " - " . $count_versions . "<br />";
}
It looks so easy to do it with a foreach:
$count_versions = array ( "2660" => 8, "2662" => 6, "2655" => 6, "2666" => 1 );
foreach ($count_versions as $key => $value)
echo $key.' - '.$value.' votes<br>';
echo "Version: " . $version . " - " . $nums . " votes<br />";
I am using a db query that takes in a state and city then spits out 10 fields. Currently I can only see those fields by using print_r. I tried a suggestion on the php manual site a for loop to print the fields however I can't get it to work properly. Here is the code:
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row)."</p>";
$arrayLength = count($row);
for ($i = 0; $i < $arrayLength; $i++){
echo "arrayName at[" . $i . "] is: [" .$row[$i] . "]<br>\n";
}
}
}
And this is the result:
Array ( [id] => 1299 [zip_code] => 04011 [city] => Brunswick [county] => Cumberland [state_name] => Maine [state_prefix] => ME [area_code] => 207 [time_zone] => Eastern [lat] => 43.9056 [lon] => -69.9646 ) ....
arrayName at[0] is: []
arrayName at[1] is: []
arrayName at[2] is: []
arrayName at[3] is: []
arrayName at[4] is: []
arrayName at[5] is: []
arrayName at[6] is: []
arrayName at[7] is: []
arrayName at[8] is: []
arrayName at[9] is: []
Any ideas why I am not able to properly print the fields and their values? Also my code fails if the query returns more than one row as the current code doesn't really accommodate it.
I put $i in the body of the for loop to see if it was working properly. Ideally I would have the field name where $i is and the value to the right of it after the colon.
you are fetching with mysql_fetch_assoc so
change loop into
foreach($row as $key => $value){
echo "Array key : $key = $value <br/>";
}
Your array keys are 'id', 'zip_code' etc. There's nothing in the 0, 1 etc. indexes of the array.
foreach ($row as $key => $value) {
echo "arrayName at[" . $key . "] is: [" . $value . "]<br>\n";
// which is the same as:
echo "arrayName at[" . $key . "] is: [" . $row[$key] . "]<br>\n";
}
Yes, because that returns an associative array
That means that you have to access the elements like so:
$row["id"] for instance
What you want is this
foreach($row as $key => $value)
echo "arrayName at[" . $key . "] is: [" .$value . "]<br>\n";
use mysql_fetch_array() instead of mysql_fetch_assoc()
the mysql_fetch_assoc() will return an associative array and only accessable via $row['name'].
With mysql_fetch_array() you can fetch an associative array, a numeric array, or both.
take a look here: http://www.php.net/manual/en/function.mysql-fetch-array.php