Change position of key of an array [duplicate] - php

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.....

Related

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++;
}
}

Create groups of values from array values [duplicate]

This question already has answers here:
Finding the subsets of an array in PHP
(5 answers)
Closed 7 years ago.
I will do my best to explain this idea to you. I have an array of values, i would like to know if there is a way to create another array of combined values. So, for example:
If i have this code:
array('ec','mp','ba');
I would like to be able to output something like this:
'ec,mp', 'ec,ba', 'mp,ba', 'ec,mp,ba'
Is this possible? Ideally i don't want to have duplicate entries like 'ec,mp' and 'mp,ec' though, as they would be the same thing
You can take an arbitrary decision to always put the "lower" string first. Once you made this decision, it's just a straight-up nested loop:
$arr = array('ec','mp','ba');
$result = array();
foreach ($arr as $s1) {
foreach ($arr as $s2) {
if ($s1 < $s2) {
$result[] = array($s1, $s2);
}
}
}
You can do it as follows:
$arr = array('ec','mp','ba', 'ds', 'sd', 'ad');
$newArr = array();
foreach($arr as $key=>$val) {
if($key % 2 == 0) {
$newArr[] = $val;
} else {
$newArr[floor($key/2)] = $newArr[floor($key/2)] . ',' . $val;
}
}
print_r($newArr);
And the result is:
Array
(
[0] => ec,mp
[1] => ba,ds
[2] => sd,ad
)
Have you looked at the function implode
<?php
$array = array('ec','mp','ba');
$comma_separated = implode(",", $array);
echo $comma_separated; // ec,mp,ba
?>
You could use this as a base for your program and what you are trying to achieve.

PHP use current function in foreach [duplicate]

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;
}

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 ...);

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