Array initialisation with value - php

Hello guys i have coded something like this ..I just dont know wheather the code is right or not ..But i have a question
THe code is
$featured = array('name' => 12,'yeah' => 10);
foreach($featured as $key => $value){
echo $value['name'];
}
I know that value of name can be acessed by $featured['name']
but now I just need to know wheather the key of array can be acessed with value like $value['name'].
Is it possbile like that ?..
Any help would be appreciated ..Thanks

$featured = array('name' => 12,'yeah' => 10);
foreach($featured as $key => $value){
echo $key; // outputs: name
echo " - ";
echo $value; // outputs: 12
echo "<br />";
}
Yes, it supports that in the next iteration of the loop.
Output:
name - 12
yeah - 10
BTW, one more way of accessing the keys from array.
$featured = array('name' => 12,'yeah' => 10);
while (current($featured)) {
echo key($featured).'<br />';
next($featured);
}
Output:
name
yeah

You most probably want to do:
echo "{$key} => {$value}";
The foreach($featured as $key => $value) statement iterates the array and for each iteration $key and $value contain both the key and value for the tuple.

Take a look at this:
http://php.net/array_search
it searches for the value and returns it's key.
It's not like accessing $array['value'] but it's still userfull if you want to find the key.

Related

PHP Print array values without all the extra "=>" stuff

I have an array and I want to simply print out a list of all the values. Not sure why I can't find the answer to this. I have tried "var_dump" and "Var_export", "TRUE" and "FALSE". Here is my code:
$var = var_export($xyz,TRUE);
print "$var";
But it outputs this:
array (
0 => '700',
1 => '750', etc
I just want this:
700
750
<?php
$a = array (700, 750);
foreach($a as $key => $value)
{
echo $value. "<br>";
}
?>
You can easily print the value of an array using the foreach loop. Here I gave an example for your better help.
foreach ($xyz as $val) {
echo $val . '<br>';
}
?
Have you tried iterating into $xyz using a foreach loop ?
foreach($xyz as $value) {
print "$value \n" }
The \n being a new line.
edit : identical to #PHPNoob's answer
Looks like you are attempting to get the values from an array.
Using a Foreach will loop through the array apply the code to each value until the array is complete.
foreach($xyz as $value){
echo $value . "<br/>";
}

Is there any special rule to use foreach and while loop(using each() function) to iterate over an array in php?

I am new in PHP.
My question is that when i use following script:
$arr1 = array('fname' => 'niraj','lname' => 'kaushal','city' => 'lucknow');
while(list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}
foreach($arr1 as $key => $value){
echo "$key:$value <br>";
}
it outputs
fname has niraj value
lname has kaushal value
city has lucknow value
fname:niraj
lname:kaushal
city:lucknow
but when i change order of foreach and while loop as follow
$arr1 = array('fname' => 'niraj','lname' => 'kaushal','city' => 'lucknow');
foreach($arr1 as $key => $value){
echo "$key:$value <br>";
}
while(list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}
it gives following output
fname:niraj
lname:kaushal
city:lucknow
Why second script doesn't display the output of while loop. What the reason behind it.
It is because each() only returns the current key/value and then advances the internal counter (where in the array you are currently). It does not reset it.
The first loop (foreach) sets the internal counter to the end of the array, so the second loop thinks it is already done and therefore does nothing.
You need to call reset() on the array before starting the loop using each():
reset($arr1);
while (list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}

Effective way to get array index and value names

I have a 0 indexed array that I can't do much about, but inside this array there are values that I need to echo. example array is:
$x = array(0 => array('store'=> 107));
I would like to have 2 variables that both echo texts store and 107
I could do this, using
$var1 = array_keys($x[0]);
$var2 = array_values($x[0]);
echo $var1[0]; // store
echo $var2[0]; // 107
I would like to know if there is a more effective way of getting those values, or remving that first 0 index. as array_filter($x) or unset($x) obviously don't work as in other cases.
As an alternative, you could also use combinations of key() and reset() if you're curious.
$x = array(0 => array('store'=> 107));
$y = reset($x); // point to first element
$key = key($y); // get the current key, store
$val = reset($y); // get the value
echo $key; // store
echo $val; // 107
this should work for you.
$x = array(0 => array('store'=> 107));
foreach($x as $y){
foreach ($y as $key => $value){
echo $key;
echo $value;
}
}

Can I access an associative array by numerical index?

I would like to know if it is possible to get the x-value position (ie 2nd) in a variable variable array reference.
the code below works for the 1st array but not the 2nd.
// WORKS FINE //
$my1stArray= array( 'red', 'green', 'blue');
$var_1st = 'my1stArray';
// for each lopp of var var works fine
echo " - my1stArray Values - <br>";
foreach ($$var_1st as $k => $v){
echo $k." : ".$v." <br>";
}
// direct access also works
echo "my1stArray 3rd value: ".${$var_1st}[2]."<br>";
// Not so good! //
$my2ndArray = array(
'color' => '#ff0000',
'face' => 'helvetica',
'size' => '+5',
);
$var_2nd = 'my2ndArray';
// for each lopp of var_2nd works fine
echo "<br> - my2ndArray Values - <br>";
foreach ($$var_2nd as $k => $v){
echo $k." : ".$v." <br>";
}
/** try to access 2nd value in array with position **/
echo "my2ndArray 2rd value: ".${$var_2nd[1]}[0]."<br>";
echo "my2ndArray 2rd value: ".${$var_2nd}[1][0]."<br>";
Well, in the comments I said "no, you can't", but there is actually a way. Here is an example (without variable variables):
$my2ndArray = array(
'color' => '#ff0000',
'face' => 'helvetica',
'size' => '+5',
);
$keys = array_keys($my2ndArray);
echo "my2ndArray 2nd value: " . $my2ndArray[$keys[1]] . "<br>";
Doesn't look very nice, but should work. Not that if you ever sort the array, the key indexes will change.
Another way to do that would be using a counter and a loop as I mentioned in the comments. But that would be even uglier...
Your last example in your code is not working for the same reason that the following code does not work:
$a = array('akey'=>'a','bkey'=>'b');
echo $a[0];
The reason is the key is set to a string and must be accessed as such. To fix my example I would need to change it to:
$a = array('akey'=>'a','bkey'=>'b');
echo $a['akey'];
To fix your example you need to change your last echo so that it references the key as a string:
echo "my2ndArray 2rd value: ".${$var_2nd}['color']."<br>";

Getting to the next key in an array

Let's assume I know that there is key "twoVal", but I do not know what is after it. How do I get to the next key for that matter? Shoud I know the position of key "twoVal"? Or there is another way around?
$arr = array('Cool Viski' => array('oneVal' => '169304',
'twoVal' => '166678',
'threeVal' => '45134'));
$keys = array_keys($arr['Cool Viski']);
$position = array_search('twoVal', $keys);
if (isset($keys[$position + 1])) {
$keyAfterTwoVal = $keys[$position + 1];
}
$arr = array('Cool Viski' => array('oneVal' => '169304',
'twoVal' => '166678',
'threeVal' => '45134'));
foreach($arr as $s=>$v){
foreach($v as $val){
if(key($v) == "twoVal"){
$t=next($v);
print "next key: ".key($v)."\n";
print "next key value is: ".$t."\n";;
}else{
next($v);
}
}
}
You might be interested in the various array seeking functions, but unless a PHP array is indexed only by integers there's no guarantee of order on the keys.

Categories