for($i = 0; $i < count($prices); $i++){
error_log($prices[$i]->anObjectVariable);
}
or
foreach ($prices as $price){
error_log($price->anObjectVariable);
}
None of these seems to work, here are the errors I get:
PHP Notice: Undefined property: price::$anObjectVariable
this is the code which I use to prepare the object(s) and the array.
class price {
public $anObjectVariable;
}
$prices = array();
$p = new price();
$p->anObjectVariable = "PRINT ME IN ERROR LOG!";
array_push($prices, $p);
I just tested it locally and the following code works fine if you define $prices as an array before you use it.
class price {
public $anObjectVariable;
}
$prices = array();
$p = new price();
$p->anObjectVariable = "PRINT ME IN ERROR LOG!";
array_push($prices, $p);
for($i = 0; $i < count($prices); $i++){
echo($prices[$i]->anObjectVariable);
}
Are you actually testing the code you show us above (i.e. the one I just posted above) or are you working on a derivative? Can you confirm that this exact snippet above works for you correctly?
Then if it's not a typo
for ($i = 0; $i < count($prices); $i++) {
error_log($prices[$i]->anObjectVariable);
}
should work
Related
I am trying to find first element(alphabetical order) from array string using loop without inbuilt functions in php8. My code is below but it is not working a intented. What is the error in my code?
eg: string = "This","is","my","apple";
apple need to print
$arraystring= array("This","is","my","apple");
$count = count($arraystring);
for ($i = 0; $i < $count; $i++) {
for ($j = 1; $j < $count; $j++) {
if(strcmp($arraystring[$i], $arraystring[$j])<0){
$temp = $arraystring[$i];
$arraystring[$i] = $arraystring[$j];
$arraystring[$j] = $temp;
}
}
}
my code not worked as intented
I get the following json data from a database:
[{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}]
I'm trying to add all value values together, so: 9.95 + 6.95 ... so that I get 67.6 as result.
I tried the below code, but I am getting 16.9 as repeated values.
for ($i = 0; $i <= $count - 1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
$totalservice = 0;
foreach ($serviceValue as $key => $value) {
$totalservice += $value['service_value'];
}
echo $totalservice;
}
You can do it like below:-
$jsonObj = json_decode($json); // Decode the JSON to OBJ
// Now loop and find the SUM
$total = 0;
foreach ($jsonObj as $item){
$total =+ $item->value;
}
// Print the SUM
echo "Sum : $total";
Note:- In your code $totalservice beome 0 every time when loop goes to next iteration and that's why you are getting same value repeated time. So do like (what #u_mulder said) :-
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
.....//rest code
}
I have made the below changes. It works fine.
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
foreach ($serviceValue as $key => $value) {
$totalservice+= $value['service_value'];
}
echo $totalservice;
}
Thanks for the help
I have a class that I will later break up into multiple classes creating a BlackJack card game. I have the the decks created and accounted for correctly. I am having a problem with the output of my player array in my method "deal". The output only displays one player but my array of outputs should be returning two. The amount of cards that are missing from my deck also indicate that I am missing 4 cards, which makes sense since I have 2 players being dealt 2 cards. Can someone please help me return both players and display both of their respective cards.
<?php
/* creating a deck of cards class to be used in a blackJack game*/
class Deck{
public $cards = array();
public $player = [];
//creates an instance of a deck of cards (works)
public function __construct(){
$values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A');
$suits =array('Diamond','Club','Heart','Spade');
foreach ($suits as $suit) {
foreach($values as $value){
$this->cards[] = "$value of $suit's";
//$deck = $this->cards;
}
}
}
/*add more decks to increase number of total cards
in my array (works)*/
public function numberOfDecks($number){
$cards = $this->cards;
$this->number = $number;
for($i = 0 ; $i < $number-1; $i++){
$this->cards = array_merge($this->cards, $cards);
}
return $cards;
}
/*adding elements to a player as expected need to return multiple players and their cards multiple players (does not currently work)*/
public function deal($numberOfPlayers){
$this->numberOfPlayers = $numberOfPlayers;
$player = $this->player;
$number = 2;
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j] = $this->cards[0];
array_shift($this->cards);
}
}
for($k = 0; $k < $numberOfPlayers; $k++)
return $player[$k];
}
}
$deck = new Deck();//works as expected
$deck->numberOfDecks(3);//works as expec
$shuffled = shuffle($deck->cards);//works as expected
$deck->deal(2);
var_dump($deck);
here is the output I am currently getting back
The problem is in this chunk here:
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j] = $this->cards[0]; // <--- fix me!
array_shift($this->cards);
}
}
When that outer loop goes around again, it just straight overwrites the cards issued. Try something like this:
for($i = 0; $i < $number; $i++){
for($j = 0; $j < $numberOfPlayers; $j++){
$this->player[$j][] = $this->cards[0]; // <- push into an array
array_shift($this->cards);
}
}
This one's creating and pushing the cards into an array for each player, rather than there being just the one (repeatedly overwritten) card.
I am new to PHP and am trying to remove duplicate entries in an array. I end up with my desired output, but I am also getting two "Undefined offset" errors along the way. This is the code I have:
$this->master refers to an array declared in the beginning of the class.
public function removeDuplicates(){
$var = count($this->master);
for($i = 0; $i < $var; $i++){
for($j = 0; $j <$var; $j++){
if(($this->master[$i] == $this->master[$j]) && $i != $j){
$this->shiftLeft($j, $var);
$var --;
}
}
}
}
public function shiftLeft($t, $s){
while($t < $s){
echo "$t ";
$this->master[$t] = $this->master[$t+1];
$t++;
}
unset($this->master[$t-1]);
}
It is probably a really simple logical error but I cannot seem to find where. Any help is greatly appreciated.
See if it works
$unique = array_unique($this->master);
I'm having to develop a site on PHP 5.1.6 and I've just come across a bug in my site which isn't happening on 5.2+. When using foreach() to iterate over an object, I get the following error: "Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference..."
Does anyone know how to convert the following foreach loop to a construct which will work with 5.1.6? Thanks in advance!
foreach ($post['commercial_brands'] as $brand)
{
$comm_food = new Commercial_food_Model;
$comm_food->brand = $brand;
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
}
Improving upon Coronatus's answer:
$max = count($post['commercial_brands']);
for ($i = 0; $i < $max; $i++)
{
$comm_food = new Commercial_food_Model;
$comm_food->brand = $post['commercial_brands'][$i];
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
}
You should never have a function in the condition of a loop, because each time the loop goes around it will run the function.
$x = 0;
$length = count($post['commercial_brands']);
while($x < $length){
$comm_food = new Commercial_food_Model;
$comm_food->brand = $post['commercial_brands'][$x];
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
$x++;
}
//while 4 eva
for ($i = 0; $i < count($post['commercial_brands']); $i++)
{
$comm_food = new Commercial_food_Model;
$comm_food->brand = $post['commercial_brands'][$i];
$comm_food->feeding_type_id = $f_type->id;
$comm_food->save();
}