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

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

Related

Php explode loop up to value [duplicate]

This question already has an answer here:
unserialize data from mysql
(1 answer)
Closed 7 months ago.
I want to break the line I pulled from the database and list the ip addresses
Sql Table export
a:3{i:0;s:13:"213.74.219.18";i:1;s:13:"321.32.321.32";i:2;s:14:"321.315.212.55";}
$set=mysqli_query($con,"SELECT * FROM simple_stats_options where option='ignored_ips'");
$value = mysqli_fetch_array($set,MYSQLI_ASSOC);
function arasinial($str,$birinci,$ikinci,$i) {
$bolum = explode ($birinci,$str);
$bolum = explode ($ikinci,$bolum[$i]);
return $bolum[0];
}
$array = explode('/', $var);
foreach ($array as $values)
{
}
$metin=$value["value"];
for ($x = 1; $x <= 10; $x++) {
echo arasinial($metin,':"','";',$x)."<br>";
}
Your data is serialized data, so you can just use unserialize():
<?php
$r = 'a:3:{i:0;s:13:"213.74.219.18";i:1;s:13:"321.32.321.32";i:2;s:14:"321.315.212.55";}';
print_r(unserialize($r));
will output
Array
(
[0] => 213.74.219.18
[1] => 321.32.321.32
[2] => 321.315.212.55
)

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

Multiple array echo with foreach statement in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple index variables in PHP foreach loop
Can we echo multiple arrays using single foreach statement?
Tried doing it in following way but wasn't successful:
foreach($cars, $ages as $value1, $value2)
{
echo $value1.$value2;
}
assuming both arrays have the same amount of elements, this should work
foreach(array_combine($cars, $ages) as $car => $age){
echo $car.$age;
}
if the arrays are not guaranteed to be the same length then you can do something like this
$len = max(count($ages), count($cars));
for($i=0; $i<$len; $i++){
$car = isset($cars[$i]) ? $cars[$i] : '';
$age = isset($ages[$i]) ? $ages[$i] : '';
echo $car.$age;
}
if you just want to join the two arrays, you can do it like this
foreach(array_merge($cars, $ages) as $key => $value){
echo $key . $value;
}

Check how many times specific value in array PHP [duplicate]

This question already has answers here:
Count number of values in array with a given value
(8 answers)
Closed 3 years ago.
I have an array named $uid. How can I check to see how many times the value "12" is in my $uid array?
Several ways.
$cnt = count(array_filter($uid,function($a) {return $a==12;}));
or
$tmp = array_count_values($uid);
$cnt = $tmp[12];
or any number of other methods.
Use array_count_values(). For example,
$freqs = array_count_values($uid);
$freq_12 = $freqs['12'];
Very simple:
$uid= array(12,23,12,4,2,5,56);
$indexes = array_keys($uid, 12); //array(0, 1)
echo count($indexes);
Use the function array_count_values.
$uid_counts = array_count_values($uid);
$number_of_12s = $uid_counts[12];
there are different solution to this:
$count = count(array_filter($uid, function($x) { return $x==12;}));
or
array_reduce($uid, function($c, $v) { return $v + ($c == 12?1:0);},0)
or just a for loop
for($i=0, $last=count($uid), $count=0; $i<$last;$i++)
if ($uid[$i]==12) $count++;
or a foreach
$count=0;
foreach($uid as $current)
if ($current==12) $count++;
$repeated = array();
foreach($uid as $id){
if (!isset($repeated[$id])) $repeated[$id] = -1;
$repeated[$id]++;
}
which will result for example in
array(
12 => 2
14 => 1
)

Categories