【问题标题】:PHP 1:1 to 1:many arrayPHP 1:1 到 1:many 数组
【发布时间】:2014-06-21 17:53:45
【问题描述】:

我有三个数组,分别称为 associativeArray keyArrayvalueArrayassociativeArray 是一个由键/值对组成的数组,这些对被拆分并放入keyArrayvalueArray。我现在要做的是创建一个名为newArray 的第四个数组,它使用valueArray 中的元素作为其键,并且值来自它们各自的keyArray。但与具有 1:1 键值对的 associativeArray 不同,我希望 newArray 具有 1:many 键值对,而没有任何重复键。这是我为它编写的代码:

foreach($keyArray as $keyElement){
    $valueElement = $associativeArray[$keyElement];
    if (!in_array($valueElement,$newArray)){
        array_push($newArray, $valueElement => array($keyElement));
    }
    else{
        array_push($newArray[$valueElement],$keyElement);
    }
}

但是每当我运行它时,我都会得到:

PHP Parse error:  syntax error, unexpected T_DOUBLE_ARROW

【问题讨论】:

    标签: php arrays multidimensional-array hashmap associative-array


    【解决方案1】:

    您不需要所有这些数组。只需associativeArray 就足够了。

    你可以这样做:

    $newArray = array();
    // This will loop in the array so that, in each step:
    //  - $k is the current key
    //  - $v is the current value
    foreach($associativeArray as $k => $v) {
        // If newArray doesn't already have $v as a key
        if (!array_key_exists($v, $newArray)) {
            // Define it as a new array with only one element
            $newArray[$v] = array($k);        
        } else {
            // If it already exists, just push $k to it's elements
            $newArray[$v][] = $k; // This is the same as array_push($newArray[$v], $k)
        }
    }
    

    【讨论】:

      【解决方案2】:

      最简单的解决方案可能是这样的

      foreach( $keyArray as $key )
      {
        // get the value from the associative array
        $value = $associativeArray[ $key ];
      
        // check if the key was set before into $newArray
        // if you don't check and initialize it, you'll get an error
        // when you try to add new elements
        if( !array_key_exists( $key, $newArray ) )
        {
             $newArray[ $key ] = array();
        }
      
        // To prevent duplicated ( like you have in your code ) 
        // you just have to check if the element is in the $newArray
        if ( !in_array( $value, $newArray[ $key ] ) )
        {
            $newArray[ $key ][] = $value; // if you wanna use array_push --> array_push( $newArray[ $key ], $value );
        }
      }
      

      但是,这取决于您的目标,我会使用引用重写 associativeArray 并将值转换为数组。那时你不需要这 3 个额外的数组,只需要 associativeArray 和一行代码

      // this will cast each value of the array. Since
      // the value is referenced the array will be updated.
      // You don't have to check for duplicated value because it was an 
      // array 'key' => 'val' so it is not possible to have multiple values 
      // with the same key
      foreach( $associativeArray as $key => &$val )
      {
         $val = (array) $val;
      }
      

      【讨论】:

      • 对我来说仍然是 1:1 数组。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      相关资源
      最近更新 更多