I have fetched json data from server and decode using json_decode(value, true);
using print_r function I got this array as output
Array
(
[id] => 120
[key] => 7ca04960a36dbb7f4b7c8607bb3
[num] => 0
)
Array
(
[id] => 121
[key] => 7ca04960a36dccgki49g6dfg57
[num] => 0
)
I want to display decoded data using while loop.
while($row = mysqli_fetch_assoc($result)) {
$json = json_decode($row['post_req'], true);
echo "<pre>";
print_r($json);
echo "</pre>";
}
ex:
id = 120,
key = 7ca04960a36dbb7f4b7c8607bb3,
num = 0
how can I print data ?
Edited: It looks like you changed some details.
It looks like you are getting multiple arrays
$output = array();
while($row = mysqli_fetch_assoc($result)) {
$json = json_decode($row['post_req'], true);
$ouput[] = $json;
}
This will give you a multi-layered array and if you just want the id you can do this
for ($i = 0; $i < sizeof($output); $i++) {
echo "id =" . $output[$i]['id'] . " <br/>";
}
or you can use this to get id only
for ($i = 0; $i < sizeof($output); $i++) {
foreach ( $output[$i] as $key => $value ) {
if ('id' === $key) {
echo "$key = $value <br />";
}
}
}
The expected output is
id = 120
id = 121
If you want all three values use this
for ($i = 0; $i < sizeof($output); $i++) {
foreach ( $output[$i] as $key => $value ) {
echo "$key = $value <br />";
}
}
Expected output
id = 120
key = 7ca04960a36dbb7f4b7c8607bb3
num = 0
id = 121
key = 7ca04960a36dccgki49g6dfg57
num = 0
foreach($json as $key => $value) {
echo "$key: $value \n";
}
Related
I am confused to count these words,
I've some data like this :
web = 1
sistem=1
web=1
sistem=1
web=1
sistem=1
sistem=0
sistem=0
web=0
sistem=0
web=0
sistem=0
web=0
web=0
I want to make result like this :
web = 3
sistem = 3
I'm using array_count_values(), but this result is not good
Array ( [web=1] => 3 [sistem=1] => 3 [sistem=0] => 4 [web=0] => 4 )
My code like this :
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
echo $kata . $ada . "<br>";
$p[] = $kata . "=" . $ada;
// print_r($p);
echo "<br><br>";
} else {
echo $kata, $tidak . "<br>";
$q[] = $kata . "=" . $tidak;
// $m = explode(" ", $q);
// print_r($q);
// echo $q . '<br>';
echo "<br><br>";
}
}
$s = array_merge($p, $q);
echo "<br><br>";
print_r($s);
echo "<br>";
$f = array_count_values($s);
// print_r($f);
echo "<br><br>";
thank you very much if you are willing to help me
RESULT THIS CODE
Another simple way is use a counter like that:
$web=0;
$sistem=0;
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
$sistem=$sistem + $ada;
} else {
$web=$web+$tidak
}
}
echo 'web='.$web.'<br> sistem='.$sistem;
First, you need to separate word and value.
Second, you need to check the value : if it's zero you let it go (can't hold it back anymore). Else you count the value ; if it's written, i suppose it can be greater than 1 ; if it's not, it should be "word", or nothing (which would greatly facilitate counting).
Something like
<?php
$tab = [
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'sistem=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'web=0',
];
$tab_clean = [];
foreach($tab as $item) {
preg_match('/([a-z]+)=([\d]+)/', $item, $matches);
//print_r($matches);
$word = $matches[1];
$number = $matches[2];
for($i = 0; $i < $number; $i++) {
$tab_clean[] = $word;
}
}
$tab_count = array_count_values($tab_clean);
print_r($tab_count);
?>
I have a function to calculate distance, the data comes from a database.
Here is the code for calculating:
function jarak() {
global $conn;
$query1 = mysqli_query($conn, "SELECT signature, sig_priority FROM centro");
$query2 = mysqli_query($conn, "SELECT signature, sig_priority, status FROM acid_event");
while ($row = mysqli_fetch_array($query1, MYSQLI_ASSOC)) { $master[]=$row; }
while ($row = mysqli_fetch_array($query2, MYSQLI_ASSOC)) { $data[]=$row; }
$jarak = array();
foreach ($data as $key => $val) {
foreach ($master as $key2 => $value) {
$jarak = sprintf("%0.2f",sqrt(pow($val['signature'] - $value['signature'], 2) + pow($val['sig_priority'] - $value['sig_priority'], 2)));
echo "distance from (" . $value['signature'] . "," . $value['sig_priority'] . ") ke (" . $val['signature'] . "," . $val['sig_priority'] . ") is : " . $jarak . "<br>";
$euc[]=$jarak;
}
}
}
And here is the result from that:
Array(
[0] => 30.04
[1] => 0.00
[2] => 30.04
[3] => 0.00
[4] => 47.00
[5] => 17.03
[6] => 5.02
[7] => 25.08
[8] => 2.06
[9] => 32.06
[10] => 37.00
[11] => 7.07 )
I want to compare each 2 index array with greater than or less than.
Example : [0] with [1], [2] with [3], [4] with [5] and so on. It just compare with 2 index.
I tried this but no result
for ($i=0; $i<count($cb); $i++) {
for ($k=0;$k<2;$k++) {
if($cb[$i][$k]<$cb[$i][$k]) {
echo "low";
} elseif ($cb[$i][$k]>$cb[$i][$k]) {
echo "high";
}
}
}
Output I want should look like this
if [0] < [1] then "high" and it loop for other index array like [2] with [3], [4] with [5] and so on.
I think you were quite close to what you wanted to accomplish but it seems like you were making things harder than they needed to be.
Your code is below.
for ($i=0; $i<count($cb); $i++) {
for ($k=0;$k<2;$k++) {
if($cb[$i][$k]<$cb[$i][$k]) {
echo "low";
} elseif ($cb[$i][$k]>$cb[$i][$k]) {
echo "high";
}
}
}
As you can see your if statements are comparing the exact same value to each other, that's not going to do much. But I can see what you were trying to do in the second for loop.
Instead, what we really want to do is move through your array in steps of 2.
for ($i=0; $i<count($cb); $i+=2) {
//stuff
}
This way we can compare the first element and the element after that to each other. Like this:
if($cb[$i] > $cb[$i+1]) {
echo $i . 'is higher than '. $i+1;
} elseif($cb[$i] < $cb[$i+1]) {
echo $i . 'is lower than '. $i+1;
} else {
echo $i . 'is the same as '. $i+1;
}
So all together it would be something like this:
for ($i=0; $i<count($cb); $i+=2) {
if($cb[$i] > $cb[$i+1]) {
echo $i . 'is higher than '. $i+1;
} elseif($cb[$i] < $cb[$i+1]) {
echo $i . 'is lower than '. $i+1;
} else {
echo $i . 'is the same as '. $i+1;
}
}
Now you can change the echo's to whatever you want to do and you should probably add some validation too (like checking if the keys actually exist before accessing them), but this is a good place to get started.
I did a solution, but I decided to keep the response in an array because it makes more sense to me to be able to read this information somewhere else later.
Here's my solution:
$test = array(
30.04,
0.00,
30.04,
0.00,
47.00,
17.03,
5.02,
25.08,
2.06,
32.06,
37.00,
7.07,
);
$output = array();
foreach ($test as $key => $value) {
// To make sure this will only be executed every second item
if ($key % 2 !== 0) {
continue;
}
$next = '';
if (!isset($test[$key+1])) {
break;
} else {
$next = $test[$key+1];
}
$output[$key] = $value . ' is ' . ($value < $next
? "lower"
: ($value > $next
? 'higher'
: 'equal')) . ' than ' . $next;
}
echo '<pre>';
print_r($output);
Here's the code tested: https://3v4l.org/Pg5La
I'm trying to rank a group of scores. So I've taken the input from a users form and sorted them from largest to smallest in a multidimensional array, so then that I can search the array for the user and retrieve the index to get its "rank" however I appear to be doing something totally wrong! any help on where I'm going wrong would be greatly appreciated, or if there is a better way entirely!
Thank you
PHP Code:
if ( isset( $_POST['submit'] ) ) {
$size = $num_rows;
$p = 0;
$myarray = array();
while ( $p < $size ) {
$myarray[] = array( "user" => $user[$p], "data" => $scanrate[$p] );
$p++;
}
$sort = array();
foreach ( $myarray as $k => $v ) {
$sort['data'][$k] = $v['data'];
}
array_multisort( $sort['data'], SORT_DESC, $myarray );
for ( $i = 0; $i < $num_rows; $i++ ) {
$key = array_search( $i, array_column( $myarray, 'user' ) );
if ( !$key ) {
$key = $num_rows;
}
//this is stored into my database
echo "<br>User " . $user[$i] . " scan rate " . $scanrate[$i] . " rank " . $key;
}
}
You do useless code to do this sort, use the usort() function like :
while($p < $size)
{
$myarray[] = array("user" => $user[$p], "data" => $scanrate[$p]);
$p++;
}
usort($myarray, function($a, $b) {
return $a['data'] - $b['data'];
});
Now your $myarray is sorted and you can search and do what you want.
If you're putting too much effort on smt. with a scripting language, you're definitely doing it wrong...
$sort = array_combine($user, $scanrate);
asort($sort);
// $sort is sorted now, used it as you like:
// List them
foreach ($sort as $user => $scanrate) {
echo "<br>User " . $user . " scan rate " . $scanrate . " rank " . ++$i;
}
// Find the rank of specific user:
echo array_search('matt', array_keys($sort)) + 1;
I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}
This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";