Hướng dẫn create object key value php - tạo giá trị khóa đối tượng php

Tôi đoán câu hỏi của tôi là hai lần

Trong Python nếu tôi làm điều này

x = {key: value}

Tôi đã tạo một từ điển

Trong javascript nếu tôi làm điều tương tự tôi đã tạo một đối tượng

Đó là gì trong PHP? Nó chỉ có thể là tôi không biết các điều khoản

Tôi đang làm việc trong một dự án với Laravel. Về cơ bản, tôi có một cơ sở dữ liệu SQLite mà tôi đang truy cập. Điều này đang trả về một mảng 'đối tượng' (theo thuật ngữ JavaScript). Tôi muốn thực hiện vòng lặp foreach thông qua 'các đối tượng' trong mảng và tôi muốn thêm một khóa và giá trị mới vào 'đối tượng'.

Thứ hai - xin lỗi tôi không chắc chắn 100 phần trăm của cú pháp- trong PHP:

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

Một lần nữa tôi thực sự xin lỗi về cú pháp, tôi đang nhận được mảng trực tiếp từ cơ sở dữ liệu SQLite nên nó không quá quan trọng ở giai đoạn này.

Câu hỏi của tôi là - nếu tôi muốn thêm nói, chiều cao cho mỗi người, làm thế nào để làm điều đó? Hãy để tôi viết lại rằng, tôi biết lặp qua mảng bằng một foreach, tôi chỉ cần lệnh sẽ cho phép tôi nối một cặp khóa/giá trị mới vào cuối từ điển/đối tượng/những gì chúng được gọi trong PHP. Kết quả sẽ là:

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

Tôi biết đó là một câu hỏi siêu đơn giản nhưng tôi đang đấu tranh để tìm ra những câu hỏi đúng đắn để hỏi Google. Nếu tôi đang hỏi sai câu hỏi, xin vui lòng khai sáng cho tôi, tôi đã gãi đầu quá lâu về điều gì đó mà tôi có thể làm dễ dàng bằng ngôn ngữ khác. Tôi thực sự đánh giá cao sự giúp đỡ.

13 năm trước

Mortoray tại ecircle-ag dot comobject, use the new statement to instantiate a class:

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>

17 năm trước

Qeremy [atta] gmail [dotta] com ¶

Nếu một đối tượng được chuyển đổi thành một đối tượng, nó không được sửa đổi. Nếu một giá trị của bất kỳ loại nào khác được chuyển đổi thành một đối tượng, một thể hiện mới của lớp tích hợp std class được tạo. Nếu giá trị là null, phiên bản mới sẽ trống. Một mảng chuyển đổi thành một đối tượng với các thuộc tính được đặt tên bởi các phím và các giá trị tương ứng. Lưu ý rằng trong trường hợp này trước các khóa số PHP 7.2.0 đã không thể truy cập được trừ khi được lặp lại.object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was null, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated.

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>

Đối với bất kỳ giá trị nào khác, một biến thành viên có tên scalar sẽ chứa giá trị.

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>

hữu ích tại Stranger Dot Com ¶

10 năm trước

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:

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

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

Anthony ¶

6 năm trước

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

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

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

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

Twitter/Matt2000 ¶

7 năm trước

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

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

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

Ashley Dambra ¶

8 năm trước

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

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

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

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

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

Nhà phát triển Dot Amankr tại Gmail Dot Com (Aman Kuma) ¶

6 năm trước

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

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

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

Mithras ¶

14 năm trước

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

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

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

Brian Dot Weber1337 tại Gmail Dot Com ¶

5 năm trước

new0

new1

new2

new3

new4

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

mailto dot aurelian tại gmail dot com ¶

12 năm trước

new6

cfreed tại Orange Dot Fr ¶

13 năm trước

new7

new8

new9

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
0

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

Mortoray tại ecircle-ag dot com

17 năm trước

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
2

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
3

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
4

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
5

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
6

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
7

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

Qeremy [atta] gmail [dotta] com ¶

10 năm trước

class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}
$bar = new foo;
$bar->do_foo();
?>
9

null0

null1

null2

null3

null4

AdityCse tại Gmail Dot Com ¶

5 năm trước

null5

null6

null7

mailto dot aurelian tại gmail dot com ¶

12 năm trước

null8

null9

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
0

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
1

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
2

cfreed tại Orange Dot Fr ¶

13 năm trước

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
3

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
4

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

Mortoray tại ecircle-ag dot com

5 năm trước

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
6

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
7

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

mailto dot aurelian tại gmail dot com ¶

12 năm trước

$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
9

scalar0

scalar1

scalar2

scalar3

scalar4

scalar5

scalar6

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

cfreed tại Orange Dot Fr ¶

13 năm trước

scalar8

Mortoray tại ecircle-ag dot com

8 năm trước

scalar9

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
0

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
1

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
2

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
3

17 năm trước

Qeremy [atta] gmail [dotta] com ¶

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
4

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
5

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
6

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
7

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

10 năm trước

17 năm trước

$obj = (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>
9

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 0

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 1

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

Qeremy [atta] gmail [dotta] com ¶

10 năm trước

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 3

AdityCse tại Gmail Dot Com ¶

12 năm trước

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 4

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 5

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 6

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 7

cfreed tại Orange Dot Fr ¶

13 năm trước

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 8

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 9

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

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

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

Mortoray tại ecircle-ag dot com

13 năm trước

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

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

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

Mortoray tại ecircle-ag dot com

14 năm trước

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

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

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

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

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

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

Ashley Dambra ¶

8 năm trước

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

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

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

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

Đối tượng STDCLASS là gì?

STDCLASS là lớp trống trong PHP được sử dụng để đúc các loại khác để đối tượng. Nó tương tự như đối tượng Java hoặc Python. STDClass không phải là lớp cơ sở của các đối tượng. Nếu một đối tượng được chuyển đổi thành đối tượng, nó không được sửa đổi.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.

Khóa và giá trị trong mảng PHP là gì?

Array_Keys (mảng $ mảng, hỗn hợp $ search_value, bool $ strict = false): mảng. Array_Keys () trả về các phím, số và chuỗi, từ mảng. Nếu search_value được chỉ định, thì chỉ có các khóa cho giá trị đó được trả về. Nếu không, tất cả các phím từ mảng được trả về.array. array_keys() returns the keys, numeric and string, from the array . If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

Php đối tượng là gì?

Định nghĩa và sử dụng trong PHP, đối tượng là một loại dữ liệu ghép (cùng với các mảng).Giá trị của nhiều loại có thể được lưu trữ cùng nhau trong một biến duy nhất.Đối tượng là một thể hiện của lớp tích hợp hoặc người dùng được xác định.Ngoài các thuộc tính, lớp xác định chức năng được liên kết với dữ liệu.a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.

PHP có phải là một mảng không?

Hàm is_array () kiểm tra xem một biến có phải là một mảng hay không.Hàm này trả về true (1) nếu biến là một mảng, nếu không nó sẽ trả về sai/không có gì.. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.