Having Issues with nested arrays
I want to get the array title into the first cell of the table, having an issue articulated the nested foreach.
$content = file_get_contents("https://whattomine.com/asic.json");
$data = json_decode($content);
echo "coins";
foreach()
{
echo "<tr>";
echo "<th>".$coins->coinname."</th>";
echo "<th>".$coins->PoW."</th>";
echo "<th>".$coins->PoS."</th>";
echo "<th>".$coins->height."</th>";
echo "<th>".$coins->diff."</th>";
echo "<th>".$coins->supply."</th>";
echo "</tr>";
}
I've looked into the data and think I found what you want.
BUT you try to get values that are not in the data set.
The structure of that json is:
{"coins":
{"LitecoinCash":
{"id":231,"tag":"LCC",...}
},
...
}
So here's how to get that data:
$data = json_decode($content);
#var_dump($data);
echo "<table>";
foreach($data->coins as $title => $coin)
{
echo $title;
echo "<tr>";
echo "<th>".$title."</th>"; // the title as first cell
echo "<th>".$coin->id."</th>"; // added by me, coinname does not exist
echo "<th>".$coin->tag."</th>"; // added by me, PoW does not exist
#echo "<th>".$coin->PoS."</th>"; // all the others do not exist in the dataset.
#echo "<th>".$coin->height."</th>";
#echo "<th>".$coin->diff."</th>";
#echo "<th>".$coin->supply."</th>";
echo "</tr>";
}
echo "</table>";
// output:
// LitecoinCash 231 LCC
There is not keys you provided, but there is next keys:
example of item:
[LitecoinCash] => [
[id] => 231
[tag] => LCC
[algorithm] => SHA-256
[block_time] => 156.0
[block_reward] => 250
[block_reward24] => 250
[last_block] => 1460137
[difficulty] => 470292855.107
[difficulty24] => 396924068.28117
[nethash] => 12948028411711743
[exchange_rate] => 4.1E-6
[exchange_rate24] => 2.9135097493036E-6
[exchange_rate_vol] => 0.34130528
[exchange_rate_curr] => BTC
[market_cap] => $18,382,732.11
[estimated_rewards] => 149.54924
[estimated_rewards24] => 177.15706
[btc_revenue] => 0.00061315
[btc_revenue24] => 0.00072634
[profitability] => 102
[profitability24] => 121
[lagging] =>
[timestamp] => 1533048969
]
Which of them do you need?
Related
<?php
$StudScore = array(
"Mary" => array(
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"Tom" => array(
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
"Jon" => array(
"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
foreach ($StudScore as $key => $value) {
echo "<li>$key</li>";
if (is_array($value)) {
echo "<ul>";
echoArray($value);
echo "</ul>";
} else {
echo "<ul><li>$value</li></ul>";
}
}
?>
Till now i have tried to print array but i am getting below error kindly help me out with this .
Output :
Mary
Fatal error: Call to undefined function echoArray()
i want it to be print as example:
Mary = physics:35,maths:30,chemistry:39
Tom = physics:30,maths:32,chemistry:29
Jon = physics:31,maths:22,chemistry:39
It has to be either echo var_dump($value); or echo print_r($value); if you want to print the WHOLE array. There is no function called echoArray() in PHP.
When you only want to print ONE key/value from the array, you also have to use echo $value;, since the keys/values are only strings.
See https://www.php.net/manual/en/function.var-dump.php and https://www.php.net/manual/en/function.print-r.php
<?php
foreach ($StudScore as $name => $value) {
echo $name." = ";
foreach ($value as $subject => $marks) {
echo $subject.":".$marks.",";
}
echo "<br/>";
}
?>
Finally i got my answer it give output like :
Mary = physics:35,maths:30,chemistry:39
Tom = physics:30,maths:32,chemistry:29
Jon = physics:31,maths:22,chemistry:39
So I need to display items that were added on the Vend Page and are now in the cart array over onto the cart page in a list
Array
(
[cart] => Array
(
[BRB] => 1
)
)
Here is my code for the cart page that I'm working on:
if (isset($_SESSION['cart'])) {
foreach ($vend as $vendID => $items) {
//if (array_search($vendID, $_SESSION['cart'])) {
echo "<article class ='cart' id='cart-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "<div class ='img-div'>";
echo "<img src =../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
echo "</article>";
//}
}
}
I commented out code that I attempted but didn't work. So I need an IF statement which says, If the vendID is in the cart array display it in the list.
Some help is appreciated here as I'm stuck.
Your cart should be as follows:
$cart = array(
[0] => Array
(
[item_id] => 1,
[title] => "your title",
[price] => 100,
[img] => "name of the file",
),
[1] => Array
(
[item_id] => 2,
[title] => "your title",
[price] => 200,
[img] => "name of the file",
)
);
And Then Try as follows:
if (isset($_SESSION['cart'])) {
foreach ($cart as $key => $items) { //$key = [0], [1] ....
echo "<article class ='cart' id='cart-$key'>";
echo "<h1 class = 'item-h1' id = 'h1'>".$items['title']."</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>".$items['item_id']."</p></div>";
echo "<div class ='img-div'>";
echo "<img src =../images/".$items['img']." alt='' height='196' width='200'></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>".$items['price']."</p></div>";
echo "</article>";
}
}
Mark, it accepted if it works.
Did you try in_array() ?
It checks if a value exists in an array.
Please provide help on how to write array values if I have array like this:
Array (
[937245328] => $0.79
[310776983] => $0.53
[720315389] => $0.39
[310800933] => $0.30
[1011934667] => $0.28
[1576813623] => $0.21
[926978479] => $0.19
[1011934570] => $0.14
[937244096] => $0.14
[310777321] => $0.13
[384801319] => $0.13
[519987816] => $0.12
[992123310] => $0.11
)
I would like to print this array out somelike this:
937245328: $0.79
310776983: $0.53
720315389: $0.39
and so on... Thanks for any help.
if your array is called
$myArray
Then you can print it out as follows:
<?php
foreach ($myArray as $key => $value)
{
echo $key.": ".$value."<br>";
}
?>
Let me know if that worked for you! :)
You can print the array (the key and the value) using foreach loop.
<?php
$dolla = Array (
'937245328' => '$0.79',
'310776983' => '$0.53',
'720315389' => '$0.39'
);
foreach($dolla as $key => $value) {
echo $key.": ".$value."<br>";
}
?>
Hello i just finish a code where i get like 50 variables... all of them with int values..
I have the variables as separete values, just for this example I will set the variables with the result, BUT the result came from other evaluations and stuff that its fine, cause im already echoing a verified result.
$one = 13
$two = 35
$three = 46
The "item1" appears <?PHP echo $one; ?> times<br />
The "item2" appears <?PHP echo $two; ?> times<br />
The "item3" appears <?PHP echo $three; ?> times<br />
This is fine but,, How can i order the results, in ASC way or DSC , to build a order by...
Thanks so much
This far this is working great
$naturales = array(
$uno => "n1",
$dos => "n2",
$tres => "n3",
$cuatro => "n4",
$cinco => "n5",
$seis => "n6",
$siete => "n7",
$ocho => "n8",
$nueve => "n9",
$diez => "n10",
$once => "n11",
$doce => "n12",
$trece => "n13",
$catorce => "n14",
$quince => "n15",
$dieciseis => "n16",
$diecisiete => "n17",
$dieciocho => "n18",
$diecinueve => "n19",
$veinte => "n20",
$veintiuno => "n21",
$veintidos => "n22",
$veintitres => "n23",
$veinticuatro => "n24",
$veinticinco => "n25",
$veintiseis => "n26",
$veintisiete => "n27",
$veintiocho => "n28",
$veintinueve => "n29",
$treinta => "n30",
$treintayuno => "n31",
$treintaydos => "n32",
$treintaytres => "n33",
$treintaycuatro => "n34",
$treintaycinco => "n35",
$treintayseis => "n36",
$treintaysiete => "n37",
$treintayocho => "n38",
$treintaynueve => "n39",
$cuarenta => "n40",
$cuarentayuno => "n41",
$cuarentaydos => "n42",
$cuarentaytres => "n43",
$cuarentaycuatro => "n44",
$cuarentaycinco => "n45",
$cuarentayseis => "n46",
$cuarentaysiete => "n47",
$cuarentayocho => "n48",
$cuarentaynueve => "n49",
$cincuenta => "n50",
$cincuentayuno => "n51",
$cincuentaydos => "n52",
$cincuentaytres => "n53",
$cincuentaycuatro => "n54",
$cincuentaycinco => "n55",
$cincuentayseis => "n56",
);
krsort($naturales);
foreach ($naturales as $count => $name) {
echo "The \"$name\" appears $count times<br />";
}
Why my results are like this (Its hidding all the results with 12 (Similar count results)
for example for "n3" appears 12 times. and its not listed.
The "n20" appears 12 times
The "n30" appears 11 times
The "n37" appears 10 times
The "n41" appears 9 times
The "n42" appears 8 times
The "n45" appears 7 times
The "n47" appears 6 times
The "n35" appears 5 times
The "n44" appears 4 times
The "n46" appears 2 times
The "n56" appears 0 times
Build an array
$myresults = array("Item1"=>13,"item2"=>35,"item3"=>46);
then use asort() or arsort() on the array $myresults
then do a for/foreach loop to output the results
basic guidelines but off this you should be able to google how to implement in detail fairly easily (even on here will work)
$one = 13;
$two = 35;
$three = 46;
$arr = array("Item 1"=>$one,"Item 2"=>$two,"Item 3"=>$three);
echo "<strong>Original</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
asort($arr);
echo "<strong>Ascending Sort</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
arsort($arr);
echo "<strong>Descending Sort</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
As previously mentioned, you can use asort and arsort to sort your array as needed... I'm adding some examples here as well as some working CODE
As mentioned, you could insert your values into an associative array, i.e.:
$items = array(
$one => "item1",
$two => "item2",
$three => "item3"
);
and then you can use a function like ksort() to sort all of your values:
http://php.net/manual/en/function.ksort.php
so you can end up with something like this:
ksort($items);
foreach ($items as $count => $name) {
echo "The \"$name\" appears $count times<br />";
}
I have an array that looks like this:
$rowarray(
[0] => [PID] => 97162 [TID] => 340 [StatsID] => 49678
[1] => [PID] => 97165 [TID] => 340 [StatsID] => 49673
[2] => [PID] => 97167 [TID] => 340 [StatsID] => 49675
[3] => [PID] => 97162 [TID] => 340 [StatsID] => 49679
)
Then my code looks like this:
$cntr=0;
foreach($rowarray as $row)
{
echo "<tr><td>$row[PID] $row[TID] $row[StatsID] </td></tr>";
$cntr++;
}
Two things I want to do I want to be able not print the duplicates in the array but print the additional column that has a different value. So my desired output would look like this.
97162 340 49678 49679
97165 340 49673
97167 340 49675
I started out with the array_unique() but that only returned:
97162 340 49678
Assuming only the StatsID changes (not clear from the question)
$map = array();
foreach($rowarray as $row){
$k = $row["PID"] . '-' . $row["TID"];
if( !isset( $map[$k] ) ){
$map[$k] = array();
}
array_push( $map[$k], $row["StatsId"] );
}
foreach($map as $k=>$v){
$row = explode( '-', $k );
echo "<tr><td>$row[0] $row[1] " . implode( " ", $v ) . " </td></tr>";
}
Here's what I'd do:
Start by sorting the array (using usort to sort by PID, then by TID)
Initialize "last" variables ($last_PID and $last_TID). They will store the respective values in the loop
In the loop, first compare the "current" variables to the "last" ones, if they're the same then just echo the StatsID value.
If they're not the same, output the <tr> (but not the final </tr>, so the first part of the loop can add more StatsID values if necessary)
Still inside the loop, after outputting everything, update the "last" variables.
After the loop, output the final </tr>
This may not be optimal, but I'm pretty sure it'll work.
Transfer the $rowarray structure into a map of maps of arrays, like this:
$rowarray = array(
array('PID' => 97162, 'TID' => 340, 'StatsID' => 49678),
array('PID' => 97165, 'TID' => 340, 'StatsID' => 49673),
array('PID' => 97167, 'TID' => 340, 'StatsID' => 49675),
array('PID' => 97162, 'TID' => 340, 'StatsID' => 49679)
);
$keys = array();
foreach ($rowarray as $row) {
if (!is_array(#$keys[$row['TID']])) {
$keys[$rowarray['TID']] = array();
}
if (!is_array(#$keys[$row['TID']][$row['PID']])) {
$keys[$row['TID']][$row['PID']] = array();
}
$keys[$row['TID']][$row['PID']][] = $row['StatsID'];
}
foreach ($keys as $pid => $pid_arr) {
foreach ($pid_arr as $tid => $tid_arr) {
echo "<tr><td>$tid $pid " . implode(' ', $tid_arr) . "</td></tr>";
}
}
See this code in action
As far as I can tell, the only way to do this would be to loop through the array creating a new unique array as you go.
$unique = array();
foreach ($row as $value)
{
$key = $value['PID'];
if (isset($unique[$key]))
{
$unique[$key]['StatsID'] .= ' ' . $value['StatsID'];
}
else
{
$unique[$key] = $value;
}
}
Now, $unique would give you the results you're looking for and you can loop through the unique array and output your results (I also added your counter if needed):
$count = count($unique);
foreach ($unique as $row)
{
echo "<tr><td>{$row['PID']} {$row['TID']} {$row['StatsID']} </td></tr>";
}