我怎样才能在PHP中的Array中实现这一点

时间:2018-06-14 22:32:34

标签: php arrays loops

我在使用数组实现此结果时遇到问题,我想用第一个数组中的数据更新另一个空数组,以便省略前两行,并将前三行添加到空数组的索引0,接下来的3行也会更新到第二个索引,依此类推。我在

中有这个数组
`$arr = [
    'tqty' => 9,
    'tPrice' => 18700,
    'food_name_1' => 'Black Coffee',
    'food_quanty_1' => 1,
    'food_price_1' => 1000,
    'food_name_2' => 'Sub Combo',
    'food_quanty_2' => 2,
    'food_price_2' => 3000
];`

我想使用这个数组数据并以这种方式更新另一个空数组,删除前两行

$arr2 = [
    0 => [
         'food_name_1' => 'Black Coffee',
         'food_quanty_1' => 1,
        'food_price_1' => 1000
    ],
   1 => [
         'food_name_2' => 'Sub Combo',
         'food_quanty_2' => 2,
        'food_price_2' => 3000

    ]

];

这是我到目前为止的代码

$arr = [
    'tqty' => 9,
    'tPrice' => 18700,
    'food_name_1' => 'Black Coffee',
    'food_quanty_1' => 1,
    'food_price_1' => 1000,
    'food_name_2' => 'Sub Combo',
    'food_quanty_2' => 2,
    'food_price_2' => 3000
];

$newdat = [];
$count = 0;
$oldcount = 1;
foreach($arr as $key => $value){
    if(preg_match_all('!\d+!', $key, $matches)){

        if($oldcount == $matches[0][0]){
            $newdat[$matches[0][0]] = [
                $count => [
                    $key => $value
                ]
                ];
        } else{
            $count = 0;
            $oldcount = $matches[0][0];
        }
    }
    $count++;
 }

我希望我能尽快得到帮助。感谢

5 个答案:

答案 0 :(得分:3)

假设数组键和订单保持一致,您可以使用array_chunk

<?php 

$inArray = [
    'tqty' => 9,
    'tPrice' => 18700,
    'food_name_1' => 'Black Coffee',
    'food_quanty_1' => 1,
    'food_price_1' => 1000,
    'food_name_2' => 'Sub Combo',
    'food_quanty_2' => 2,
    'food_price_2' => 3000,
];

$outArray = [];

// Remove first 2 values.
$inArray = array_slice( $inArray, 2 );

// 'Chunk' the rest of the values.
// true preserves keys.
$outArray = array_chunk( $inArray, 3, true );

echo '<pre>' . print_r( $outArray, true ) . '</pre>';

/**
    Output:

    <pre>Array
    (
        [0] => Array
            (
                [food_name_1] => Black Coffee
                [food_quanty_1] => 1
                [food_price_1] => 1000
            )

        [1] => Array
            (
                [food_name_2] => Sub Combo
                [food_quanty_2] => 2
                [food_price_2] => 3000
            )

    )
    </pre>
 */

答案 1 :(得分:0)

这是一个解决方案,可以在最后找到_x以检查其数字

此解决方案不会担心您拥有多少个非编号字段,或者每个编号为“行”的字段数量,它们也会根据_x数字编制索引。

$arr = [
  'tqty' => 9,
  'tPrice' => 18700,
  'food_name_1' => 'Black Coffee',
  'food_quanty_1' => 1,
  'food_price_1' => 1000,
  'food_name_2' => 'Sub Combo',
  'food_quanty_2' => 2,
  'food_price_2' => 3000
];

$arr2 = array();

foreach( $arr as $key => $value )
{
  $explode = explode( '_', $key );
  if( ctype_digit( $index = end( $explode ) ) === true)
  {
    if( isset( $arr2[ $index ] ) === false )
    {
      $arr2[ $index ] = array();
    }
    $arr2[ $index ][ substr( $key, 0, strlen( $key ) - 1 - strlen( $index ) ) ] = $value;
  }
}

print_r( $arr2 );

输出:

Array
(
    [1] => Array
        (
            [food_name] => Black Coffee
            [food_quanty] => 1
            [food_price] => 1000
        )

    [2] => Array
        (
            [food_name] => Sub Combo
            [food_quanty] => 2
            [food_price] => 3000
        )

)

答案 2 :(得分:0)

循环遍历它并通过变量构建数组索引字符串。

<?php
$arr = [
    'tqty' => 9,
    'tPrice' => 18700,
    'food_name_1' => 'Black Coffee',
    'food_quanty_1' => 1,
    'food_price_1' => 1000,
    'food_name_2' => 'Sub Combo',
    'food_quanty_2' => 2,
    'food_price_2' => 3000
];

foreach(array("food_name_","food_quanty_","food_price_") as $v){


// replace the set value of 2 here with a count() on the $arr
// and some basic math - IF you are always sure you'll have 3 fields
    for($i=0;$i<2;$i++){
        $newarr[$i][$v.($i+1)]=$arr[$v.($i+1)];
    }

}

print_r($newarr);


 ?>

答案 3 :(得分:0)

如果我理解正确的话,

$newdata = array();
for ($i = 1; $i++;) { // Intentionally no condition set.
    if (array_key_exists('food_name_' . $i, $arr)) {
        $temparray = array();
        $temparray['food_name_' . $i] = $arr['food_name_' . $i];

        if (array_key_exists('food_quanty_' . $i, $arr)) {
            $temparray['food_quanty_' . $i] = $arr['food_quanty_' . $i];
        }
        if (array_key_exists('food_price_' . $i, $arr)) {
            $temparray['food_price_' . $i] = $arr['food_price_' . $i];
        }
        $newdata[] = $temparray;
    } else {
        break; // break out of the loop
    }
}

echo "<pre>";
print_r($newdata);
echo "</pre>";
die();

答案 4 :(得分:0)

如果您已经知道需要始终使用$ arr中的相同索引,则可以使用array_keys函数来索引关联数组。
例如:

$keys = array_keys($arr);
echo '<br><br>'.$arr[$keys[1]];


这是一个完整的例子:

$keys = array_keys($arr);    #stores the associative keys by index
$arr2 = array();

/* For each array $arr do the following */
$limit = 5; #substitute this with: count($arraylist)
for( $n_array=0; $n_array<limit; $n_array++ ){
    $cur_arr = array(); #substitute with your: $arraylist[ $n_array ]
    for( $a = 2; $a<count($arr); $a++ ){
        $cur_arr[ $keys[$a] ] = $arr[ $keys[$a] ];
    }  
    $arr2[ $n_array ] = $cur_arr;
}

希望它会有所帮助