使用数组的最后一个值作为其他项的键 – php

huangapple go评论68阅读模式
英文:

Pop last value of array and use it as key for others items - php

问题

$inputArray = array(
    array(
        array('value31', 'value32'),
        array('value33', 'value34'),
        'value21'
    ),
    'value11'
);

function convertArray(array $inputArray, $old_key = null) {
    $outputArray = [];
    $isMultiDimensional = count($inputArray) != count($inputArray, COUNT_RECURSIVE);
    $array_key = $isMultiDimensional ? array_pop($inputArray) : $old_key;

    foreach ($inputArray as $value) {
        $itemIsMultiDimensional = count($value) != count($value, COUNT_RECURSIVE);

        if (is_array($value) && $itemIsMultiDimensional) {
            $outputArray = array_merge($outputArray, convertArray($value, $array_key));
        } else {
            $outputArray[$array_key][] = $value;
        }
    }

    return $outputArray;
}

$outputArray = convertArray($inputArray);

你可以使用上面的代码来实现你所描述的功能。

英文:

I have a multidimensional array like

$inputArray = array(
    array(
        array('value31', 'value32'),
        array('value33', 'value34'),
        'value21'
    ),
    'value11'
);

I need to pop the last item from each item and considered it as the key for rest values. Output array should like,

$outputArray = array(
    'value11' => array(
        'value21' => array(
            array('value31', 'value32'),
            array('value33', 'value34'),
        )
    )
);

I tried this code, but it doesn't work.

function convertArray(array $inputArray, $old_key = null) {
    $outputArray = [];
    $isMultiDimensional = count($inputArray) != count($inputArray, COUNT_RECURSIVE);
    $array_key = $isMultiDimensional ? array_pop($inputArray) : $old_key;

    foreach ($inputArray as $value) {
        $itemIsMultiDimensional = count($value) != count($value, COUNT_RECURSIVE);

        if (is_array($value) && $itemIsMultiDimensional) {
            $outputArray = array_merge($outputArray, convertArray($value, $array_key));
        } else {
            $outputArray[$array_key][] = $value;
        }
    }

    return $outputArray;
}

答案1

得分: 1

function convertArray(array $inputArray) {
    $lastItem = array_pop($inputArray);
    $outputArray = [];
    if (!empty($inputArray) && is_array($lastItem)) {
        $outputArray = convertArray($inputArray);
        $outputArray[$lastItem[0]] = $lastItem[1];
    } else {
        $outputArray[$lastItem] = $inputArray;
    }

    return $outputArray;
}

$inputArray = array(
    array(
        array('value31', 'value32'),
        array('value33', 'value34'),
        'value21'
    ),
    'value11'
);

$outputArray = convertArray($inputArray);
print_r($outputArray);

This code defines a PHP function convertArray that takes an input array and recursively converts it into a nested associative array. The input array structure appears to consist of nested arrays with values, and the function organizes them into a nested associative array with appropriate keys.

英文:
function convertArray(array $inputArray) {
    $lastItem = array_pop($inputArray);
    $outputArray = [];
    if (!empty($inputArray) && is_array($lastItem)) {
        $outputArray = convertArray($inputArray);
        $outputArray[$lastItem[0]] = $lastItem[1];
    } else {
        $outputArray[$lastItem] = $inputArray;
    }

    return $outputArray;
}

$inputArray = array(
    array(
        array('value31', 'value32'),
        array('value33', 'value34'),
        'value21'
    ),
    'value11'
);

$outputArray = convertArray($inputArray);
print_r($outputArray);

答案2

得分: 1

看着你的结构,你实际上不需要使用_COUNT_RECURSIVE_来做这个。如果数组的最后一个元素不是字符串,只需返回数组本身。
如果确实是字符串,那么只需将整个结果包装在一个新的键下。


function convertArray($inputArray) {
    if (!checkIfAllArraysExceptLast($inputArray)) return $inputArray;
    
    $value = array_pop($inputArray);
    $result = [];
    
    foreach ($inputArray as $k => $v){
        $result[$k] = is_array($v) ? convertArray($v) : $v;
    }
    
    return [ $value => $result];
}

function checkIfAllArraysExceptLast($array){
    $v = array_pop($array);
    return !is_array($v) && count(array_filter($array, fn($v) => is_array($v))) == count($array);
}

[**Live Demo**](https://3v4l.org/Xbi91)

英文:

Looking at your structure, you don't really need COUNT_RECURSIVE to do this. If the last element of the array isn't a string, just return the array as is.
If it is indeed a string, then just wrap the entire result under a new key.

<?php

function convertArray($inputArray) {
    if(!checkIfAllArraysExceptLast($inputArray)) return $inputArray;
    
    $value = array_pop($inputArray);
    $result = [];
    
    foreach($inputArray as $k => $v){
        $result[$k] = is_array($v) ? convertArray($v) : $v;
    }
    
    return [ $value => $result];
}

function checkIfAllArraysExceptLast($array){
    $v = array_pop($array);
    return !is_array($v) && count(array_filter($array, fn($v) => is_array($v))) == count($array);
}

Live Demo

huangapple
  • 本文由 发表于 2023年7月20日 14:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定