How to debug an issue in the code sum of digit? - php

I am having an issue in the code
Warning: Cannot use a scalar value as an array in /opt/lampp/htdocs/programe/sumDigit.php on line 410
Here is the code:
class SumOfDigit{
public $sum = 0;
public $num ;
public function __construct($n) {
$this->num = $n;
}
public function equation() {
for($i= 0; $i<strlen($this->num); $i++){
$this->num[$i] = array_map('intval', str_split($this->num));
$this->sum += $this->num[$i];
print_r($this->sum);
}
}
}
$obj = new SumOfDigit(232);
echo $obj->equation();

Based on the name of your class I assume you want to sum the digits in the value you create an object of the class with. In that case, you can simplify your code by using array_sum on the output of str_split:
Class SumOfDigit{
public $sum = 0;
public $num;
public function __construct($n){
$this->num = $n;
}
public function equation(){
$this->sum = array_sum(str_split($this->num));
return $this->sum;
}
}
$obj = new SumOfDigit(232);
echo $obj->equation();
Output:
7
Demo on 3v4l.org

In your code first you assigned a integer to "num"
$this->num = $n;
and inside the function equation() you are using the same value as array as well
$this->num[$i] = array_map('intval', str_split($this->num));
Change the name any one of the variable and try.
Hope it helps

Related

How can i change the value of $number here

