Làm cách nào để đảo ngược mảng nhiều chiều trong PHP?

Xin chào,

Làm cách nào để sắp xếp một mảng nhiều chiều theo thứ tự ngược lại?

Ví dụ,

$songs =  array[
		'3' => array['artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'],
		'4' => array['artist'=>'The Decemberists', 'songname'=>'The Island'],
		'1' => array['artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News'],
		'2' => array['artist'=>'Jack Johnson', 'songname' =>'Only the Ocean']
	];
	
print_r[$songs];

sẽ dẫn đến điều này,

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]

nhưng tôi muốn có nó theo thứ tự này thay vào đó,

Array
[

   [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

 [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

  [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

]

Tôi muốn giữ các giá trị chính là [2],[1],[4],[3] [thứ tự ban đầu là [3],[4],[1],[2]]

Tôi có cần phải viết một chức năng để làm điều này?

Cảm ơn nhiều,
Lau

Trong bài viết này, chúng ta sẽ xem cách lấy chỉ mục của mảng đa chiều NumPy theo thứ tự ngược lại

Cách tiếp cận

  • Đầu tiên, chúng tôi nhập thư viện NumPy và khởi tạo các tham số cần thiết bao gồm ma trận mà chúng tôi cần làm việc và các tham số cần thiết khác
  • Bây giờ chúng ta sẽ lặp qua từng hàng, lật nó và tìm chỉ mục của phần tử mong muốn trong mỗi hàng theo thứ tự ngược lại và lưu trữ nó trong danh sách [kết quả] mà chúng ta đã tạo
  • Bây giờ chúng tôi đã có tất cả các chỉ mục của mình từ mặt sau của các hàng trong mảng. Bây giờ chúng ta cần chuyển đổi chỉ mục phù hợp để đọc từ trái sang phải, vì vậy, chúng ta trừ độ dài của hàng với mỗi chỉ mục và tiếp tục giảm nó đi một để mảng của chúng ta phù hợp với việc lập chỉ mục bằng 0

Chỉ mục cuối cùng = Tổng số phần tử trong hàng - Chỉ mục hiện tại-1

Thí dụ

Đầu vào

[[1,2,3,4,2],

[2,3,4,1,5],

[2,2,4,3,2],

[1,3,4,2,4]]

đầu ra

[4 0 4 3]

Giải trình. Trong ví dụ trên, chúng tôi đang cố gắng tìm cách lập chỉ mục xuất hiện đầu tiên của '2' theo thứ tự ngược lại và chúng tôi có 5 phần tử trong mỗi hàng và sau khi lập chỉ mục ngược, chúng tôi nhận được một mảng như [0, 4, 0, 1] hiện đang sử dụng

final_list[0] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-0-1 =>4

final_list[1] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-4-1 =>0

final_list[2] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-0-1 =>4

final_list[3] => Tổng số phần tử trong hàng – Chỉ mục hiện tại- 1 => 5-1-1 =>3

ví dụ 1

Python3




#import Modules

import numpy as np

 

# initialize parameters

x__=

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
0
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
1
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import0
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import2
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import4
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import0import7

import8import9import0

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import2
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import4
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
1
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2numpy as np8import7

import8import9import0

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import0
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import4
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import2
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import0import7

import8import9

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
1
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import2
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import4
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import0
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import4x3

x4= x6x7x8x9

=0= =2

 

=4

=5 =6=7 =8

=9=6=

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
02

________ 99

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
04= ________ 106== ________ 40
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
10

=9

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
12____88
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
14x8x9

 

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
18

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
19

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
20= x4
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
23
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
24
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
23
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
1

 

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
28

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
29import00

đầu ra

[4 0 4 3]

ví dụ 2

Phương pháp trên cũng có thể hoạt động đối với chuỗi. Trong ví dụ bên dưới, chúng tôi đang cố gắng tìm chỉ mục xuất hiện đầu tiên của 'Sam' theo thứ tự ngược lại

Python3




#import Modules

import numpy as np

 

# initialize parameters

x__=

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
0____409
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2____411
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import13import7

import8import9____409

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2____409
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import21import7

import8import9____425

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2____411
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import09import7

import8import9____409

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2____425
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import37import38

x4= x6x7x8import44

=0= import47

 

=4

=5 =6=7 import53

=9=6=

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
02

________ 99

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
04= ________ 106== ________ 409
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
10

=9

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
12____88
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
14x8import44

 

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
18

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
19

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
20= x4
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
23
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
24
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
23
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
1

 

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
28

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
29import00

đầu ra

[ 0 1 2 ]

ví dụ 3

Đối với dữ liệu Boolean, chúng tôi có cách tiếp cận tương tự, nhưng vì nó chỉ có 0 hoặc 1 làm giá trị nên chúng tôi có thể sử dụng argmax[] để tìm chỉ mục của giá trị cao nhất [đối với mỗi hàng có trục=1]. Vì True tương đương với 1 và False tương đương với 0 nên nó sẽ ghi chỉ số của giá trị True đầu tiên

Python3




import86

import numpy as np

 

# initialize parameters

import91=

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
0import94
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import96
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import94
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import94import7

import8import9import94

Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import96
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import96
Array
[
    [3] => Array
        [
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        ]

    [4] => Array
        [
            [artist] => The Decemberists
            [songname] => The Island
        ]

    [1] => Array
        [
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        ]

    [2] => Array
        [
            [artist] => Jack Johnson
            [songname] => Only the Ocean
        ]

]
2import94numpy as np31

Làm cách nào để đảo ngược mảng nhiều chiều trong PHP?

chưa được kiểm tra. function reveseme[&$array]{$array=array_reverse[$array];return;} $array=array[1,2,3,4,5]; . This should do it.

Làm cách nào để đảo ngược một mảng trong PHP?

Hàm array_reverse[] trả về một mảng theo thứ tự ngược lại.

Làm cách nào để lặp lại một mảng theo thứ tự đảo ngược PHP?

Cách tiếp cận 1. Sử dụng vòng lặp for , vòng lặp for giảm dần có thể được bắt đầu để truy cập các phần tử theo thứ tự ngược lại. Chỉ số bắt đầu ở độ dài của mảng – 1 cho đến khi nó chạm đến đầu mảng.

Làm cách nào chúng ta có thể chuyển đổi một mảng nhiều chiều thành chuỗi mà không cần bất kỳ vòng lặp nào?

Có thể trong trường hợp bạn cần chuyển đổi một mảng nhiều chiều thành một chuỗi, bạn có thể muốn sử dụng hàm print_r[] . Đây còn được gọi là “mảng có phím”. Bằng cách thêm “true” làm tham số thứ hai, tất cả nội dung của mảng sẽ được chuyển thành chuỗi.

Chủ Đề