can not get an element from an array - php

I'm trying to get a list of values for the Manufacturer field, but I do not get anything other than a white screen, no bugs, everything seems to be right, but nothing works
<?php
function removeBomUtf8($s){
if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
return substr($s,3);
}else{
return $s;
}
}
$url = "http://www.pureexample.com/backend/data/car-sale.json";
$content = file_get_contents($url);
$clean_content = removeBomUtf8($content);
$decoded = json_decode($clean_content);
while ($el_name = current($decoded)) {
if ($el_name == 'Manufacturer') {
echo key($decoded).'<br />';
}
next($decoded);
}
?>

$decoded is an array of objects (type StdClass): when you call current you are returning that object which has three properties: Manufacturer, Sold and Month. If you just want to print out the Manufacturer name, edit your code as follows:
while ($el_name = current($decoded)) {
echo $el_name->Manufacturer . '<br>';
next($decoded);
}
However, as one of the commenters mentioned, the current/next syntax is pretty obscure, and not as easy to follow. You'd be better off writing:
foreach($decoded as $el_name) {
echo $el_name->Manufacturer . '<br>';
}

Each entry in the $decoded array should be a stdclass object representation of something like
{
"Manufacturer": "Toyota",
"Sold": 1200,
"Month": "2012-11"
}
Going from your comment...
i expect to see all rows with Manufacturer part like 1 "Manufacturer": "Toyota", 2 "Manufacturer": "Ford"3 "Manufacturer": "BMW" etc
what you're probably after is
$manufacturers = array_map(function($el) {
return sprintf('"Manufacturer": "%s"', $el->Manufacturer);
}, $decoded);
echo '<ol><li>', implode('</li><li>', $manufacturers), '</li></ol>';
Demo ~ https://eval.in/866131
Alternatively, just loop over $decoded with a foreach...
?>
<ol>
<?php foreach ($decoded as $el) : ?>
<li>"Manufacturer": "<?= htmlspecialchars($el->Manufacturer) ?>"</li>
<?php endforeach ?>
</ol>

You are dealing with an Object, and to make it more simple, try running this to get the results you said you wanted:
<?php
function removeBomUtf8($s){
if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
return substr($s,3);
}else{
return $s;
}
}
$url = "http://www.pureexample.com/backend/data/car-sale.json";
$content = file_get_contents($url);
$clean_content = removeBomUtf8($content);
$decoded = json_decode($clean_content);
foreach ($decoded as $car) {
echo "Manufacturer is: $car->Manufacturer" . "<BR>";
echo "Sold is: $car->Sold" . "<BR>";
echo "Month is: $car->Month" . "<BR>";
echo "<P>";
}
?>
Here is the output from the PHP code above:
Manufacturer is: Toyota
Sold is: 1200
Month is: 2012-11
Manufacturer is: Ford
Sold is: 1100
Month is: 2012-11
Manufacturer is: BMW
Sold is: 900
Month is: 2012-11
Manufacturer is: Benz
Sold is: 600
Month is: 2012-11
Manufacturer is: GMC
Sold is: 500
Month is: 2012-11
Manufacturer is: HUMMER
Sold is: 120
Month is: 2012-11

Related

How to count total entries in an array, and display the sum total in PHP

I have some code that processes an array of values, each one with a value 1 or 0.
<?php
$GD_album = [
[$gdclock,1],
[$gdheart,1],
[$gdblue,1],
[$gdblack5th,0],
[$gdgreymarble,1],
[$gdblack7th,0]
];
$GD[have] = '';
foreach($GD_album as list($name, $own)) {if ($own === 1)echo $name;}
echo $GD[have];
?>
Each of my $gd*** variables draws a block of PHP and HTML code, and the foreach I have above will only draw the ones with a "1", and skip the ones with a 0. This works great.
What I'd like to do is have a piece of code that will add up each variable with a value of 1; for a total of 4. I'd also like some code to add up 0; for a total of 2. Then I would like to add those two results together for a total of 6.
I have tried the count() and array_sum() functions with little success. What I tried that gave me some results is:
<?php
$GD[h_total] = '';
foreach($GD_album as list($name, $own))
{if ($own === 1) echo count($own);}
echo $GD[h_total];
?>
<?php
$GD[w_total] = '';
foreach($GD_album as list($name, $own))
{if ($own === 0) echo count($own);}
echo $GD[w_total];
?>
This however, only outputs: "1111" and not "4". And "00", not "2", respectively. Ideally, some code that could count my 0's & 1's, also add them, and stylize as "Have 4/6" or "Need 2/6" would be best! Can anyone point me in the right direction? Thanks!
Try the code below:
<?php
$GD_album = [
['gdclock',1],
['gdheart',1],
['gdblue',1],
['gdblack5th',0],
['gdgreymarble',1],
['gdblack7th',0]
];
$GD['h_total'] = [];
foreach($GD_album as list($name, $own)){
if(isset($GD['h_total'][$own])){
$GD['h_total'][$own]++;
}else{
$GD['h_total'][$own] = 1;
}
}
echo 'total zeros: '. $GD['h_total'][0];
echo 'total ones: '. $GD['h_total'][1];
echo 'total: ' .array_sum($GD['h_total']);
The output:
total zeros: 2
total ones: 4
total: 6

