0

I am having a terrible time getting this to work I have been struggling with it for a couple hours now. Can someone please help me? I have included a fiddle.

I believe my problem is in this string:

        $$salesAndOwner[$i]["$salesAndOwner[$i]".$l] = $salesAndOwner[$i.$l][$param] = $values[$l];

Basically I have the following multidimensional array:

[sales] => Array
        (
            [FirstName] => Array
                (
                    [0] => salesFirst1
                    [1] => salesFirst2
                )

            [LastName] => Array
                (
                    [0] => salesLast1
                    [1] => salesLast2
                )

        )

    [decisionmaker] => Array
        (
            [FirstName] => Array
                (
                    [0] => dmFirst1
                    [1] => dmFirst2
                )

            [LastName] => Array
                (
                    [0] => dmLast1
                    [1] => dmLast2
                )

        )

)

I need this to be reorganized like I did with the following array:

Array
(
    [additionallocations0] => Array
        (
            [Address] => Address1
            [State] => State1
        )

    [additionallocations1] => Array
        (
            [Address] => Address2
            [State] => State2
        )

)

Here is the original:

Array
(
    [additionallocations] => Array
        (
            [Address] => Array
                (
                    [0] => Address1
                    [1] => Address2
                )

            [State] => Array
                (
                    [0] => State1
                    [1] => State2
                )

        )

This is how I reorganize the above array:

if(isset($_POST['additionallocations'])) {
        $qty = count($_POST['additionallocations']["Address"]);

        for ($l=0; $l<$qty; $l++)
        {
            foreach($_POST['additionallocations'] as $param => $values)
            {
                $additional['additionallocations'.$l][$param] = $values[$l];
            }
        }

And this is what I am using for the sales and decisionmaker array. If you notice I have an array that contains sales and decisionmaker in it. I would like to be able to sort any future arrays by just adding its primary arrays name. I feel I am close to solving my problem but I can not get it to produce right.

$salesAndOwner = array(0 => "sales", 1 => "decisionmaker");

for($i = 0; $i < 2; $i++){
$qty = count($_POST[$salesAndOwner[$i]]["FirstName"]);

        for ($l=0; $l<$qty; $l++)
        {
            foreach($_POST[$salesAndOwner[$i]] as $param => $values)
            {
                            $$salesAndOwner[$i]["$salesAndOwner[$i]".$l] = $salesAndOwner[$i.$l][$param] = $values[$l];
            }
        }
}

In the above code I hard coded 'sales' into the variable I need it to make a variable name dynamically that contains the sales0 decisionmaker0 and sales1 decisionmaker1 arrays so $sales and $decisionmaker I hope this makes sense please let me know if you need any more info

1
  • "$salesAndOwner[$i]" probably is getting expanded not in the way you think. Commented Feb 11, 2014 at 16:36

3 Answers 3

1

Let's break it down. Using friendly variable names and spacing will make your code a lot easier to read.

Remember. The syntax is for you to read and understand easily. (Not even just you, but maybe future developers after you!)

So you have an array of groups. Each group contains an array of attributes. Each attribute row contains a number of attribute values.

PHP's foreach is a fantastic way to iterate through this, because you will need to iterate through (and use) the index names of the arrays:

<?php

$new_array = array();

// For each group:

foreach($original_array as $group_name => $group) {

    // $group_name = e.g 'sales'

    // For each attribute in this group:

    foreach($group as $attribute_name => $attributes) {

        // $attribute_name = e.g. 'FirstName'

        // For each attribute value in this attribute set.

        foreach($attributes as $row_number => $attribute) {

            // E.g. sales0
            $row_key = $group_name . $row_number;

            // if this is the first iteration, we need to declare the array.
            if(!isset($new_array[$row_key])) {
                $new_array[$row_key] = array();
            }

            // e.g. Array[sales0][FirstName]
            $new_array[$row_key][$attribute_name] = $attribute;

        }

    }

}

?>

With this said, this sort of conversion may cause unexpected results without sufficient validation.

Make sure the input array is valid (e.g. each attribute group has the same number of rows per group) and you should be okay.

Sign up to request clarification or add additional context in comments.

Comments

1
$salesAndOwner = array("sales", "decisionmaker");

$result = array();
foreach ($salesAndOwner as $key) {
    $group = $_POST[$key];
    $subkeys = array_keys($group);
    $first_key = $subkeys[0];
    foreach ($group[$first_key] as $i => $val) {
        $prefix = $key . $i;
        foreach ($subkeys as $subkey) {
            if (!isset($result[$prefix])) {
                $result[$prefix] = array();
            }
            $result[$prefix][$subkey] = $val;
        }
    }
}

DEMO

2 Comments

Taking directly from $_POST is risky. I would advise you have a whitelist array which takes only specific keys from $_POST instead.
Good point, I changed to use a hard-coded array, like in the original question.
1

Try

$result =array();

foreach($arr as $key=>$val){

    foreach($val as $key1=>$val1){
        foreach($val1 as $key2=>$val2){
           $result[$key.$key2][$key1] = $val2;
         }
    }
}

See demo here

1 Comment

This will throw warnings/notices when you create $result values several levels deep without first declaring arrays.

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.