English 中文(简体)
整合在 php中的阵列
原标题:combining arrays in php

我要说的是,两阵列是个阵列,其阵列是年金和类似美元。 他们拥有一个称为“姓名”的关键。 我怎么能够做到:

每年1美元

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [year] => 1950
            [info] => his ghost still haunts us
        )
    [1] => Array
        (
            [name] => FUTUREMAN
            [year] => 1930
            [info] => RIP
        )
)

以及这种类型的美元

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [likes] => cornbread
            [dislikes] => pain
        )
    [1] => Array
        (
            [name] => E. Case
            [likes] => chaos
            [dislikes] => order
        )
    [2] => Array
        (
            [name] => FUTUREMAN
            [likes] => mustard
            [dislikes] => mayo
        )
)

可将第2阵列中的信息添加到第1号,如果“名称”价值与下列内容相匹配:

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [year] => 1950
            [info] => his ghost still haunts us
            [likes] => cornbread
            [dislikes] => pain
        )
    [1] => Array
        (
            [name] => FUTUREMAN
            [year] => 1930
            [info] => RIP
            [likes] => mustard
            [dislikes] => mayo
        )
)

我审视了已经提出的问题,但看不到什么,也许用错误术语描述问题。 我 st着ach子,因为如果我这样说的话,那就不了。

foreach (每年1美元 as $y){
    $complete[]=array( name =>$y[ name ],  year =>$y[ year ],  info =>$y[ info ],  likes =$likes_dislikes[0][ likes ], dislikes =>$likes_dislikes[0][ dislikes ] )
}

我只想给所有人带来同样的价值观。 这样做的最简单方式是什么?

最佳回答

我试图做的是,通过两套阵列进行 lo,(一种被nes为ach,是一种很好的选择),检查两种阵列中的名称相同的情况。 如果是的话,你可以使用阵列——合并——将其并入新的阵列。 例如:

$newArray = array();
foreach ($arr1 as $first) {
    foreach ($arr2 as $second) {
        if($first["name"] == $second["name"]){
            array_push($newArray, array_merge($first, $second));
        }
    }
}

假设你点名数为1美元和2美元。

问题回答

这里是一线摇.器(我把它从技术上说成一条线),应当奏效。

$complete = array_map(function($a) use($likes_dislikes){
    foreach($likes_dislikes as $ld){
        if($a[ name ] === $ld[ name ]){
            return $a + $ld;
            break;
        }
    }
}, $year_info);

这只是PHP5.3+的工作,否则你就可以这样做:

$complete = array();
foreach($year_info as $a){
    foreach($likes_dislikes as $ld){
        if($a[ name ] === $ld[ name ]){
            $complete[] = $a + $ld;
            break;
        }
    }
}

超级拉齐,外加托办法:

$complete = array();

foreach($year_info as $yr){
  $name = $yr[ name ];
  foreach($likes_dislikes as $ld){
    if($ld[ name ]!=$name){
      continue;
    }else{
      $new_entity = array();
      $new_entity[ name ] = $name;
      $new_entity[ year ] = $yr[ year ];
      $new_entity[ info ] = $yr[ info ];
      $new_entity[ likes ] = $ld[ likes ];
      $new_entity[ dislikes ] = $ld[ dislikes ];
      $complete[] = $new_entity;
      break;
    }
  }
}

虽然这将在数量众多的情况下表现不佳,但如果可能的话,改变数据结构将更有意义。 最好有简单地用名称确定的数据结构。 你是否掌握了你们的投入?

just loop over both arrays to create a third one.

$complete = array();
foreach ($year_info as $year) {
foreach ($like_dislikes as $like {
if ($year[ name ] == $like[ name ]) {
$complete[] = array_merge($year, $like);
}
}
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签