Php create key value object

I guess my question is two-fold

In Python if I do this

x = {key: value}

I've created a dictionary

In javaScript if I do the same thing I've created an object

What is that in PHP? It could just be that I don't know the terms

I'm working on a project with laravel. I basically have an sqlite database which I am accessing. This is returning an array of 'objects'[in javaScript terms]. I want to do a foreach loop through the 'objects' in the array and I want to add a new key and value to the 'object'.

Secondly - Sorry I'm not 100 percent certain of the syntax- in php:

$person = [0 => {name: "Jake", age: 22},
           1 => {name: "Chris", age: 34}];

Again I'm really sorry about the syntax, I'm receiving the array directly from the sqlite database so it isn't overly important at this stage.

My question is - If I want to add say, a height to each person, how do I do that? Let me rephrase that, I know to loop through the array with a foreach, I just need the command which will allow me to append a new key/value pair to the end of a Dictionary/Object/What they are called in PHP.The result would be:

$person = [0 => {name: "Jake", age: 22, height: "2m"},
          1 => {name: "Chris", age: 34, height: "3m"}];

I know it is a super simple question but I'm struggling to find the right questions to ask google. If I am asking the wrong question please enlighten me I've been scratching my head for too long on something that I could do so easily in another language. I really appreciate the help.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP offers us a special type of array called an Associative Array that allows us to create an array with Key-Value pairs. The syntax for creating an Associative Array is as follows:

    Syntax 1: Using array[] constructor

    $arrayVariable = array[
        key1  => value1,
        key2 => value2,
        key3 => value3,
        ...
        keyN => valueN,
    ];
    

    Syntax 2: Using shorthand notation

    $arrayVariable = [
        key1  => value1,
        key2 => value2,
        key3 => value3,
        ...
        keyN => valueN,
    ];
    

    Note:

    1. The comma after the last Key-Value pair is optional.
    2. The Key can be of either integer or string type.
    3. The Value can be of any valid type.
    4. If the type of Key is other that string or integer, it will be cast to string or integer depending on the type of Key.

    Example 1: Using array[] constructor

    Output:

    Example 2: Using shorthand notation

    Output:


    How do you create an object in PHP?

    To create an Object in PHP, use the new operator to instantiate a class. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.

    How get the key of an object in PHP?

    You can cast the object to an array like this: $myarray = [array]$myobject; And then, for an array that has only a single value, this should fetch the key for that value. $value = key[$myarray];

    What is StdClass object in PHP?

    The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

    What is $Key in PHP?

    Technical Details. Return Value: Returns the key of the array element that is currently being pointed to by the internal pointer. PHP Version: 4+

    Chủ Đề