0

I have the following array result returning from a table when called from xmlrpc client and want to send the this response to client side.

array(17) {
      ["A"]=>
      int(1)
      ["B"]=>
      int(0)
      ["pnl"]=>
      int(1)
      ["C"]=>
      int(1)
      
      ["buttons"]=>
      array(5) {
        [1]=>
        string(9) "ABC"
        [2]=>
        string(5) "DEF"
        [3]=>
        string(5) "G"
        [4]=>
        string(7) "H"
        [5]=>
        string(9) "I"
      }
      ["List"]=>
      string(580) "<th title="S">Ticker</th><th title="Position - Shares Held in Portfolio">Pos</th><th title="Average Cost">Cost</th><th title="Current Market Price">Price</th>"
    }

Currently using the following code to fetch the record but it returns undefined offsets .i got the above result using the var_dump function which contains all the data fetched . how can i change my loop to get the result in appropriate row?

var_dump($model) ;

        for ($i = 0; $i < count($model); ++$i) {
            $xml_rpc_rows[$i] = array($model[$i], 'struct');
        }
       
    }
   $response = array(
        $xml_rpc_rows,
        'struct');
    return $this->xmlrpc->send_response($response);
2
  • Try to loop with a foreach Commented Sep 13, 2013 at 8:23
  • please also note before entering foreach() check if desired $variable is fit for foreach or you'll get errors (in views mostly). Commented Sep 13, 2013 at 9:32

2 Answers 2

2

Your loop wants the following index from the array: $xml_rpc_rows[$i] which would translate to $xml_rpc_rows[0], so index == 0. But your array has only indexes (keys) as: $xml_rpc_rows['A'].

The loop would also not work on the buttons array, since the first index for that array is 1. So your loop would need $i=1.

A foreach loop would give you more info and make it a bit clearer.

<?php
foreach ($model as $key => $val) {
  print $key . '<br />';
  print $val;
}

Now you can see the key / index your array uses and the values those contain.

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

Comments

1

While creating your array you used letters as indexes but in your loop you're trying to use numerical indexes. Instead of that you should use foreach to iterate through this array.

6 Comments

$i=0; foreach ($model as $row): $xml_rpc_rows[$i]=$row; $i++; endforeach;
but can i still pass it like this array($row[$i], 'struct');
ill just try it . i am having a different response set then i expected that is why i am asking . ill post the latest response
No, there is no index $i in $row.
yeah that line did cause offset issue again .. here is the response without ($row[$i] ,'struct')
|

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.