0

I've been stuck on this for a while and am not sure of the problem.

Maybe someone here might have some good insight?

Here is the 'code':

class File extends Stackable{
    private $data;
    function set_data($array){
        foreach($array as $row => $data)
            foreach($data as $col => $val)
                $this->data[$row][$col]=$val;
                echo $this->data[$row][$col];                    
    } 

}

In which it states that on the echo there is an Undefined index : $col, where $col is generally a letter.

$row can be assumed to be set.

Maybe I'm not providing enough details, and its possible that there may be other dependencies, if so please let me know.

One thing of note is the use of php pthreads here, though i don't believe it is the cause, since the error still happens with 1 thread.

Thank you in advance for any help.

6
  • What's the output of var_dump($array); right in the beginning of the function? Commented Oct 10, 2013 at 11:36
  • Why do this? You're trying to implement $this->data=$array in a very strange fashion Commented Oct 10, 2013 at 11:37
  • @TheWolf var_dump simply outputs the current values that were entered into the array before, here being a 2x20 array Commented Oct 10, 2013 at 11:47
  • @AlmaDoMundo, would this specific implementation have an impact on the result? If so can you clarify on that? Commented Oct 10, 2013 at 11:50
  • @Victor.dMdB you're iterating through your array just to fill your data with same keys and values. So I can't understand why do so, if you can just assign array directly Commented Oct 10, 2013 at 11:56

2 Answers 2

1

in your second foreach you must put the code between {} like :

     foreach($data as $col => $val){
                    $this->data[$row][$col]=$val;
                    echo $this->data[$row][$col]; 

}

in the echo $this->data[$row][$col]; is out of for each , $col and $val is not defined .

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Morteza, thanks for the tip, though I tried that initially, hoping it would change something, though it unfortunately did not
victor , can u print_r the $this->data after end of loops, end of the function then exit; , so u can find is the $this->data empty or not , if not so u can use the keys for test .
0

The member "data" is just a normal array, your are loosing dimensions because of the way pthreads objects work; you don't want to use the member "data", it is not necessary:

<?php
class File extends Stackable {

    /* in pthreads you are responsible for the objects you create */
    /* so we accept an array by reference to store dimensions */

    public function set_data($array, &$files){
         foreach($array as $row => $data) {
            foreach($data as $col => $val) {
                 /* force this vector into existence */
                 if (!isset($this[$row])) {
                    $this[$row] = $files[] = new File();
                 }
                 $this[$row][$col]=$val;
            }
         }                  
    } 

    public function run() {}
}

$files = array();

$f = new File();
$f->set_data(array("test" => array($_SERVER)), $files);

var_dump($f);
?>

You should bear in mind that pthreads objects have the overhead of safety to contend with, so loop over their members as little as possible, in an ideal world, the $array coming to setData would already be of a suitable type ...

1 Comment

thanks for the clarification, I think it was the in pthreads you are responsible for the objects you create, which I'd misunderstood the 1st time round. I've recoded to only use 1dimensional arrays added to a pool instead of a shared ressource, works all fine now (except for other aspects which i am still struggling with, ie imported packages which don't seem to be thread safe)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.