2

I have little experience in PHP and I have to convert a php script to python. I couldn't understand what exactly these lines do in the code:

$vars = array();
$vars['a'] = array();
$vars['b'] = array();
$vars['b'][] =  'text1';

What does the last line stand for? And what would happen if I add the line below to the code?

$vars['b'][] =  'text2';

I would appreciate help also on converting this to python. Thanks a lot,

alpoktem
  • 43
  • 4

3 Answers3

1

If you would like to convert the PHP code snippet to python, the closest you can get would be somewhat

>>> var = {}
>>> var['a'] = {}
>>> var['b'] = {}
>>> var['b'][len(var['b'] )] = 'text1'
>>> var['b'][len(var['b'] )] = 'text2'
>>> var
{'a': {}, 'b': {0: 'text1', 1: 'text2'}}

another variation

>>> class array(dict):
    def __getitem__(self, key):
        return dict.__getitem__(self, key)
    def __setitem__(self, key, value):
        if key == slice(None, None, None):
            dict.__setitem__(self, self.__len__(), value)
        else:
            dict.__setitem__(self, key, value)


>>> var = array()
>>> var['a'] = array()
>>> var['b'] = array()
>>> var['b'][:] = 'text1'
>>> var['b'][:] = 'text2'
>>> var
{'a': {}, 'b': {0: 'text1', 1: 'text2'}}
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • I used append instead of adding at len(var['b']). I guess it works the same way – alpoktem Oct 07 '13 at 18:55
  • @googiddygoo: If you are using `dict`, append method may not be available. Strictly speaking, php's array are associative arrays which are best translated to pythons dict – Abhijit Oct 07 '13 at 18:57
  • Yes I was using a list instead of dict. But changed it to dict instead. Works perfect. Thanks – alpoktem Oct 07 '13 at 19:05
0

The last line just adds an entry with the string text with a numerical (incrementing) key to the array $vars['b'].

When $vars['b'] is empty, it begins with the key 0. (=> $vars['b'][0] === 'text')


So that means your array will look like:

array(2) {
  ["a"]=>
  array(0) {
  }
  ["b"]=>
  array(2) {
    [0]=>
    string(5) "text1"
    [1]=>
    string(5) "text2"
  }
}

(sorry; at the point where you asked the question there wasn't the last phrase I would appreciate help also on converting this to python. yet … and I don't know python.)

bwoebi
  • 23,637
  • 5
  • 58
  • 79
0
// http://www.trainweb.org/mccloudrails/History/boxcars_runaround.jpg![a train with box cars][1]
// imagine this like a train, and each boxcar on that train has a name on it
// this is like a list in python @see http://www.tutorialspoint.com/python/python_lists.htm
$vars = array();

// this is the name of the first box car, now this box car is ALSO a train with box cars, ie, a train within a boxcar of a train
$vars['a'] = array();

// same as above, but with a different box car 
$vars['b'] = array();

// @see http://stackoverflow.com/questions/252703/python-append-vs-extend
$vars['b'][] =  'text1';

// Q) what  does the last line stand for? And what would happen if I add the line below to the code? $vars['b'][] =  'text2';
// A) This would make the array look somewhat like this: 
//    [a => [/* empty sad box car in an empty sad box car */]] [b => ['text1', 'text2'] ]

This is not entirely accurate, but a great start. http://www.youtube.com/watch?v=ufmzc2sDmhs

aretecode
  • 334
  • 1
  • 5