Davis
Davis

Reputation: 361

How to convert array to multidimensional array with special keys using php?

I have an array

$array = [
    0=>1 
    1=>Jon 
    2=>[email protected] 
    3=>2 
    4=>Doe 
    5=>[email protected] 
    6=>3
    7=>Foo 
    8=>[email protected]
]

What I`d like to do is to add and extra value to each value. Something like this so I can access it when looping through the array

$array=[
    0=>1[id] 
    1=>Jon[name] 
    2=>[email protected][email] 
    3=>2[id] 
    4=>Doe[name] 
    5=>[email protected][email] 
    6=>3[id] 
    7=>Foo[name] 
    8=>[email protected][email]
]

I guess it would be a multidimensional array? What would be the proper way of doing it?

Upvotes: 3

Views: 103

Answers (6)

Prakash Pandey
Prakash Pandey

Reputation: 75

You can also do like this:

$array1 = ["name"=>"alaex","class"=>4];
$array2 = ["name"=>"aley","class"=>10];
$array3 = ["student"=>$array1,"student2"=>$array2];

print_r($array3);

Upvotes: 0

Elementary
Elementary

Reputation: 1453

Use array_map on the result of array_chunck this way:

 $array=array_map(function($val){ return array_combine(['id','name','email'],$val);}, array_chunk($array,3));

note that the second parameter of array_chunk depend of the number of columns and the first array used in array_combine too

see the working code here

Upvotes: 0

Mohammad
Mohammad

Reputation: 21499

Loop through array and check key of items and based of it create new array and insert values in it.

$newArr = [];    
foreach($array as $key=>$value){
    if ($key % 3 == 0)
        $newArr[] = ["id" => $value];
    if ($key % 3 == 1)
        $newArr[sizeof($newArr)-1]["name"] = $value;
    if ($key % 3 == 2)
        $newArr[sizeof($newArr)-1]["email"] = $value;
}

Check result in demo

Upvotes: 3

Martin Heralecký
Martin Heralecký

Reputation: 5789

A simple, yet often-used solution is to use multidimensional array with string keys for better readability:

$array = [
    0 => [
        'id'    => 1,
        'name'  => 'Jon',
        'email' => '[email protected]',
    ],
    1 => [
        'id'    => 2,
        'name'  => 'Doe',
        'email' => '[email protected]',
    ],
    2 => [
        'id'    => 3,
        'name'  => 'Foo',
        'email' => '[email protected]',
    ],
];

You can loop through this like so:

for ($array as $item) {
    // $item['id']
    // $item['name']
    // $item['email']
}

But since PHP is an object-oriented language, I'd suggest creating a class for the data-structure. This is even easier to read and you can very easily add functionality related to the entity etc.

class Person {
    public $id;
    public $name;
    public $email;

    function __construct($id, $name, $email) {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }
}

$array = [
    0 => new Person(1, 'Jon', '[email protected]'),
    1 => new Person(2, 'Doe', '[email protected]'),
    2 => new Person(3, 'Foo', '[email protected]'),
];

You can loop through this like so:

for ($array as $person) {
    // $person->id
    // $person->name
    // $person->email
}

Upvotes: 0

maio290
maio290

Reputation: 6742

Since an array is a map in PHP, I'd recommend to use it like a map or to create a class holding the data.

$arr = array();
$arr[0]['ID'] = 1;
$arr[0]['name'] = "John";
$arr[0]['mail'] = "[email protected];

Example for one class:

 <?PHP
 class User
    {
        public $id;
        public $name;
        public $mail;

        function __construct($ID,$name,$mail)
        {
            $this->id = $ID;
            $this->name = $name;
            $this->mail = $mail;
        }
    }
?>

and then you can simply use it like that:

 <?PHP
    require_once("User.php");
    $user = new User(1,"Mario","[email protected]");
    echo $user->name;
?>

Upvotes: 0

Beginner
Beginner

Reputation: 41

yes just use 2 dimension array

$arr = array();

$arr[0][0] = "1"
$arr[0][1] = "Jon"
$arr[0][2] = "[email protected]"

$arr[1][0] = "2"
$arr[1][1] = "Doe"
$arr[1][2] = "[email protected]"

Upvotes: 0

Related Questions