Calculating Average (Mean) in PHP

I'm a bit of a beginner with PHP and am implementing a review aggregator system for a few products.
I have created the input fields and am outputting the results from these fields using this code:
{ echo '<div class="review1">Review 1: '; the_field('review1'); '</div>';}
{ echo '<div class="review2">Review 2: '; the_field('review2'); '</div>';}
{ echo '<div class="review3">Review 3: '; the_field('review3'); '</div>';}
{ echo '<div class="review4">Review 4: '; the_field('review4'); '</div>';}
{ echo '<div class="review5">Review 5: '; the_field('review5'); '</div>';}
I want to use PHP to calculate the average (mean) however the number I am using to calculate this is set to 5 as that is the total number of number fields I have. Here is the code I am using
{ echo (get_field('review1')+get_field('review2')+get_field('review3')+get_field('review4')+get_field('review5'))/5;}
The problem with this method is that sometimes the fields will not contain a value so the number to divide by would need to be 1, 2, 3 or 4 instead of 5 depending on the total number of review fields that have a value.
Essentially I need to replace "/5" with "/n" where "n" is the total number of fields with values.
Can anyone please assist?
Regards,
Peter
I would put the values into an array, then filter out non-numeric values, and then do the calculation of the average:
$array = [ 123, 45, null, 17, 236 ];
// $array = [ get_field('review1'), get_field('review2'), etc. ]
$values = array_filter($array, 'is_numeric');
$result = array_sum($values) / count($values);
echo $result; // Output: 105.25
$items = ['1','2','7','',''];
$average = calculateAverage($items);
echo $average;
function calculateAverage($items)
{
$total = 0;
$count = 0;
foreach($items as $item)
{
if(is_numeric($item))
{
$total += $item;
$count++;
}
}
return $total / $count;
}
If the number is empty, it will not add the number to the devider

similar_text() to compare product names to a given string

I am currently trying to play about with some PHP that will compare an array of words/phrases with a user provided word and then return just the word which has the highest percentage..
My code so far is (for the sake of testing):
<?php
$CRProductName = strtoupper("Product 30");
$XProdNames = array("Product 1","Product 2", "Product 300", "Not a product");
echo "Checking product matches for: ".$CRProductName."<br /><br />";
foreach ($XProdNames as $ProductName) {
similar_text($CRProductName,strtoupper($ProductName), $p);
echo "Percentage:".$p."%<br />";
}
?>
This outputs the following:
Checking product matches for: PRODUCT 30
Percentage:84.2105263158%
Percentage:84.2105263158%
Percentage:95.2380952381%
Percentage:60.8695652174%
Which is great and it works, however I would just like it to return the product name with the highest percentage in the results?
Can anyone advise on a good route for me to take?
I tried adding an IF statement to check the value of $p, but the highest percentage may differ every time.
I converted all to uppercase just to make sure it is marking the similarity by content and not by case.
Thanks,
<?php
$CRProductName = strtoupper("Product 30");
$XProdNames = array("Product 1","Product 2", "Product 300", "Not a product");
echo "Checking product matches for: ".$CRProductName."<br /><br />";
$bestMatch = array('score' => 0, 'name' => 'None');
foreach ($XProdNames as $ProductName) {
$p = 0;
similar_text($CRProductName,strtoupper($ProductName), $p);
echo "Percentage:".$p."%<br />";
if($p > $bestMatch['score'])
{
$bestMatch = array('score' => $p, 'name' => $ProductName);
}
}
print_r($bestMatch);
?>
you could always run simlar_text a few times in each loop and average the results as well if you're getting fluxing results.
It's very simple. Just use an if statement to check for the highest value in the foreach loop. then echo the highest value. Here's your code with the minor change:
<?php
$CRProductName = strtoupper("Product 30");
$XProdNames = array("Product 1","Product 2", "Product 300", "Not a product");
echo "Checking product matches for: ".$CRProductName."<br /><br />";
$pHighest = 0;
foreach ($XProdNames as $ProductName) {
similar_text($CRProductName,strtoupper($ProductName), $p);
if ($p > $pHighest) {
$pHighest = $p;
}
}
echo "Percentage:".$pHighest."%<br />";
?>

