0

I am trying to convert a multidimensional array to a simple array. Some parts of my code is working, but I am lost when I have to preserve a child array. Here is the main array:

Array
(
    [0] => Array
        (
            [name] => scattr
            [value] => 250
        )

    [1] => Array
        (
            [name] => scattrtel
            [value] => 9830293789
        )

    [2] => Array
        (
            [name] => scattrcolor
            [value] => #2764C6
        )

    [3] => Array
        (
            [name] => scattrta
            [value] => 
        )

    [4] => Array
        (
            [name] => scattrcb
            [value] => 
        )

    [5] => Array
        (
            [name] => scattrmcb[1]
            [value] => 10
        )

    [6] => Array
        (
            [name] => scattrmcb[4]
            [value] => 40
        )

    [7] => Array
        (
            [name] => scattrmrdo
            [value] => 20
        )

    [8] => Array
        (
            [name] => scattrselect
            [value] => 30
        )

    [9] => Array
        (
            [name] => pwpus-shortcode-nonce
            [value] => 028c9426c5
        )

    [10] => Array
        (
            [name] => _wp_http_referer
            [value] => /wp-admin/admin-ajax.php
        )

)  

And, my PHP to convert it in simple array:

$new = array();

foreach ( $datas as $data ) { // $datas as being the multidimensional array.
    $new[$data['name']] = $data['value'];
}

Now, a print_r of $new gives me:

Array
(
    [scattr] => 250
    [scattrtel] => 9830293789
    [scattrcolor] => #2764C6
    [scattrta] => gdkwsdghwkdhgk
    [scattrcb] =>
    [scattrmcb[1]] => 10
    [scattrmcb[4]] => 40
    [scattrmrdo] => 20
    [scattrselect] => 30
    [pwpus-shortcode-nonce] => 028c9426c5
    [_wp_http_referer] => /wp-admin/admin-ajax.php
)  

So, I think my code is somewhat working, but not preserving that scattrmcb[1] and scattrmcb[4] in an array. Should give something like this:

[scattrmcb] => Array
            (
                [1] => 10
                [4] => 40
            )

How do I do that?

Thanks

2
  • Misunderstood at first, edited... Commented Oct 13, 2016 at 20:20
  • Totally an aside, but data is plural, the singular is datum. Commented Oct 13, 2016 at 23:52

2 Answers 2

1

So array_column() will get you most of the way. I used a regex to get the key and the inner array index and then eval() != evil:

foreach(array_column($datas, 'value', 'name') as $key => $value) {
    if(preg_match('/(.*)(\[\d+\])$/', $key, $match)) {
        eval("\$new['{$match[1]}']{$match[2]} = $value;");
    } else {
        $new[$key] = $value;
    }
}

Maybe a more or less readable eval():

        eval('$new["'.$match[1].'"]'.$match[2].' = $value;');
Sign up to request clarification or add additional context in comments.

2 Comments

OP wants to convert string scattrmcb[4] to array like [scattrmcb][4]. Does your code help with this?
In a fatter of fact, your first answer served me better. Just omitted the need of the key=>value pair from the child array and I was all good. Thanks mate, that really helped me a lot.
0

You need more logic:

<?php
$datas = [
    ['name'=>'scattr', 'value'=>250],
    ['name'=>'scattrmcb[1]', 'value'=>10],
    ['name'=>'scattrmcb[4]', 'value'=>40]
];

$new = array();

foreach ( $datas as $data ) { // $datas as being the multidimensional array.
    // 'scattrmcb' is special so handle it here
    if(substr($data['name'], 0, 9) === 'scattrmcb'){
        // Set the 'scattrmcb' index
        if(!isset($new[substr($data['name'], 0, 9)])){
            $new[substr($data['name'], 0, 9)] = [];
        }

        // Use preg_replace to set a sub-array to the value between []
        $new[substr($data['name'], 0, 9)][preg_replace("/[^0-9]/", "", $data['name'])] = $data['value'];
    }
    else{
        $new[$data['name']] = $data['value'];
    }
}

print_r($new);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.