基于键值的组数组

时间:2016-05-15 19:58:06

标签: php arrays csv

是的,通过使用不同的方法已经无数次地回答了这个问题,在大多数情况下它是有效的但不是我的情况,请允许我解释。

我正在从CSV文件构建一个数组,这是一个简单的部分,困难的部分是,我的是我必须从第一个数组构建另一个数组,根据一个键值对结果进行分组,问题是这个值不是一个数组,也不是一个简单的字符串...
这是CSV文件数组中的示例

[0] => Array
     (
       [key_1] => FOO
       [cats] => /30/
       [key_2] => FTU-1
     )
[1] => Array
     (
       [key_1] => FOO
       [cats] => /30/
       [key_2] => FTU-2
     )
[2] => Array
     (
       [key_1] => FOO
       [cats] => /30/10/
       [key_2] => FTU-3
     )
[3] => Array
     (
       [key_1] => FOO
       [cats] => /15/
       [key_2] => FTU-4
     )
[4] => Array
     (
       [key_1] => FOO
       [cats] => /10/
       [key_2] => FTU-5
     )
[0] => Array
     (
       [key_1] => FOO
       [cats] => /15/
       [key_2] => FTU-6
     )

最终数组必须基于列cats

[30] => Array 
        (
           [0] => Array 
                 (
                   [key_1] => FOO
                   [cats] => /30/
                   [key_2] => FTU-1
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /30/
                   [key_2] => FTU-2
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /30/10/
                   [key_2] => FTU-3
                 )
[15] => Array 
        (
           [0] => Array 
                 (
                   [key_1] => FOO
                   [cats] => /15/
                   [key_2] => FTU-4
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /15/
                   [key_2] => FTU-6
                 )
[10] => Array 
        (
           [0] => Array 
                 (
                   [key_1] => FOO
                   [cats] => /30/10/
                   [key_2] => FTU-3
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /10/
                   [key_2] => FTU-5
                 )

我正在寻找最接近我需要的This answer,但是没有用,这就是我寻求帮助的原因。

更新:我想我刚解决了......

foreach($firstarr as $k => $v) {
  $cats = array_filter(explode('/', $v['cats']));
  foreach($cats as $ks=>$vs) {
    if(stripos($v['cats'], $vs)){
      $pp[$vs][] = $v;
    }
  }
}
看起来不错。

3 个答案:

答案 0 :(得分:1)

也许你帮助这段代码:

function arrayByOneKey($array, $keyName)
{
    $result = [];
    foreach ($array as $item)
    {
        $keys = explode('/', (string)$item[$keyName]);
        foreach($keys as $key)
        {
            if($key == '')
            {
                continue;
            }
            $result[$key][] = $item;
        }
    }
    return $result;
}

$array = [
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-1',
        'cats' => '/15/'
    ],
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-2',
        'cats' => '/15/'
    ],
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-3',
        'cats' => '/30/10/'
    ],
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-4',
        'cats' => '/30/10/0'
    ]
];

$array = arrayByOneKey($array, 'cats');
var_dump($array);

<强>结果:

array(4) {
  [15]=>
  array(2) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-1"
      ["cats"]=>
      string(4) "/15/"
    }
    [1]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-2"
      ["cats"]=>
      string(4) "/15/"
    }
  }
  [30]=>
  array(2) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-3"
      ["cats"]=>
      string(7) "/30/10/"
    }
    [1]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-4"
      ["cats"]=>
      string(8) "/30/10/0"
    }
  }
  [10]=>
  array(2) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-3"
      ["cats"]=>
      string(7) "/30/10/"
    }
    [1]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-4"
      ["cats"]=>
      string(8) "/30/10/0"
    }
  }
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-4"
      ["cats"]=>
      string(8) "/30/10/0"
    }
  }
}

<强>更新

array_filter - 优雅的解决方案但是 0 的类别将被忽略。如果是这样,那么!empty($ key)在循环中优于 array_filter - 因为它也会通过数组元素

答案 1 :(得分:0)

您可以从extern int MagicNumber = 10001; extern double Lots = 0.01; extern double StopLoss = 1; extern double TakeProfit = 1; extern int TrailingStop = 0; extern int Slippage = 3; //+------------------------------------------------------------------+ // expert start function //+------------------------------------------------------------------+ int start() // New-MQL4.56789 #strict uses another constructor: int OnTick(){...} { double MyPoint = Point; if ( Digits == 3 || Digits == 5 ) MyPoint = Point*10; double TheStopLoss = 0; double TheTakeProfit = 0; if ( TotalOrdersCount() == 0 ) { int result = 0; if ( ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 1 ) < iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 1 ) ) && ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 0 ) > iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0 ) ) ) // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Here is your open BUY rule { result = OrderSend( Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "Buy", MagicNumber, 0, Blue ); if ( result > 0 ) { TheStopLoss = 0; TheTakeProfit = 0; if ( TakeProfit > 0 ) TheTakeProfit = Ask + TakeProfit * MyPoint; if ( StopLoss > 0 ) TheStopLoss = Ask - StopLoss * MyPoint; OrderSelect( result, SELECT_BY_TICKET ); OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( TheStopLoss, Digits ), NormalizeDouble( TheTakeProfit, Digits ), 0, Green ); } return(0); } if ( ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 1 ) > iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 1 ) ) && ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 0 ) < iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0 ) ) ) // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Here is your open SELL rule { result = OrderSend( Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "Sell", MagicNumber, 0, Red ); if ( result > 0 ) { TheStopLoss = 0; TheTakeProfit = 0; if ( TakeProfit > 0 ) TheTakeProfit = Bid - TakeProfit * MyPoint; if ( StopLoss > 0 ) TheStopLoss = Bid + StopLoss * MyPoint; OrderSelect( result, SELECT_BY_TICKET ); OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( TheStopLoss, Digits ), NormalizeDouble( TheTakeProfit, Digits ), 0, Green ); } return(0); } } for ( int cnt = 0; cnt < OrdersTotal(); cnt++ ) { OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ); if ( OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber ) { if ( OrderType() == OP_BUY ) { if ( TrailingStop > 0 ) { if ( Bid - OrderOpenPrice() > MyPoint * TrailingStop ) { if ( OrderStopLoss() < Bid - MyPoint * TrailingStop ) { OrderModify( OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MyPoint, OrderTakeProfit(), 0, Green ); return(0); } } } } else { if ( TrailingStop > 0 ) { if ( ( OrderOpenPrice() - Ask ) > ( MyPoint * TrailingStop ) ) { if ( ( OrderStopLoss() > ( Ask + MyPoint * TrailingStop ) ) || ( OrderStopLoss() == 0 ) ) { OrderModify( OrderTicket(), OrderOpenPrice(), Ask + MyPoint * TrailingStop, OrderTakeProfit(), 0, Red ); return(0); } } } } } } return(0); } int TotalOrdersCount() { int result = 0; for ( int i = 0; i < OrdersTotal(); i++ ) { OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); if ( OrderMagicNumber() == MagicNumber ) result++; } return( result ); } 获取密钥:

explode('/', $item['cats'])[1]

答案 2 :(得分:0)

这可能是一个扭曲的解决方法,但我愿意打赌它可以解决问题:

<?php 
incude('db');
mysqli_query("DELETE FROM about_us_tble WHERE ID=$GET(about_id)") 
or die(mysqli_error());
    echo"Data has been deleted";

header("location:about_admin.php")
?>

尝试一下,让我们知道它是怎么回事......