PHP use current function in foreach [duplicate] - php

This question already has answers here:
How does PHP 'foreach' actually work?
(7 answers)
Closed 8 years ago.
$arr = array(1,2,3,4,5);
foreach($arr as $key => $row) {
echo current($arr);
}
//output is 22222, why?
Why the result is not 12345?

if you want the output to be 12345:
$arr = array(1,2,3,4,5);
foreach($arr as $key => $val) {
echo $val;
}

Related

php adressing variables difference between &$value vs $value [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
What is exactly the difference in php between &$Value and $value in a foreach loop ?
And how it works?
In the example below print_r ($arr) will return the array modified on the first loop and unmodified on the second one.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $key => &$value) {
$value = $value * 2;
echo "$key => $value; ";
print_r ($arr);
echo '<br>';
}
unset ($value);
unset ($key);
echo '<br>Second loop without "&" on value <br>';
foreach ($arr as $key => $value) {
$value = $value * 2;
echo "$key => $value; " ;
print_r($arr);
echo '<br>';
}
?>
I now it's a beginner question because I'm one :)
Pass the value by-reference instead of by-value. Variables passed by reference (using the reference operator '&') can have their values changed inside of functions.
For example, see the examples here

How to sorting array value by key? [duplicate]

This question already has answers here:
Custom key-sort a flat associative based on another array
(16 answers)
Closed 7 years ago.
I have array like this
<?php
$sliders=array(
1=>array('url'=>"url1.com",'image'=>"img1.jpg"),
2=>array('url'=>"url2.com",'image'=>"img2.jpg"),
3=>array('url'=>"url3.com",'image'=>"img3.jpg"),
4=>array('url'=>"url4.com",'image'=>"img4.jpg"),
5=>array('url'=>"url5.com",'image'=>"img5.jpg")
);
foreach($sliders as $sKey=>$sVal)
{
echo $sKey.'=>'.$sVal['url'].' image=>'.$sVal['image'].'<br>';
}
?>
And my sorting key is
$sort[]='2,4,5,3,1';
And I want result like this.
array(
1=>array('url'=>"url2.com",'image'=>"img2.jpg"),
2=>array('url'=>"url4.com",'image'=>"img4.jpg"),
3=>array('url'=>"url5.com",'image'=>"img5.jpg"),
4=>array('url'=>"url3.com",'image'=>"img3.jpg"),
5=>array('url'=>"url1.com",'image'=>"img1.jpg"));
How can I sorting Array like this?
Thanks.
I cannot believe that you did not find this yourself...
<?php
$newArray = array();
$sortArray = explode(',', $sort[0]);
$i = 1;
foreach ($sortArray as $s) {
if (isset($sliders[$s])) {
$newArray[$i] = $sliders[$s];
$i++;
}
}

can we use foreach() for two different arrays at once? [duplicate]

This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 8 years ago.
I am facing problem when I am trying to insert values from array to mysql database.
foreach ( $_POST['product_id'] as $key=>$value AND $_POST['discount'] as $key1=>$discount) { }
check the above given code where I am going wrong?
You can use a regular for loop as long as the indexes match:
$count = count($_POST['product_id']);
for($i = 0; $i < $count; $i++) {
echo $_POST['product_id'][$i];
echo $_POST['discount'][$i];
}
use array_map this will loop through all keys in all arrays provided simultaneously.
array_map(function(){
$args = func_get_args();
foreach($args as $k => $v) {
echo $v;
}
}, $arr1, $arr2 ...);

Change position of key of an array [duplicate]

This question already has answers here:
Move Value in PHP Array to the Beginning of the Array
(12 answers)
Closed 8 years ago.
The following code will print "com, net, org, biz".
I want to print "net, com, org, biz".
Basically, to set the first array using the $chosen variable.
<?php
$array = array('com', 'net', 'org', 'biz');
$chosen = 'net';
$newArray = array();
foreach($array as $value) {
$newArray = $value;
}
print_r($newArray);
?>
<?php
$array = array('com', 'net', 'org', 'biz');
foreach($array as $value)
{
print_r($value);
}
?>
It simply all the itme of the $arry contains.....

How to get position in a php associative array? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - get numeric index of associative array
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
//echo $position ( 1,2 )
}
Can I get the position in the array with a simple function ?
Try:
$i = 0;
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
$i++;
echo $i;
}
$array = array('a'=>'a', 'b'=>'b');
for ($x = 0; $x < count($array);$x++)
echo $x."<br >";

Categories