John Davidson

Array of scalars and vectors at the same time in PHP


I have to create an array of the following "structure" in PHP:


Basically, for each user_id, there is an array of (item_id, flag, and an array of feedback_messages)..

Within a "classes" table, I have a field called "users" that holds data with separators. An example of data will be:

I would need to put the data into an array as follows:

$myArray[0][0]="1";       // the first user's user_id = 0
$myArray[0][1][0]="5"; // the first user's first item's id = 5
$myArray[0][1][1]="1"; // the first user's first item's flag = 0
$myArray[0][1][2][0]="1"; // the first user's first item's first feedback message's id = 1
$myArray[0][1][2][1]="2"; // the first user's first item's second feedback message's id = 2
$myArray[0][1][2][2]="5"; // the first user's first item's third feedback message's id = 3

I don't know how to create such an array and how to retrieve data from it (syntax-wise)... I worked with single-dimensional arrays and multidimensional array, but this is an array partially bi-dimensional, partially tri and four-dimensional... is that even possible?

My Answer:
In PHP, you can create an array that contains both scalars (single values) and vectors (arrays) by simply adding them as elements in the array. Here is an example of how you can create an array with scalars and vectors:

$array = array(
1, // scalar value
array(2, 3, 4), // vector value
5, // scalar value
array(6, 7, 8) // vector value


In this example, the array contains scalar values `1` and `5`, as well as vector values `array(2, 3, 4)` and `array(6, 7, 8)`. You can access these values using their respective indexes in the array.

Rate this post

3 of 5 based on 5998 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us