3

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?

1
  • The proper way of doing it would be this way: $array = [ [ 'id'=>1, 'name'=>Jon, 'email'=>[email protected] ], [ 'id'=>2, 'name'=>Doe, 'email'=>[email protected] ], [ 'id'=>3, 'name'=>Foo, 'email'=>[email protected] ] ]; Commented Sep 17, 2018 at 8:33

6 Answers 6

3

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

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

3 Comments

This is the only answer that actually answers the OP's question
One question though. How can I access "string" or "int"?
@Davis If you mean getting int or string value from array, you should use $array[index] to get value. for example $array[1] and type of value not important
0

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]"

Comments

0

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;
?>

Comments

0

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
}

Comments

0

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

7 Comments

@IcedAnt now there is one more answer to solve the question
One question though. How can I access "string" or ''int"?
you don't need to access string or int.it just a dump.You can use $array[0]['id'] or any column else using normal array brackets access
@Davis take a look again to 3v4l.org/Y3ND2 the full working example....the answer marked as the solution can't easily handle columns up to ten without many lines of complex calcul and code.Mine can.
Well the trick is that my code has to check whether data is string or int or date so I actually need to access it. That was the idea behind putting an extra value to value
|
0

You can also do like this:

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

print_r($array3);

Comments

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.