Assigning variable on each result using foreach loop in PHP

My current code is as follows
foreach($flowers as $flower){
echo "Order by: " . $flower->cust_name . "with item: " . $flower->item;
// code result above
// Order by: Anna Witck with item Lily
// Order by: George Maxwel with item Rose
// Order by: Catherine Giin with item Aster
// and so on..
}
But I want it to update it so that each line result have different (two) variable like as follows.
$anna_cust = $flower->cust_name; // result: Anna Witck
$anna_item = $flower->item; // result: Lily
$george_cust = $flower->cust_name; // result: George Maxwel
$george_item = $flower->item; // result: Rose
$catherine_cust = $flower->cust_name; // result: Catherine Giin
$catherine_item = $flower->item; // result: Aster
So if it's an array and not an object, you'd access keys like this:
$name = $flower['cust_name'];
You are trying to show object data like -> symbol but $flower is an array so try the following
<?php
foreach($flowers as $flower){
echo "Order by: " . $flower['cust_name'] . "with item: " .$flower['item'];
}
?>
Like this
$data = array();
foreach ($flowers as $flower) {
$name = explode(' ', trim($flower->cust_name));
// Set varibale name
$var_cust = strtolower($name[0]) . '_cust' ;
$var_item = strtolower($name[0]) . '_item' ;
$data[$var_cust] = $flower->cust_name;
$data[$var_name] = $flower->item;
}
Using ${} is a way to create dynamic variables.So Try the following
<?php
foreach($flowers as $flower){
$cast_name = explode(' ', trim($flower->cust_name));
${strtolower($cast_name[0]). '_cust'}=$flower->cust_name;
${strtolower($cast_name[0]). '_item'}=$flower->item;
}
?>

Division with php

Im trying to get out an average value from a vote function.
<?php
$file = file("textfile.txt");
$textfil = file_get_contents("textfile.txt");
$textfill = str_split($textfil);
echo "Number of votes: " . count($textfill) . "<br>";
$sum = 0;
foreach ($textfill as $vote) {
$sum = $sum + intval($vote);
}
echo "Average: " . $sum;
?>
Simple by substitute (+) with a (/), and even tried a (%). But still getting error message.
Would appreciate alot if anyone could help me out and tell me what im doing wrong.
/thanks
Edit
Sidenote: Please read an explanation under "First answer given" further down below.
This version will take into account any blank lines in a file, if the content looks like:
1
2
3
// <- blank line
Sidenote: Please provide a sample of your text file. A comment has already been given to that effect.
PHP
<?php
// first line not required
// $file = file("textfile.txt");
$textfil = file_get_contents("textfile.txt");
$textfill = array_filter(array_map("trim", file("textfile.txt")), "strlen");
echo "Number of votes: " . count($textfill) . "<br>";
$sum = 0;
foreach ($textfill as $vote) {
$sum += intval($vote);
}
$avg = $sum / count($textfill);
echo "Average: " . $avg;
?>
First answer given
Using the following in a text file: (since no example of file content was given)
5
5
5
IMPORTANT NOTE: There should not be a carriage return after the last entry.
produced
Number of votes: 5
Average: 3
which is false, since there are 3 entries in the text file.
explode() should be used, and not str_split()
The following using the same text file produced:
Number of votes: 3
Average: 5
which is correct. In simple mathematics, averages are done by adding all numbers then dividing them by how many numbers there are.
In this case it's 3 numbers (all 5's) added equals 15, divided by 3 is 5.
Sidenote: The first line is not required $file = file("textfile2.txt");
<?php
// first line not required
// $file = file("textfile.txt");
$textfil = file_get_contents("textfile.txt");
$textfill = explode("\n", $textfil);
echo "Number of votes: " . count($textfill) . "<br>";
$sum = 0;
foreach ($textfill as $vote) {
$sum += intval($vote);
}
$avg = $sum / count($textfill);
echo "Average: " . $avg;
?>
Footnotes:
If the average comes out to 8.33333 and you would like it to be rounded off to 8, use:
echo "Average: " . floor($avg);
If the average comes out to 8.33333 and would like it to be as 9 you would use:
echo "Average: " . ceil($avg);
ceil() function
floor() function
You may be mixing in stuff that can't be divided, like text, etc. I don't know what your text file looks like. intval may be having a problem with arrays. You may try:
foreach ($textfill as $vote) {
if(is_int($vote) {
$sum += $vote;
}
}
echo "Average: " . $sum;
Lower school math says:
foreach ($textfill as $vote) {
$sum += intval($vote);
}
$avg = $sum / count($textfill);
The average value is calculated by divide the sum with the number of votes. This line will print the average value:
echo "Average: " . $sum/count($textfill);

Categories