I need to create an array where i can use 1,2,3 but have 1v1, 2v2, Clubs associated with them.
I am going to use 1,2,3 for option value and 1v1,2v2 and clubs to display to the user.
How can I store this in an array and then use foreach to extract?
Thanks
$data = array(
1 => '1v1',
2 => '2v2',
3 => 'Clubs';
);
echo '<select>';
foreach($data as $value => $title) {
echo '<option value="'.$value.'">'.$title.'</option>';
}
echo '</select>';
You can make your original values (1,2,3) the keys and the (1v1, 2v2, Clubs) the values.
$data = array(1 => '1v1', 2 => '2v2', 3 => 'Clubs');
foreach($data as $key => $value) {
print $key.' - '.$value.'<br/>';
}
Maybe, I don't understand your question. try to use the next array:
$array = array(1 => '1v1', 2 => '2v2');
And foreach:
<select>
<? foreach ($array as $k => $v) { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<? } ?>
</select>
I don't fully understand the question, but it seems like you want to map the values 1,2,3 to the text '1v1','2v2','Clubs'. PHP supports associative arrays that are suitable for this purpose:
$a = Array(
1 => '1v1',
2 => '2v2',
3 => 'Clubs'
);
Related
I must be missing something about how PHP arrays are handled. When I execute the following code:
<?php
$ary = array(
"alpha" => array("A"=>1,"B"=>2,"C"=>3),
"beta" => array("A"=>7,"B"=>8,"C"=>9)
);
foreach ($ary as $key => $vals) {
$vals["B"]=99;
echo $key."= ".$vals["A"]." ".$vals["B"]." ".$vals["C"]."<br>";
}
echo $ary['alpha']["B"]."<br>";
?>
I get:
alpha= 1 99 3
beta= 7 99 9
2
The change to 99 in each case seems to be lost. What am I doing wrong?
If you want to change items of array in foreach statement you should pass by reference.
foreach ($ary as $key => &$vals) {
}
<?php
$ary = array(
"alpha" => array("A"=>1,"B"=>2,"C"=>3),
"beta" => array("A"=>7,"B"=>8,"C"=>9)
);
foreach ($ary as $key => $vals) {
//$vals["B"]= 99;
$ary[$key]["B"] = 99;
echo $key."= ".$vals["A"]." ".$vals["B"]." ".$vals["C"]."<br>";
}
echo $ary['alpha']["B"]."<br>";
?>
This question is just for fun and out of curiosity.
Edit : My question is different than
How to find the foreach index
Because $key has already a non-numeric value in my case.
Without having a variable outside a foreach that is increment inside the foreach scope, as the usual $i, is there a way to get the index of an item when $key is already named ?
Exemples :
$myNumericIndexArray = ('foo', 'bar', 'go', 'habs');
foreach($myNumericIndexArray as $key => $value){
//Here $key will be 0 -> 1 -> 2 -> 3
}
Now, if I have :
$myNamedIndexArray = ('foo' => 'bar', 'go' => 'habs', 'CSGO_bestTeam' => 'fnatic');
foreach($myNamedIndexArray as $key => $value){
//Here $key will be foo -> go -> CSGO_bestTeam
}
Can I, without having to :
$i=0;
foreach($myNamedIndexArray as $key => $value){
//Here $key will be foo -> go -> CSGO_bestTeam
$i++;
}
access the index of a named array. Something declared in the foreach declaration like in a for or a status of $key ?
Have a good one.
If you really want index array of associative array than try this:
$myNamedIndexArray = ['foo' => 'bar', 'go' => 'habs', 'CSGO_bestTeam' => 'fnatic'];
$keys = array_keys($myNamedIndexArray);
foreach($myNamedIndexArray as $key => $value){
echo array_search($key, $keys);
}
I don't know why you would want to do an array_keys() and array_search() every loop iteration. Just build a reference array and you can still foreach() the original:
$positions = array_flip(array_keys($myNamedIndexArray));
foreach($myNamedIndexArray as $key => $value){
echo "{$key} => {$value} is position {$positions[$key]}\n";
}
Something like this :
<?php
$myNamedIndexArray = array('foo' => 'bar', 'go' => 'habs', 'CSGO_bestTeam' => 'fnatic');
$numericIndexArray = array_keys($myNamedIndexArray);
foreach($numericIndexArray as $key=>$value){
echo $key.'</br>'; //here key will be 0 1 2
echo $value. '</br>';
}
I'd try something like this:
<?php
$myNamedIndexedArray = ['foo' => 'bar', 'go' => 'habs', 'CSGO_bestTeam' => 'fnatic'];
$myNumberIndexedArray = array_keys($myNamedIndexedArray);
foreach($myNumberIndexedArray as $key => $value){
echo $key. " => " . $myNamedIndexedArray[$myNumberIndexedArray[$key]]."<br />";
}
?>
Adn the output will be:
0 => bar
1 => habs
2 => fnatic
I have this some arrays that look like this,
$array = Array(
'Homer' => Array
(
'id' => 222,
'size' => 12
),
'Bart' => Array
(
'id' => 333,
'size' => 3
)
);
I would like to echo Homer: id is 222, size is 12
then in the next line echo Bart: id is 333, size is 3 using a foreach loop as key and values.
So i basically want to echo all the Simpsons character names which have their id and size next their names.
I tired this but it printed homer too many times and it even used Bart's id and size at one point.
foreach( $array as $billdate => $date) {
foreach( $date as $k => $v) {
echo $billdate; // Prints Homer and bart
foreach($array as $innerArray){
foreach($innerArray as $key => $value){
echo "[". $key ."][". $value ."] <br/>";
}}
}
}
Thanks in advance.
you can try like this:
foreach( $array as $billdate => $date) {
echo $billdate.': id is '.$date['id'].', size is '.$date['size'];
}
Don't use so many foreach ,just think your need loop ...
foreach( $array as $billdate => $date) {
echo $billdate; // Prints Homer and bart
foreach($date as $key => $value){
echo "[". $key ."][". $value ."] <br/>";
}
}
Have an array for a ranking script.
Some times the key will be the same. They are numeric.
When the sort is ran, only non-like values are echoed.
Can't figure out the fix.
$list = array( $value1 => 'text', $value2 => 'text', $value3 => 'text');
krsort($list);
foreach ($list as $key => $frame) {
echo $frame;
}
If you assign two values to the same key in an array, the first value will be overridden by the second. You'll therefore end up with only one value for that key in the array.
To resolve this, I'd suggest to change your array structure like this:
<?php
$list = array( $key1 => array($key1member1, $key2member2),
$key2 => array($key2member1),
$key3 => array($key3member1, $key3member2, $key3member3) );
krsort($list);
foreach ($list as $key => $frames) {
foreach ($frames => $frame) {
echo $frame;
}
}
?>
Going by what you wrote in the comments to this question and my other answer, I'd recommend to switch keys and values.
<?php
$list = array( "frame1" => 4, "frame2" => 2, "frame3" => 99, "frame4" => 42 );
arsort($list);
foreach ($list as $frame => $ranking) {
echo $frame;
}
?>
I have been working on this a while. I see multi-dimensional arrays in php are not that easy.
Here is my code:
while (list($key,$value) = each ($x))
{
Print "$key => $value\n<br>\n";
}
This works well to display the keys of the main array. what I get is :
visitors => Array
actions => Array
actions-average => Array
time-average => Array
pages-entrance => Array
What I want is the visitors and the value (number of visitors), value of actions, etc.
I want to then save the value in Mysql. Some I will have to convert from a string to and int or date.
I need to list one more level deep. But I cannot see how to do this.
--------------Added -----------
So what I have is an array of arrays. I need to step through each array.
did you try print_r ?
if you need more control over formatting then embedded loops as suggested by #Nick is the best option. Although it would be more natural and safer to use foreach loops rather than while.
foreach($x as $key => $value){
foreach( $value as $key2 => $value2){
print "$key $key2 => $value2\n<br />\n";
}
}
see PHP manual: each , there is a "caution" frame.
EDIT 1
I update sample code above for 2 day array.
It seems your array has more than 2 dimension. Then you should use recursion.
function my_print_r($x,$header="")
{
foreach($x as $key => $value){
if(is_array($value))
my_print_r($value,$header . $key . " " );
else
print "$header $key2 => $value2\n<br />\n";
}
}
Try loops like this code:
$arrA=array("a", "b", "c");
$arrB=array("x", "y", "z");
$x=array("visitors" => $arrA, "actions" => $arrB);
foreach($x as $key => $value)
{
foreach($value as $v)
echo "$key => $v<br>\n";
}
OUTPUT
visitors => a<br>
visitors => b<br>
visitors => c<br>
actions => x<br>
actions => y<br>
actions => z<br
the best way is var_dump($arr);
<?php
var_dump($_SERVER);
?>
with output that includes types, string length, and will iterate over objects as well.
Since you want to iterate over an array, give foreach a try:
foreach ($arr as $el)
{
// ... Work with each element (most useful for non associative arrays, or linear arrays)
}
// or
foreach ($arr as $key => $value)
{
// ... Work with $key and $value pairs (most useful for hashes/associative arrays)
}
/ / . . . here, we take variable $company for nested array and in this variable we put 2 employee id and its called multi-dimensional array and we take $company variable to assign the associative array with keys so we print the output with keys
$company=[
$emp=[1,"neha","employee",12000,30000],
$emp1=[2,"moni","manager",12000],
];
$company=["first" => $emp,"second" => $emp1];
foreach($company as $key => $value)
{
echo "$key ";
foreach($value as $v1){
echo "$v1";
}
}
output :-
employee ID name designation salary bonus
first 1 neha employee 12000 30000
second 2 moni manager 12000