dqqg0811 2019-02-01 17:52
浏览 48
已采纳

操纵php数组

I need to create path structure, with the values that I am getting from the array:

Array
(
    [machineAttribute] => Array
    (
        [0] => TTT 1000 S
        [1] => TTT 1100 S
    )

    [technicalAttribute] => Array
    (
        [0] => Certificate
        [1] => Software
    )

    [languageAttribute] => Array
    (
        [0] => English
        [1] => Spanish
    )

)

So, I need to create path that looks like this:

Array
(
    [0] => TTT 1000 S/Certificate/English
    [1] => TTT 1000 S/Certificate/Spanish
    [2] => TTT 1000 S/Software/English
    [3] => TTT 1000 S/Software/Spanish
    [4] => TTT 1100 S/Certificate/English
    [5] => TTT 1100 S/Certificate/Spanish
    [6] => TTT 1100 S/Software/English
    [7] => TTT 1100 S/Software/Spanish
)  

This is a perfect scenario and I was able to solve this with nested foreach:

if (is_array($machineAttributePath))
{
    foreach ($machineAttributePath as $machinePath)
    {
        if (is_array($technicalAttributePath))
        {
            foreach ($technicalAttributePath as $technicalPath)
            {

                if (is_array($languageAttributePath))
                {
                    foreach ($languageAttributePath as $languagePath)
                    {
                        $multipleMachineValuesPath[] = $machinePath . '/' . $technicalPath . '/' . $languagePath);
                    }
                }

            }
        }
    }

    return $multipleMachineValuesPath;
}

But, the problem begins, if the array returns mixed values, sometimes, single value, sometimes array. For example:

Array
(
    [machineAttribute] => Array
    (
        [0] => TTT 1000 S
        [1] => TTT 1100 S
        [2] => TTT 1200 S
    )

    [technicalAttribute] => Certificate

    [languageAttribute] => Array
    (
        [0] => English
        [1] => Spanish
    )

)

Then the array should look like:

Array
(
    [0] => TTT 1000 S/Certificate/English
    [1] =>TTT 1000 S/Certificate/Spanish
    [2] => TTT 1100 S/Certificate/English
    [3] => TTT 1100 S/Certificate/Spanish
)

I wrote code, but it is really messy and long and not working properly. I am sure that this could be somehow simplified but I have enough knowledge to solve this. If someone knows how to deal with this situation, please help. Thank you.

  • 写回答

5条回答 默认 最新

  • dongne1560 2019-02-01 18:14
    关注

    If you need something simple, you can convert your scalar values to array by writing simple separate function (let name it "force_array"). The function that wraps argument to array if it is not array already.

    function force_array($i) { return is_array($i) ? $i : array($i); }
    
    //...
    
    foreach (force_array($machineAttributePath) as $machinePath)
    {
        foreach (force_array($technicalAttributePath) as $technicalPath)
        {
            foreach (force_array($languageAttributePath) as $languagePath)
            {
                $multipleMachineValuesPath[] = $machinePath . '/' . $technicalPath . '/' . $languagePath;
            }
        }
    }
    
    return($multipleMachineValuesPath);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度