How to short multidimensional array using php? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to short multidimensional array using php?

Shorting multiple dimensions array

13th Aug 2019, 12:26 AM
Rahul Kumar Gupta
Rahul Kumar Gupta - avatar
5 Answers
+ 1
What do you mean by short it? Isn't $x = [[1,2],[3,4]] short enough?
13th Aug 2019, 12:45 AM
Potato Squad
Potato Squad - avatar
+ 1
Maybe you can use an objects and make it shorter with the constructor of the class something like this: class Student { public $name; public $lastName; public $grades; function __construct($name,$lastName,$grades)   {        $this->name=$name; $this->lastName=$lastName; $this->grades=$grades; } } $x=[1=>new Student("potato","squad",[10,20,30]),4=>new Student("raul","kumar",[100,100,100])]; echo $x[1]->name; Now you souldn't insert the name,lastname and grade field everytime. If you still want to do this with key value pair I would recommend you to use function that returns the structure you need or just use constants and array without store the "name" field always.
13th Aug 2019, 1:50 AM
Potato Squad
Potato Squad - avatar
0
Potato Squad no, I want multi dimensions key value pair array shorting, for example. Array of students with their attributes.
13th Aug 2019, 12:48 AM
Rahul Kumar Gupta
Rahul Kumar Gupta - avatar
0
Rahul Kumar Gupta something like this? ["Jack"=>[90,100,90], "Rahul"=>[100,100,100]]
13th Aug 2019, 12:54 AM
Potato Squad
Potato Squad - avatar
0
No, I like $x =[1=>['name':'rahul']]
13th Aug 2019, 12:56 AM
Rahul Kumar Gupta
Rahul Kumar Gupta - avatar