PHP의 배열에서 단일 값을 제거하는 방법

php中世界最好的语言
풀어 주다: 2023-03-22 09:14:01
원래의
2230명이 탐색했습니다.

이번에는 PHP에서 배열에서 단일 값을 검색하는 방법을 보여 드리겠습니다. PHP가 배열에서 단일 값을 검색할 때 주의 사항은 무엇입니까? 다음은 실제 사례입니다.

1. 배열 arr

var_dump(arr)의 값은 다음과 같습니다.

array (size=3)
 'delete' => 
 array (size=3)
  0 => string 'HBSFlyRecode20170222-101501.txt' (length=31)
  1 => string 'HBSFlyRecode20170222-105502.txt' (length=31)
  2 => string 'HBSFlyRecode20170222-108803.txt' (length=31)
 'new' => 
 array (size=3)
  0 => string 'HBSFlyRecode20170223-101504.txt' (length=31)
  1 => string 'HBSFlyRecode20170223-105505.txt' (length=31)
  2 => string 'HBSFlyRecode20170223-108806.txt' (length=31)
 'old' => 
 array (size=3)
  0 => string 'HBSFlyRecode20170221-101507.txt' (length=31)
  1 => string 'HBSFlyRecode20170221-105508.txt' (length=31)
  2 => string 'HBSFlyRecode20170221-108809.txt' (length=31)
로그인 후 복사
echo $arr['old'][0];
打印出: HBSFlyRecode20170221-101507.txt
로그인 후 복사

그러나 arr이 object 형식인 경우 인쇄된 결과는 다음과 같습니다.

var_dump(arr)
object(stdClass)[1]
 public 'delete' => 
 array (size=3)
  0 => string 'HBSFlyRecode20170222-101501.txt' (length=31)
  1 => string 'HBSFlyRecode20170222-105502.txt' (length=31)
  2 => string 'HBSFlyRecode20170222-108803.txt' (length=31)
 public 'new' => 
 array (size=3)
  0 => string 'HBSFlyRecode20170223-101504.txt' (length=31)
  1 => string 'HBSFlyRecode20170223-105505.txt' (length=31)
  2 => string 'HBSFlyRecode20170223-108806.txt' (length=31)
 public 'old' => 
 array (size=3)
  0 => string 'HBSFlyRecode20170221-101507.txt' (length=31)
  1 => string 'HBSFlyRecode20170221-105508.txt' (length=31)
  2 => string 'HBSFlyRecode20170221-108809.txt' (length=31)
로그인 후 복사

할 수 없습니다. $arr['old'][0 ]을 사용하여 값을 얻은 경우 arr 객체 및 배열에 공통적인 foreach 메서드를 사용하여 값을 얻을 수 있습니다.

function getValue($arr){
 foreach($arr as $key => $value){
  if(is_array($value)){
   getValue($value);
  }else{
   echo $value."<br>";
  }
 }
}
로그인 후 복사

arr이 객체 형식인 경우 변환할 수 있습니다. 다음은 객체를 배열 형식으로 변환하는 방법입니다.

1. $object_json = json_encode($arr); 얻는 것은 객체입니다.

$json = json_encode($arr,true); json

2.json_decode($object_json) 및 json_decode($json) 당신이 얻는 것은 배열 객체입니다.

json_decode($object_json,true) 및 json_decode($json,true) 당신이 얻는 것은 배열입니다.

정리하자면, 배열 객체를 배열로 변환하는 방법은 프로젝트에서

arr=jsondecode(jsonencode(arr=jsondecode(jsonencode(arr,true),true);
로그인 후 복사

입니다. 이 문제를 발견한 후, php에서 json과 배열을 변환할 때 json_encode()와 json_decode의 두 번째 매개변수인 json_encode()를 사용하는 것이 좋습니다. ()는 true로 추가되어야 합니다. 즉:

json_encode(arr,true);jsondecode(arr,true);jsondecode(json,true);
로그인 후 복사

이 글의 사례를 읽으신 후 방법을 마스터하셨다고 믿습니다. 자세한 내용은 PHP 중국어 웹사이트의 다른 관련 글도 주목해 주세요!

추천 도서:

PHP에서 키 값 유지 + 배열 병합에 대한 자세한 설명

2차원 배열에서 중복 값을 제거하는 방법

연관 배열과 인덱스를 구별하는 방법 배열

위 내용은 PHP의 배열에서 단일 값을 제거하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!