Php 如何向多维数组添加值?
$array1:Php 如何向多维数组添加值?,php,arrays,multidimensional-array,array-difference,Php,Arrays,Multidimensional Array,Array Difference,$array1: I compare these two arrays: $array2: array(3) { [0]=> array(10000) { ["12345"]=> array(5) { ["dateTime"]=> string(19) "2016-10-12 19:46:25" ["
I compare these two arrays:
$array2:
array(3) {
[0]=>
array(10000) {
["12345"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "monkey.jpg"
["path"]=>
string(149) "Volumes/animals/monkey.jpg"
["size"]=>
string(7) "2650752"
}
["678790"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "elephant.jpg"
["path"]=>
string(171) "Volumes/animals/elephant.jpg"
["size"]=>
string(7) "2306688"
}
["23123"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "fred.jpg"
["path"]=>
string(171) "Volumes/animals/fred.jpg"
["size"]=>
string(7) "2306688"
}
}
[1]=>
array(10000) {
["624245"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "duck.jpg"
["path"]=>
string(149) "Volumes/animals/duck.jpg"
["size"]=>
string(7) "2650752"
}
["934522"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "lora.jpg"
["path"]=>
string(171) "Volumes/animals/lora.jpg"
["size"]=>
string(7) "2306688"
}
["832452"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "sam.jpg"
["path"]=>
string(171) "Volumes/animals/sam.jpg"
["size"]=>
string(7) "2306688"
}
}
[2]=>
array(1500) {
["234214"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "chi.jpg"
["path"]=>
string(149) "Volumes/animals/chi.jpg"
["size"]=>
string(7) "2650752"
}
["942134"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "ape.jpg"
["path"]=>
string(171) "Volumes/animals/ape.jpg"
["size"]=>
string(7) "2306688"
}
["234234"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "cat.jpg"
["path"]=>
string(171) "Volumes/animals/cat.jpg"
["size"]=>
string(7) "2306688"
}
}
}
我将其与此函数进行比较:
array(3) {
[0]=>
array(10000) {
["12345"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "monkei.jpg"
["path"]=>
string(149) "Volumes/animals/monkey.jpg"
["size"]=>
string(7) "2650752"
}
["678790"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "elephant.jpg"
["path"]=>
string(171) "Volumes/animals/elephant.jpg"
["size"]=>
string(7) "2306688"
}
["23123"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "fred.jpg"
["path"]=>
string(171) "Volumes/animals/fred.jpg"
["size"]=>
string(7) "2306688"
}
}
[1]=>
array(10000) {
["624245"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "duck.jpg"
["path"]=>
string(149) "Volumes/animals/duck.jpg"
["size"]=>
string(7) "2650752"
}
["934522"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "lora.jpg"
["path"]=>
string(171) "Volumes/animals/lora.jpg"
["size"]=>
string(7) "2306688"
}
["832452"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "sam.jpg"
["path"]=>
string(171) "Volumes/animals/sam.jpg"
["size"]=>
string(7) "2306688"
}
}
[2]=>
array(1500) {
["234214"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 19:46:25"
["fileName"]=>
string(46) "chi.jpg"
["path"]=>
string(149) "Volumes/animals/chi.jpg"
["size"]=>
string(7) "2650752"
}
["942134"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "ape.jpg"
["path"]=>
string(171) "Volumes/animals/ape.jpg"
["size"]=>
string(7) "2306688"
}
["234234"]=>
array(5) {
["dateTime"]=>
string(19) "2016-10-12 14:39:43"
["fileName"]=>
string(45) "cat.jpg"
["path"]=>
string(171) "Volumes/animals/cat.jpg"
["size"]=>
string(7) "2306688"
}
}
}
但我需要的是,无论差异在哪里,我也需要始终将路径放在数组中。我试图将$value[“path”]
添加到数组$difference
,但实际上不知道如何操作
我需要的结果是:
array(1) {
[0]=>
array(1) {
["12345"]=>
array(2) {
["fileName"]=>
string(46) "monkey.jpg"
["path"]=>
string(46) "Volumes/animals/monkey.jpg"
}
}
}
试试这个
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
$difference['path'] = $value['path'];
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
$difference['path'] = $value['path'];
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
$difference['path'] = $new_diff['path'];
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
函数数组\u diff\u assoc\u递归($array1,$array2){
foreach($array1作为$key=>$value){
if(是_数组($value)){
如果(!isset($array2[$key])){
$difference[$key]=$value;
$difference['path']=$value['path'];
}
elseif(!is_数组($array2[$key])){
$difference[$key]=$value;
$difference['path']=$value['path'];
}
否则{
$new_diff=array_diff_assoc_recursive($value,$array2[$key]);
如果($new_diff!=FALSE)
{
$difference[$key]=$new_diff;
$difference['path']=$new_diff['path'];
}
}
}
elseif(!isset($array2[$key])| |$array2[$key]!=$value){
$difference[$key]=$value;
}
}
返回!设置($difference)?0:$difference;
}
回声“;
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
efunction array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
$difference['path'] = $array1['path']; // added this feature here
}
}
return !isset($difference) ? 0 : $difference;
打印(数组差异关联递归($array1,$array2));
回声“;
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
efunction array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
$difference['path'] = $array1['path']; // added this feature here
}
}
return !isset($difference) ? 0 : $difference;
试试这个
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
$difference['path'] = $value['path'];
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
$difference['path'] = $value['path'];
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
$difference['path'] = $new_diff['path'];
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
函数数组\u diff\u assoc\u递归($array1,$array2){
foreach($array1作为$key=>$value){
if(是_数组($value)){
如果(!isset($array2[$key])){
$difference[$key]=$value;
$difference['path']=$value['path'];
}
elseif(!is_数组($array2[$key])){
$difference[$key]=$value;
$difference['path']=$value['path'];
}
否则{
$new_diff=array_diff_assoc_recursive($value,$array2[$key]);
如果($new_diff!=FALSE)
{
$difference[$key]=$new_diff;
$difference['path']=$new_diff['path'];
}
}
}
elseif(!isset($array2[$key])| |$array2[$key]!=$value){
$difference[$key]=$value;
}
}
返回!设置($difference)?0:$difference;
}
回声“;
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
efunction array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
$difference['path'] = $array1['path']; // added this feature here
}
}
return !isset($difference) ? 0 : $difference;
打印(数组差异关联递归($array1,$array2));
回声“;
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
efunction array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
$difference['path'] = $array1['path']; // added this feature here
}
}
return !isset($difference) ? 0 : $difference;
我已经更改了您的功能,现在它可以按照您的预期工作:
}我已经更改了您的功能,现在它可以按照您的预期工作:
}测试结果:
array(2){[0]=>array(2){[“12345”]=>array(1){[“fileName”]=>string(10)“monkey.jpg”}[“path”]=>NULL}[“path”]=>NULL}
你能回显$value[“path”];代替$difference['path']=$value['path'];测试结果:array(2){[0]=>array(2){[“12345”]=>array(1){[“fileName”]=>string(10)“monkey.jpg”}[“path”]=>NULL}[“path”]=>NULL}
能否回显$value[“path”];代替$difference['path']=$value['path'];并检查值是否正在打印是的,它只打印一次。如您所见,Akshay Khairmode仅使用$array1值作为结果输出,因此我们只需要它的“路径”字段。现在,我们需要将这个字段值放在$difference中,与$array2中的字段不同。这个特殊的位置在顶级“else-if”部分的内部。这部分是这个递归的“底部”。我将这些解释作为对用户问题的回答,但此问题已被删除。所以我就把它放在这里。可能有人需要它做些什么。正如您所看到的,Akshay Khairmode只使用$array1值作为结果输出,所以我们只需要它的“路径”字段。现在,我们需要将这个字段值放在$difference中,与$array2中的字段不同。这个特殊的位置在顶级“else-if”部分的内部。这部分是这个递归的“底部”。我将这些解释作为对用户问题的回答,但此问题已被删除。所以我就把它放在这里。也许有人需要它做点什么。
function array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
echo "<pre>";
print_r(array_diff_assoc_recursive($array1, $array2));
echo "</pre>";
efunction array_diff_assoc_recursive($array1, $array2){
foreach($array1 as $key => $value){
if(is_array($value)){
if(!isset($array2[$key])){
$difference[$key] = $value;
}
elseif(!is_array($array2[$key])){
$difference[$key] = $value;
}
else{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value){
$difference[$key] = $value;
$difference['path'] = $array1['path']; // added this feature here
}
}
return !isset($difference) ? 0 : $difference;