I want to create a class. Each time it will be called it will increase the value of $number by 1. When it will reach 7, it should return a message "Maximum limit reached". Where to define the $number and how to store the new value in it.
class addClass
{
public $number = 0;
public static function addOne($number)
{
$number = $number++;
if ($number == 7) {
return 'This is 7';
}
}
}`
I think this is what you are looking for based on your description:
class MyNumber {
private static $number = 0;
public static function addOne() {
self::$number++;
if (self::$number === 7) {
return 'Maximum limit reached';
}
return self::$number;
}
}
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
First result is 1
Second result is 2
Third result is 3
Fourth result is 4
Fifth result is 5
Sixth result is 6
Seventh result is Maximum limit reached
You won't need to have $number in the addOne Function
There are two alternatives
If you don't want to keep $number as static then you can change addOne to a non-static method and access using $this->
class addClass
{
public $number = 0;
public function addOne()
{
$this->number = $this->number + 1;
if ($this->number == 7) {
return 'This is 7';
}
}
}
Or if you want addOne to be static then you can declare $number as static and access using self::
class addClass
{
private static $number = 0;
public static function addOne()
{
self::number = self::number + 1;
if (self::number == 7) {
return 'This is 7';
}
}
}
Please bear in mind:
1) The $number parameter in the addOne() method is taking precedence over the $number member in the addClass() parameter.
2) The sentence $number = $number++ is no affecting the variable $number at all, because it is first being assigned.
3) The addOne() method doesn't need to be static, unless is intended to be used without an instance of the class addClass.
4) Static variables only need to be initialize once, refer to the php manual for more information on the static keyword: http://php.net/manual/en/language.oop5.static.php
5) You cannot reference member variables inside a static method (e.g. using $this), because static methods have "class scope" and they are meant to be used without any instance of such class. On the other hand, non static methods require an instance of the class and they can reference members of the class by using $this.
6) Here's an example of how you can do this:
<?php
class addClass{
public function addOne($number) {
static $limit = 0;
if (!isset($limit)) {
$limit = $number;
}
if ($limit+1 == 7) {
return "Maximum limit reached";
} else {
$limit = $number+1;
}
}
}
$a = new addClass();
for($i = 0; $i< 7; $i++) {
echo $i+1, " => ", $a-> addOne($i), PHP_EOL;
}

Trouble understanding the calling of global variables with in a class to a method within the class

I am having trouble with my numberOfDecks method. I try to call the array of cards that is created in my constructor, but the an error keeps coming up that I am passing an undefined variable ($cards). I thought since it is a global variable $cards would be able to be called to the numberOfDecks method.
<?php
/* creating a deck of cards class to be used in a blackJack game*/
class Deck{
public $cards = array();
//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";
}
}
}
/*trying to add more decks to increase number of total cards
in my array (does not work)*/
public function numberOfDecks($number){
$this->cards = $cards;
$this->number= $number;
for($i = 0 ; $i < $number; $i++){
array_push($cards[],$i);
}
return $cards;
}
}
$deck = new Deck();//works as expected
$deck->numberOfDecks(3);//trouble
$shuffled = shuffle($deck->cards);//works as expected
var_dump($deck);
Your variable $cards is undefined because you don't declare it.
public function numberOfDecks($number){
$this->cards = $cards;
You probably meant to flip the assignment direction.
$cards = $this->cards;
Given your comment for adding decks to the object total deck, after flipping the assignment direction, try using array_merge
Here's a working fiddle.
<?php
class Deck{
public $cards = array();
//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";
}
}
}
public function numberOfDecks($number){
$cards = $this->cards;
$this->number = $number;
for($i = 0 ; $i < $number; $i++){
$this->cards = array_merge($this->cards, $cards);
}
}
}
$deck = new Deck();//works as expected
// how many cards are intially constructed?
echo "count of cards in new deck: " . count($deck->cards) . "\n<br/>\n";
// add 3 more decks of cards
$deck->numberOfDecks(3);//trouble
echo "count of cards after adding 3 decks: " . count($deck->cards);
// $shuffled = shuffle($deck->cards);//works as expected
// var_dump($deck);
Results in this output:
count of cards in new deck: 52
count of cards after adding 3 decks: 208

PHP method to get values dynamically from an array object property

In this class, is it possible to get dynamically a value from the array?
class MyClass {
private $array_data;
function __construct() {
$this->array_data['first']['a'] = '1';
$this->array_data['second']['b'] = '2';
$this->array_data['third']['c'] = '3';
}
public function getIndexValue($index){
return $this->{'array_data' . $index};
}
}
$MyClass = new MyClass();
// Prints NULL, but i expect '1'
var_dump($MyClass->getIndexValue("['first']['a']"));
Here's a simple solution. Rather than passing in a string for the indexes, you pass in an array.
public function getIndexValue(array $indexes) {
// count the # of indexes we have
$count = count($indexes);
// local reference to data
$data = $this->array_data;
for ($i = 0; $i < $count; $i++)
{
// enter the array at the current index
$data = $data[$indexes[$i]];
}
return $data;
}
And then rather than a string, you'd pass in an array:
$MyClass->getIndexValue(['first', 'a'])

i am trying to make a array of a object in php

I'm trying to make an array of Spells.
My current code
class Spell
{
public $bomb = 0;
public $fire = 0;
function Spell()
{
$this->bomb =0;
$this->fire =0;
}
}
And I declare the object spell on my game class like this
class game
{
public $Spell=array();
function Game()
{
$this->Spell[0] = new Spell();
}
function s()
{
$this->Spell[1]->$bomb = $load($x)
$this->Spell[1]->$fire = $load($x);
$this->Spell[2]->$bomb = $load($y)
$this->Spell[3]->$bomb = $load($z)
}
}
It returns this error -- Warning: Creating default object from empty value in...
I guess this isn't the best way to create an array of objects. How to do it properly?
EDIT:
x y z, just return strings
The problem is that you have not created objects for $this->Spell[1], $this->Spell[2] and $this->Spell[3]. If you change your Game() constructor to this:
function Game()
{
for ($i = 1; $i <= 3; $i++) {
$this->Spell[$i] = new Spell();
}
}
It should probably work fine.
You seem to have more than just one problems in your code.
However, I will discuss the one you have asked the question for.
Instead of
$this->Spell[1]->$bomb = something;
Use
$this->Spell[1]->bomb = something;
Second, What do you intend to do by using $load($y)?
If you're using a function named "load", use load($y)
you must create object, then use it, look:
class Spell
{
public $bomb = 0;
public $fire = 0;
function __construct()
{
$this->bomb =0;
$this->fire =0;
}
}
class game
{
public $Spell=array();
function s()
{
$this->Spell[1] = new Spell();
$this->Spell[1]->bomb = 0 ; //or other value
}
}
<?php
class Spell
{
public $bomb = 0;
public $fire = 0;
function Spell()
{
$this->bomb =0;
$this->fire =0;
}
}
class game
{
public $Spell=array();
function Game($index)
{
$this->Spell[$index] = new Spell();
echo 'constructer called';
}
function s()
{
$this->Spell[1]->bomb = $load($x);
$this->Spell[1]->fire = $load($x);
$this->Spell[2]->bomb = $load($y);
$this->Spell[3]->bomb = $load($z);
}
}
$ob = new game();
//$ob->Game(1); to pass the index for array.
?>
You are using lots of undefined stuff, I would say the half of your script is missing.
I just added the comments down here:
class game
{
public $Spell=array();
function Game()
{
$this->Spell[0] = new Spell();
}
function s()
{
/**
down here you are using these undefined "variables":
$bomb
$load
$x
$y
$z
undefined means, you are using a varible which was not declared. so it´s just null.
I tried to fix it:
**/
$x = 1;
$y = 2;
$z = 3;
$this->Spell[1] = new Spell();
$this->Spell[2] = new Spell();
$this->Spell[3] = new Spell();
$this->Spell[1]->bomb = load($x); // add ;
$this->Spell[1]->fire = load($x);
$this->Spell[2]->bomb = load($y)
$this->Spell[3]->bomb = load($z)
}
}
function load($v)
{
return $v * 2;
}

sort associative array codeigniter php

Here is what I want to do:
$newArray = array();
foreach($student as $s){
$newArray[$s->id][$s->grade] = $s;
}
I want to sort the students by their grades (more of a group than a sort) but I just want the grades to be sorted not the id. I could have don't this:
$newArray[$s->id] = $s->grade
asort($newArray)
but I need the remaining data in $s. Also, there is huge chunk of data associated with each student which I want to maintain.
How can I achieve such a sorting?
Edit:
Sine you're working in a framework, best declare your sort callback as a member function (inside the same class as where you'll be needing it, of course):
private function sortCB(array $a, array $b)
{//the array type hinting in arguments is optional
$i = array_keys($a);//but highly recommended
$j = array_keys($b);
if (end($i) === end($j))
{
return 0;
}
//replace '>' with '<' if you want to sort descending
return (end($i) > end($j) ? 1 : -1);//this is ascending
}
Then, in the method where the actual sorting is needed:
uasort($theArray,array($this,'sortCB'));
For more examples, see the docs. I've added a full class example at the end of this (bulky) answer
I've tried this on writecodeonline, which isn't all too good at this kind of stuff, but this did work:
$foo = array_fill_keys(array('foo','bar','q','Bond'),array());
$i = '256';
foreach($foo as $k=>$v)
{
$foo[$k][$i] = $k;
$i = (string)((int)$i%2 === 0 ? ((int)$i/2)+1 : (int)$i*3);
}
function sortCB($a,$b)
{
$i = array_keys($a);
$j = array_keys($b);
if (end($i) === end($j))
{
return 0;
}
return (end($i) > end($j) ? 1 : -1);
}
uasort($foo,'sortCB');
var_dump($foo);
But since you're using a framework, you might do well declaring that function as a member function private function sortCB(array $a,array $b), and use it like so:
uasort($foo,array($this, 'sortCB'));
There might be some more info on how best to use this callback function in a class context here
Full example + usage (tested and working):
class test
{
public $foo = null;
public function __construct()
{
$this->foo = array_fill_keys(array('foo','bar','q','Bond'),array());
$i = '256';
foreach($this->foo as $k=>$v)
{
$this->foo[$k][$i] = $k;
$i = (string)((int)$i%2 === 0 ? ((int)$i/2)+1 : (int)$i*3);
}
}
private function sortCB($a,$b)
{
$i = array_keys($a);
$j = array_keys($b);
if (end($i) === end($j))
{
return 0;
}
return (end($i) > end($j) ? 1 : -1);
}
public function sortFoo()
{
uasort($this->foo,array($this,'sortCB'));
print_r($this->foo);
return $this->foo;
}
}
$bar = new test();
$arr = $bar->sortFoo();
You can do something like:
foreach($student as $s){
$newArray[$s->id] = $s;
}
usort($newArray, function ($a, $b) { return $a->grade - $b->grade; });
Edit
For later versions that don't support anonymous functions you can define comparison function first:
function sortByGrade($a, $b)
{
return $a->grade - $b->grade;
}
usort($newArray, 'sortByGrade');
But if you get this data from db it would be easier to order it in your sql query. If you use ORM you can use its associated method.

Categories