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
)
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have result
{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}
I need to explode it on two arrays:
$id = 6583879;
and
$listid = 11745/3470/OMS
I'd like to avoid counting characters, this response may be another in future. I was thinking about taking:
everything between "id": and comma
everything between "listingId":" and "
PHP code demo
<?php
$json='{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}';
$array= json_decode($json,true);
extract($array["data"]);
$first=array("id"=>$id);
$second=array("listingId"=>$listingId);
print_r($first);
print_r($second);
Output:
Array
(
[id] => 6583879
)
Array
(
[listingId] => 11745/3470/OMS
)
just decode the json data
$data = '{"success":true,"data":[{"id":6583879,"listingId":"11745/3470/OMS"}]}';
$arr = json_decode($data);
$id = array();
$listing = array();
for($i=0; $i < count($arr->data); $i++){
$id[$i] = $arr->data[$i]->id;
$listing[$i] = $arr->data[$i]->listingId;
}
//loop array to view data
foreach($id as $value){
echo $value.'<br>';
}
?>
<?php
$json='{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}';
$decoded = json_decode($json);
$id = $decoded['data']['id'];
$listingId = $decoded['data']['listingId'];
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.
This question already has answers here:
Multiply each integer in a flat array by 60
(5 answers)
Closed 9 years ago.
i want to mutiply all the values in a foreach array by itself
here is an example of the array
Array ( [0] => 2.4 [1] => 6 )
, all i want to do is mutiply 2.4*6
foreach($pp_events as $key=>$value)
{
#echo $value;
$pick=mysql_query("select * from pick where eventid='$value'");
$row=mysql_fetch_array($pick);
$sum*=$row['startPrice'];
}
There's no need to implement this yourself. PHP has a built-in function for multiplying all the values in an array - array_product(). Use that instead:
$products = array();
foreach ($pp_events as $key => $value) {
$pick = mysql_query("select * from pick where eventid='$value'");
$row = mysql_fetch_array($pick);
$products[] = $row['startPrice'];
}
echo array_product($products);
See also:
Why shouldn't I use mysql_* functions in PHP?
You need to initialize the variable to 1, then your loop should work.
$product = 1;
foreach ($pp_events as $key => $value) {
$pick = mysql_query("select * from pick where eventid='$value'");
$row = mysql_fetch_array($pick);
$product *= $row['startPrice'];
}
echo $product;
You can also do a single query, instead of a separate query for each value:
$events = implode(',', array_map(function($x) { return "'$x'"; }, $pp_events));
$pick = mysql_query("select startPrice from pick where eventid IN ($events)");
$product = 1;
while ($row = mysql_fetch_assoc($pick)) {
$product = $row['startPrice'];
}
echo $product;
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
)
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 >";