class Test
{
public $data = array();
public function addData($data = array())
{
array_merge($data, $this->data);
return $this;
}
public function showData()
{
print_r($this->data);
}
}
$test = new Test;
$test->addData(array("halo", "zaki"))->showData();
i tried to merging 2 array, but it doesn't work, maybe someone can explain to me?
array_merge does not modify the arrays passed to it, but rather returns the result.
Try this:
public function addData($data = array())
{
$this->data = array_merge($data, $this->data);
return $this;
}
You forgot to assign the resulting array to member variable $data. It should be,
$this->data = array_merge($data, $this->data);
Related
Here is my php code, the test method not giving wanted output, and the other weird thing is var_dump('a') print 3 times;
my wanted output is array('qtggccc','qtff23sdf');
public function main()
{
$serverIds = array('ff23sdf','ggccc');
$res = $this->test($serverIds);
var_dump($res);
}
public function test($serverIds,$imgArray = array())
{
if(count($serverIds) > 0){
$media_id = array_pop($serverIds);
$imgUrl= $this->hh($media_id);
array_push($imgArray,$imgUrl);
var_dump($serverIds);
var_dump($imgArray);
$this->test($serverIds,$imgArray);
}
var_dump('a');
return $imgArray;
}
public function hh($m)
{
return 'qt'.$m;
}
Try this:
class MyClass{
private $imgArray = array();
public function main(){
$serverIds = array('ff23sdf','ggccc');
$res = $this->test($serverIds);
print_r($this->imgArray);
}
public function test($serverIds){
if(count($serverIds) > 0){
$media_id = end($serverIds);
$imgUrl= $this->hh($media_id);
array_push($this->imgArray,$imgUrl);
//remove last element
array_pop($serverIds);
$this->test($serverIds);
}
return;
}
public function hh($m){
return 'qt'.$m;
}
}
$obj = new MyClass();
echo '<pre>';
$obj->main();
Why use recursion? You are using a complicated solution for a simple problem.
public function main()
{
$serverIds = array('ff23sdf','ggccc');
$res = array();
//These three lines replace an entire recursive function, making the code easier and saving a chunk of memory once you start using real arrays
foreach ($serverIds as $media_id){
array_unshift($res, $this->hh($media_id));
}
var_dump($res);
}
public function hh($m)
{
return 'qt'.$m;
}
I got an error
"Cannot use object of type QueryResult as array"
I am simply trying to take state name from the records array but, I only manage to get this error "Cannot use object of type QueryResult as array"
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
return $statename;
}
Controller.php
public function index() {
$data['getstatename'] = $this->model->getstatename();
$this->load->view('footer', $data);
echo "<pre>";
print_r($data['getstatename']['records']);
}
Actually $statename is result-set object not an array.
So you need to do like below:-
Model.php:-
public function getstatename(){
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array(); // create an array
foreach ($statename as $row){
$state_array[] = $row->State__c; // assign value to array
}
return $state_array; // return array
}
Controller.php:-
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
echo "<pre/>";print_r($data['getstatename']); // check this and accordingly change you code further
$this->load->view('footer', $data);
}
Here is the solved code Thanks to #Anant
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array();
foreach ($statename as $row){
$state_array[] = $row->State__c;
}
return $state_array;
}
Controller.php
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
$this->load->view('footer', $data);
}
See if you can do
$data['getstatename']->fetchRow();
I have two functions and Im trying to pass an array of data to the function that have the view like this:
function one()
{
$data['array'] = $array;
$this->load->view('etc/asdf', $data);
}
function two()
{
$array[];
return $array;
}
What Im doing wrong?
Thank you in Advance!
Try:
$data['array'] = $this->two(); // Instead of $array, as it is undefined
$this->load->view('etc/asdf', $data);
And also to define array you have to do following $array = array();, not $array[];
Note: You have to invoke function rather than specify $array, as it is returned from function called two
function one()
{
$data['array'] = $this->two();
$this->load->view('etc/asdf', $data);
}
function two()
{
$array= array('1','2','2');
return $array;
}
instead of $data['array'] = two();
use $data['array'] = $this->two();
So I have a problem I have an array that is passes to setData function
after that I call getE that suppose to return the array but instead I'm getting Null what am I doing wrong?
<?php
class Se {
public $data1;
public function setData(array $data){
if (empty($data)) {
throw new InvalidArgumentException('The name of an employee cannot be empty.');
}
$data1 = $data;
$data1 = array_values($data1);
var_dump($data1);
}
public function getE(){
return $data1[0];
}
}
$tmpaaa= array('3333','222');
$ttt = new Se();
$ttt->setData($tmpaaa);
echo $ttt->getE();
So my revised code looks like this now
class Se {
public $data1;
public function setData(array $data)
{
if (empty($data))
{
throw new InvalidArgumentException('The name of an employee cannot be empty.');
}
$this->data1 = $data;
}
public function getE()
{
return $this->$data1[0];
}
};
$tmpaaa= array('3','2');
$ttt = new Se();
$ttt->setData($tmpaaa);
echo $ttt->getE();
?>
In order to access class instance properties from within the class, you need to prefix the variable name with $this. See http://php.net/manual/language.oop5.properties.php
To fix your problem, change this in setData
$data1 = $data;
$data1 = array_values($data1);
var_dump($data1);
to this
$this->data1 = array_values($data);
var_dump($this->data1);
and getE to
public function getE(){
return $this->data1[0];
}
Update
As it appears the $data1 property is required in Se, I'd set it in the constructor, eg
public function __construct(array $data) {
$this->setData($data);
}
and instantiate it with
$ttt = new Se($tmpaaa);
echo $ttt->getE();
It is also recommended not closing the php tag in a class file, this prevents space issues.
<?php
class Se {
public $data1;
public function setData(array $data)
{
if (empty($data))
{
throw new InvalidArgumentException('The name of an employee cannot be empty.');
}
$this->data1 = array_values($data); //you error was here, no need to to assign $data twice so I deleted top line.
}
public function getE()
{
return $this->data1[0];
}
}
$tmpaaa = array('3333','222');
$ttt = new Se();
$ttt->setData($tmpaaa);
echo $ttt->getE();
I'm trying to make the following example works. It looks like PHP thinks $this->getData2 as a member variable. How do I make it so that PHP thinks it as a method?
class Test {
public function getData()
{
return array(
'data1'=>array('name'=>'david'),
'data2'=>$this->getData2
);
}
public function getData2()
{
return "hello"
}
}
$test = new Test;
$data = $test->getData();
$data = $data['data2']();
I've tried the following, but looks like..I can't use $this in this case
function() use($this) {
return $This->getData2();
}
class Test {
public function getData(){
return array(
'data1'=>array('name'=>'david'),
'data2'=>'getData2'
);
}
public function getData2() {
return "hello";
}
}
$test = new Test;
$data = $test->getData();
$data = $test->$data['data2']();
echo $data;
Wasn't working without the $test-> on the $data = $test->$data['data2'](); line
And because I love fiddles: http://phpfiddle.org/main/code/4f5-v37
A callable to a method is an array with the object as a first member, and the method name as a second one.
So:
class Test {
public function getData()
{
return array(
'data1'=>array('name'=>'david'),
'data2'=>array($this, 'getData2')
);
}
public function getData2()
{
return "hello";
}
}
$test = new Test;
$data = $test->getData();
$data = $data['data2']();
Try:
class Test {
public function getData(){
return array('data1' => array('name' => 'david'), 'data2' => 'getData2');
}
public function getData2(){
return 'hello';
}
}
$test = new Test; $data = $test->getData(); echo $test->$data['data2']();
Easiest way would just be to do the calculation in a variable outside of the array and then just put the variable into the array