Hướng dẫn php get multiple values from array - php nhận nhiều giá trị từ mảng

0

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Làm thế nào tôi có thể lọc qua từng mảng trong mảng này để chỉ lấy một số phần dữ liệu nhất định như

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
6,
array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
7 và
array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
8?

Tôi đã sử dụng

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
9 để chỉ nhận được một thông tin nhưng làm thế nào tôi có thể nhận được nhiều thông tin?

$object = $ticket->find[];
$arr = json_decode[json_encode[$object], true];
var_dump[$arr];

Đoạn mã ở trên là những gì trả về mảng dữ liệu.

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]

Đầu ra dự kiến:

array[2] {
    [0]=> array[3] {
        [0]=> string[1] "1"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 8"
    }
    [1]=> array[3] {
        [0]=> string[1] "2"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 7"
    }
}

Sahil Gulati

14.8k4 Huy hiệu vàng24 Huy hiệu bạc42 Huy hiệu đồng4 gold badges24 silver badges42 bronze badges

Đã hỏi ngày 25 tháng 5 năm 2017 lúc 15:18May 25, 2017 at 15:18

Trenton Tylertrenton TylerTrenton Tyler

1.6623 huy hiệu vàng22 Huy hiệu bạc48 Huy hiệu đồng3 gold badges22 silver badges48 bronze badges

2

Ở đây chúng tôi đang sử dụng

array[2] {
    [0]=> array[3] {
        [0]=> string[1] "1"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 8"
    }
    [1]=> array[3] {
        [0]=> string[1] "2"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 7"
    }
}
0 để đạt được đầu ra mong muốn.

Giải pháp 1:

Hãy thử đoạn mã này ở đây

$array=array_map[function[$value]{
    return array[$value["tid"],$value["uid"],$value["subject"]];
},$array];
print_r[$array];

Giải pháp 2:

Hãy thử đoạn mã này ở đây

foreach[$array as $value]
{
    $result[]=array[$value["tid"],$value["uid"],$value["subject"]];
}
print_r[$result];

Giải pháp 2:May 25, 2017 at 15:23

Đã trả lời ngày 25 tháng 5 năm 2017 lúc 15:23Sahil Gulati

14.8k4 Huy hiệu vàng24 Huy hiệu bạc42 Huy hiệu đồng4 gold badges24 silver badges42 bronze badges

7

Đã hỏi ngày 25 tháng 5 năm 2017 lúc 15:18

array_map[function[$v]{return [$v['tid'], $v['uid'], $v['device']];}, $array];

Giải pháp 2:May 25, 2017 at 15:23

Đã trả lời ngày 25 tháng 5 năm 2017 lúc 15:23

foreach [$arr as $ar] {
    foreach [$ar as $key => $value] {
        if [$key == 'tid' | $key == 'uid' | $key == 'device'] {
            echo $key . ': ' . $value;
        }
    }
}

Sahil Gulatisahil GulatiMay 25, 2017 at 15:24

Sử dụng

array[2] {
    [0]=> array[3] {
        [0]=> string[1] "1"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 8"
    }
    [1]=> array[3] {
        [0]=> string[1] "2"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 7"
    }
}
0,jkeys

Bạn có thể sử dụng vòng lặp kép

array[2] {
    [0]=> array[3] {
        [0]=> string[1] "1"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 8"
    }
    [1]=> array[3] {
        [0]=> string[1] "2"
        [1]=> string[2] "22"
        [2]=> string[8] "iPhone 7"
    }
}
2 để đi qua và sau đó in ra các giá trị mà bạn thích.3 silver badges10 bronze badges

5.5.5. Loại bỏ và chèn các phần tử trong một mảng

Hàm mảng_splice [] có thể xóa hoặc chèn các phần tử vào một mảng:

$removed = array_splice[array, start [, length [, replacement ] ]];

Chúng tôi sẽ xem array_splice [] bằng mảng này:

$subjects = array['physics', 'chem', 'math', 'bio', 'cs', 'drama', 'classics'];

Chúng ta có thể xóa các phần tử toán học, tiểu sử và CS bằng cách nói với Array_Splice [] để bắt đầu ở vị trí 2 và xóa 3 phần tử:

