Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-寻找一种方法来比较二维数组中的值,然后将具有相同值的数组组合成一个三维数组_Php_Arrays_Compare_Array Push - Fatal编程技术网

PHP-寻找一种方法来比较二维数组中的值,然后将具有相同值的数组组合成一个三维数组

PHP-寻找一种方法来比较二维数组中的值,然后将具有相同值的数组组合成一个三维数组,php,arrays,compare,array-push,Php,Arrays,Compare,Array Push,我正在寻找一种方法来实现上述标题的结果。 我有一个这样的二维数组 Array ( [0] => Array ( [reference_no] => A0012 [code] => HSWH30 [net_unit_price] => 24000 [quantity] => 2 ) [1] => Array

我正在寻找一种方法来实现上述标题的结果。 我有一个这样的二维数组

Array
(
    [0] => Array
        (
            [reference_no] => A0012
            [code] => HSWH30
            [net_unit_price] => 24000
            [quantity] => 2
        )

    [1] => Array
        (
            [reference_no] => A0012
            [code] => HSWH15
            [net_unit_price] => 21000
            [quantity] => 2
        )

    [2] => Array
        (
            [reference_no] => A0013
            [code] => HS-106AR
            [net_unit_price] => 2400
            [quantity] => 1
        )

    [3] => Array
        (
            [reference_no] => A0013
            [code] => HS-8012
            [net_unit_price] => 4500
            [quantity] => 2
        )
)
我正在寻找一种使用[reference_no]值比较数组的方法,然后在数组中添加具有相同[reference_no]的数组,从而形成如下所示的三维数组

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [reference_no] => A0012
                    [code] => HSWH30
                    [net_unit_price] => 24000
                    [quantity] => 2
                )

            [1] => Array
                (
                    [reference_no] => A0012
                    [code] => HSWH15
                    [net_unit_price] => 21000
                    [quantity] => 2
                )
        )

    [1] => Array
        (
            [0] => Array
                (
                    [reference_no] => A0013
                    [code] => HS-106AR
                    [net_unit_price] => 2400
                    [quantity] => 1
                )

            [1] => Array
                (

                    [reference_no] => A0013
                    [code] => HS-8012
                    [net_unit_price] => 4500
                    [quantity] => 2
                )       
        )
)
请告诉我需要快速帮助,我已经尝试使用for loop和array\u push来比较和合并/组合类似的数组,但是没有得到我需要的结果。

为什么不试试呢

 $arrayExample[0] => Array
                ( [reference_no] => A0013
                        [code] => HS-8012
                         [net_unit_price] => 4500
                         [quantity] => 2 )
因此从技术上讲,
$arrayExample[0][“reference_no”]的值是A0013
如果是,为什么不使用

if( strpos($arrayExample[0]["reference_no"],"A0013") !==false)
{
    $arrayExample[0]["reference_no"] = array( 
[0] = //something
[1] = //something1
...
)}

希望它有帮助

我将创建一个新的关联数组,并使用参考号作为键。这样就更容易检查新数组中是否存在密钥

$sorted = [];

foreach($array as $el) {
  $ref = $el[reference_no];
  if (array_key_exists($ref, $sorted)) {
    $sorted[$ref][] = $el;
  } else {
    $sorted[$ref] = [$el];
  }
}

如果需要将$sorted转换为序列数字索引(0,1,2,3…),则可以使用array_map或另一个foreach循环作为最后一步。

只需循环数组并创建一个以ref no为键的新关联数组。
这将根据需要对项目进行分组,循环完成后,使用数组值重置数组的索引

foreach($arr as $sub){
    $new[$sub['reference_no']][] = $sub;
}
$new = array_values($new);
var_dump($new);
输出:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0012"
      ["code"]=>
      string(6) "HSWH30"
      ["net_unit_price"]=>
      string(5) "24000"
      ["quantity"]=>
      string(1) "2"
    }
    [1]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0012"
      ["code"]=>
      string(6) "HSWH15"
      ["net_unit_price"]=>
      string(5) "21000"
      ["quantity"]=>
      string(1) "2"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0013"
      ["code"]=>
      string(8) "HS-106AR"
      ["net_unit_price"]=>
      string(4) "2400"
      ["quantity"]=>
      string(1) "1"
    }
    [1]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0013"
      ["code"]=>
      string(7) "HS-8012"
      ["net_unit_price"]=>
      string(4) "4500"
      ["quantity"]=>
      string(1) "2"
    }
  }
}

当你用一个大数组发布一个问题时,最好用json编码或var_导出它。我们不能使用你在这里发布的数据。你看过我的答案了吗?参考值是动态的,看不出有什么帮助。
$arrayExample=Array('reference\u no'=>'A0012','code'=>'HSWH30','net\u unit price'=>24000',quantity'=>2);echo$arrayExample[“参考号”]$arrayExample['reference_no']=array('newData'=>12345,'exampleData'=>56789);回音“
”;echo$arrayExample['reference_no']['newData'];回声“
”;echo$arrayExample['reference_no']['exampleData']$arrayExample['reference_no']['exampleData']=array('anotherData'=>'something');回声“
”;echo$arrayExample['reference_no']['exampleData']['anotherData']谢谢@Andreas这很有用那么它能解决你的问题吗?如果是,请接受答案。