【问题标题】:How to prepare a new array out of two arrays inside foreach loop?如何从 foreach 循环中的两个数组中准备一个新数组?
【发布时间】:2019-04-08 12:24:01
【问题描述】:

我正在尝试将一个数组的键与另一个数组的值进行比较。如果当前键的值更大,我想将该值推入一个新数组。然后我想将该特定键的所有收集值插入到数据库表中。

这些是我的输入数组:

$productlist = [result] => Array
    (
        [0] => Array
            (
                [configoption1] => 2M
                [id] => 96 

            )
        [1] => Array
            (
                [configoption1] => 5M
                [id] => 97
             )
        [2] => Array
            (
                [configoption1] => 15M
                [id] => 98
            )
        [3] => Array
            (
                [configoption1] => 30M
                [id] => 99
             )
    )

$myplans = Array
        (
            [2M] => Array
                (
                    [price] => 10
                )
            [5M] => Array
                (
                    [price] => 10
                )
            [15M] => Array
                (
                    [price] => 10
                )
            [30M] => Array
                (
                    [price] => 10
                )
        )

以下是我的示例代码:

$upgradelist = array()

foreach ($myplans as $plan => $data) {

    foreach($productlist['result'] as $key=>$value){
        if($plan == 'ENTERPRISE'){
            //no higher plans than Enterprise
        }else{

            $plan1 = (int)substr_replace($plan, "", -1);

            $value['configoption1'] = (int)substr_replace($value['configoption1'], "", -1);
            #echo " configconfig=> ".$value['configoption1'];

            if($plan > $value['configoption1']){
                $upgrade_product_ids[$plan][] = $value['id'];
            }
        }
    }


    //insert upgrade products list
    if(!empty($upgradelist)){
        foreach($upgradelist as $key => $upgrade_product_id){
            #$insert_stmt_upgradeproduct = <insert statement> for each $plan
        }
    }
}

每次我从 foreach 循环出来时的预期输出:> foreach($productlist['result']

$upgradelist = Array
(
    [2] => Array
        (
            [0] => 97 //5M
            [1] => 98 //15M
            [2] => 99 //30M
        )
)

$upgradelist = Array
(
    [5] => Array
        (
            [0] => 98
            [1] => 99
        )

)

$upgradelist = Array
(

    [15] => Array
        (
            [0] => 99
        )
)

$upgradelist = Array
(

    [30] => Array
        (

        )
)

【问题讨论】:

  • 能否请您创建一个有效的数组
  • 格式不正确。调整了它。我希望现在可以理解了。这些是我的有效示例数组。

标签: php arrays foreach


【解决方案1】:

你可以像下面这样解决:

foreach($myplans as $key => $plan) {
   $data_to_be_inserted = [];
   foreach($productlist['result'] as $product) {
       if ($key > $product['configoption1']) {
           $data_to_be_inserted [] = $product['id'];
       }
   }
   if (count($data_to_be_inserted)) {
      //insert into database(expected value will be available here)
   }
}

【讨论】:

  • 感谢您的回答。将 $array2 重命名为 $myplans。 $arr['configoption1'] 无效。 $myplans 中不存在此值。
  • 例如,对于键 30M,预期输出为空,因为 30M 是最大值。使用您的代码,30M 包含所有剩余的 ID (97,98,99)。 [30M] => 数组([0] => 97 [1] => 98 [2] => 99)
【解决方案2】:

你可以这样做

<?php
 $myArray = [];
$myArray['result'] = array(
array("configoption1" => "2m", "id" => 96),
array("configoption1" => "5m", "id" => 97),
array("configoption1" => "15m", "id" => 98),
array("configoption1" => "30m", "id" => 99)
);
 //print_r($myArray);

$myArray2 =[
"2m" => ["price" => 10],
 "5m" => ["price" => 10],
 "15m" => ["price" => 10],
 "30m" => ["price" => 10]
];

$resultArray = [];
foreach($myArray2 as $key => $values) {

$keyNum = (int)$key;
$newTempArray[$keyNum] = [];
foreach($myArray['result'] as $newresult) {
    $configoption1Num = (int)$newresult['configoption1'];
    if($configoption1Num > $keyNum) {
        array_push($newTempArray[$keyNum], $newresult['id']);
    }
}

print_r($newTempArray[$keyNum]);
echo "<br>";
}

http://sandbox.onlinephpfunctions.com/code/008c839fb4fad4ed2aceae7c4ed1220579e8e318

【讨论】:

  • 这很酷。非常感谢您的回答。你能看到我的示例代码吗?我尝试了与您类似的方法,但没有成功。我的代码有什么错误?有什么想法吗?
猜你喜欢
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多