Question

I'm quite new to PHP and I'm havint some trouble understanding how I'm supposed to turn something like this:

Array
(
    [name] => Array
        (
            [0] => Bob
            [1] => Tom
            [2] => Ryan
            [3] => Lisa
            [4] => Peter
        )
    [age] => Array
        (
            [0] => 23
            [1] => 33
            [2] => 43
            [3] => 33
            [4] => 29
        )
)

Into this kind of an array:

   Array
    (
        [person] => Array
            (
               [name] => Bob
               [age] => 23
            )
        [person] => Array
            (
               [name] => Tom
               [age] => 33
            )
    )

So I wan't to be able to have a person key, rather than name and age keys. And to put identical indexes from name and age into this person key. How would I go about achieving this?

Thanks.

Était-ce utile?

La solution 3

The key, by definition, must be unique. What you can do is, make an array called $persons, with the following data:

Array (
    [1] => Array (
               [name] => Bob
               [age] => 23
           )
    [2] => Array (
               [name] => Tom
               [age] => 33
           )
    ...
)

You can do this as follows:

$persons = Array();

for($i = 0; $i < sizeof($arr["name"]); $i++) {
    $persons[$i]["name"] = $arr["name"][$i];
    $persons[$i]["age"] = $arr["age"][$i];
}

Sample output:

Array
(
    [0] => Array
        (
            [name] => Bob
            [age] => 23
        )

    [1] => Array
        (
            [name] => Tom
            [age] => 33
        )

    [2] => Array
        (
            [name] => Ryan
            [age] => 43
        )

    ...    
)

Autres conseils

It is impossible to have many person keys in array because keys have to be unique.

Just try with:

$input = [
    'name' => ['Bob', 'Tom', 'Ryan', 'Lisa', 'Peter'],
    'age'  => [23, 33, 43, 33, 29],
];

$output = array_map(function($name, $age) {
    return ['name' => $name, 'age' => $age];
}, $input['name'], $input['age']);

var_dump($output);

Output:

array (size=5)
  0 => 
    array (size=2)
      'name' => string 'Bob' (length=3)
      'age' => int 23
  1 => 
    array (size=2)
      'name' => string 'Tom' (length=3)
      'age' => int 33
  2 => 
    array (size=2)
      'name' => string 'Ryan' (length=4)
      'age' => int 43
  3 => 
    array (size=2)
      'name' => string 'Lisa' (length=4)
      'age' => int 33
  4 => 
    array (size=2)
      'name' => string 'Peter' (length=5)
      'age' => int 29

Easiest way is this:

// Create a main array for the 'people' to be stored in
$people = array();

// Create a person
$bob = array('name' => 'Bob', 'age' => 23);
// Add that person into the 'people' array
$people[] = $bob;

// Repeat as many times a necessary
$tom = array('name' => 'Tom', 'age' => 33);
$people[] = $tom;

$ryan = array('name' => 'Ryan', 'age' => 43);
$people[] = $ryan;

To see the output do:

var_dump($people);

Should produce something like:

array(3) {
  [0]=>
  array(2) {
    ["name"]=>
    string(3) "Bob"
    ["age"]=>
    int(23)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(3) "Tom"
    ["age"]=>
    int(33)
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(4) "Ryan"
    ["age"]=>
    int(43)
  }
}

This is because you can automatically append items to the end of arrays without needing to specify the key you wish to insert them at. This is what we're doing with the lines:

$people[] = $bob;

Essentially that says, add the variable '$bob' to the next available slot in the array. As the array is initially empty, that happens to be the '0' key, and so on.

like

$array=new Array();

$array[]=array('name'=>'Jhon Doe','age'=>23]

and so on for each person you want to store.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top