$removed = array_splice[$subjects, 2, 3];
// $removed is array['math', 'bio', 'cs']
// $subjects is array['physics', 'chem'];

Nếu bạn bỏ qua độ dài, Array_Splice [] sẽ loại bỏ đến cuối mảng:

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
0

Nếu bạn chỉ muốn xóa các yếu tố và bạn không quan tâm đến các giá trị của chúng, bạn không cần phải gán kết quả của Array_Splice []:

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
1

Để chèn các phần tử nơi những người khác bị xóa, hãy sử dụng đối số thứ tư:

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
2

Kích thước của mảng thay thế không phải giống như số lượng các phần tử bạn xóa. Mảng phát triển hoặc co lại khi cần thiết:

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
3

Để có được hiệu ứng chèn các phần tử mới vào mảng, hãy xóa các phần tử bằng không:

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
4

Mặc dù các ví dụ cho đến nay đã sử dụng một mảng được lập chỉ mục, Array_Splice [] cũng hoạt động trên các mảng kết hợp:

array [size=2]
  0 => 
    array [size=9]
      'tid' => string '1' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 8' [length=8]
      'issue' => string 'iPhone 8 screen replacement' [length=27]
      'device' => string 'iPhone 8' [length=8]
      'created' => string '2017-05-25 00:01:11' [length=19]
      'identifier' => string '29cd54bf' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
  1 => 
    array [size=9]
      'tid' => string '2' [length=1]
      'uid' => string '22' [length=2]
      'subject' => string 'iPhone 7' [length=8]
      'issue' => string 'iPhone 7 screen replacement' [length=27]
      'device' => string 'iPhone 7' [length=8]
      'created' => string '2017-05-25 00:27:42' [length=19]
      'identifier' => string 'b47f2c82' [length=8]
      'status' => string 'New' [length=3]
      'tech' => string 'None' [length=4]
5

Làm thế nào tôi có thể nhận được nhiều giá trị của mảng trong PHP?

Truy xuất các giá trị: Chúng ta có thể truy xuất giá trị của mảng đa chiều bằng phương pháp sau:..
Sử dụng khóa: Chúng ta có thể sử dụng khóa của mảng kết hợp để truy xuất trực tiếp giá trị dữ liệu. ....
Sử dụng Foreach Loop: Chúng ta có thể sử dụng vòng lặp foreach để truy xuất giá trị của từng khóa được liên kết bên trong mảng kết hợp đa chiều ..

Làm thế nào để bạn tìm thấy nhiều giá trị trong một mảng?

Để kiểm tra xem có tồn tại nhiều giá trị trong một mảng không: sử dụng phương thức mỗi [] để lặp qua mảng giá trị hay không.Nếu tất cả các giá trị tồn tại trong mảng, mọi phương thức sẽ trả về true.Use the every[] method to iterate over the array of values. On each iteration, use the indexOf method to check if the value is contained in the other array. If all values exist in the array, the every method will return true .

Làm thế nào tôi có thể nhận được nhiều giá trị trong PHP?

PHP không hỗ trợ để trả về nhiều giá trị trong một hàm.Bên trong một hàm khi câu lệnh trả về đầu tiên được thực thi, nó sẽ điều khiển trực tiếp trở lại hàm gọi và câu lệnh trả về thứ hai sẽ không bao giờ được thực thi.. Inside a function when the first return statement is executed, it will direct control back to the calling function and second return statement will never get executed.

Làm thế nào để bạn trích xuất giá trị 3 từ mảng trong PHP?

Hàm trích xuất [] nhập các biến vào bảng ký hiệu cục bộ từ một mảng.Hàm này sử dụng các phím mảng làm tên và giá trị biến làm giá trị biến.Đối với mỗi phần tử, nó sẽ tạo một biến trong bảng ký hiệu hiện tại.Hàm này trả về số lượng biến được trích xuất khi thành công.. This function uses array keys as variable names and values as variable values. For each element it will create a variable in the current symbol table. This function returns the number of variables extracted on success.

Bài Viết Liên Quan

Chủ Đề