V
V
Vadim2014-01-17 16:23:32
PHP
Vadim, 2014-01-17 16:23:32

How to nest an array in a cell whose key is auto-incrementing( $name[]) (PHP)?

Good afternoon.
Question:
How about an array whose elements are created in the form:
$users[] = $user['info'];
Add some other value to the created cell?
To make it work for example - so?
a[1][info] = 'test'
a[1][status] = 'online'
There is a loop that forms an array, it looks like this:
for simplicity, the example is made up
Structure:
$users[0..N][sex]
$users[0..N][user_info]

foreach ($users as $key => $value) {
  switch ($value['sex']) {
    case 1: 
      $men[] = $value['user_info'];
      //как можно к массиву $men добавить еще 1 массив?
      //$men[] = array('status' => 'online'); такая строка, естественно создаст новый элемент массива.
      break;
    case 0:
      $women[] = $value['user_info'];
      break;
    default:
      break;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shirshov Alexander, 2014-01-17
@Subotinn

$men[] = array (
  'user_ifo' => $value['user_info'],
  'status' => 'online'
 );

or add via $men[$key] = ...
if $value['user_info'] is already an array, then you can first add the status to it, and then to men,
well, or you can$men[count($men) - 1]['status'] = 'online';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question