1

I do have following associative multidimensional array available after json_decode(string, true).

Array
(
    [statusCode] => 200
    [data] => Array
        (
            [objects] => Array
                (
                    [0] => deals
                    [1] => contacts
                    [2] => accounts
                )

            [deals] => Array
                (
                    [0] => dealName
                    [1] => ApprovedBy
                    [2] => ApprovedDate
                    [3] => CloseDate
                )

            [contacts] => Array
                (
                    [0] => contectName
                    [1] => email
                    [2] => firstName
                    [3] => lastName 
                )

            [accounts] => Array
                (
                    [0] => accountName
                    [1] => creationDate
                    [2] => ApprovedDate
                    [3] => accountNumber
                )
        )

)

It want to replace numeric keys with their corresponding values in arrays like:

[deals] => deals
[contacts] => contacts
[accounts] => accounts

What I tried so far?

$finalIOArray = array();
$integrationObjectsArray = $response['data']['objects']; 
foreach($integrationObjectsArray as $integrationObject){ 
   $finalIOArray[$integrationObject] = $integrationObject; 
}

This is for only the objects array in the main data array. But I want to replace keys with values in all the sub-arrays in the main data array.

5
  • 1
    what have you tried, this isn't code my stuff for me site. Commented Sep 9, 2017 at 8:47
  • What I did is: $finalIOArray = array(); $integrationObjectsArray = $response['data']['objects']; foreach($integrationObjectsArray as $integrationObject){ $finalIOArray[$integrationObject] = $integrationObject; } But if I follow this approach, I manually have to treat each array in data array. Commented Sep 9, 2017 at 8:58
  • Please edit your original question with this info. Commented Sep 9, 2017 at 9:13
  • I don't see why you need the objects entry at all; it is duplicate information for what is already represented by the other keys in the same array. Also I don't understand why deals, contacts and accounts are plural words when you only store one item in their values. Commented Sep 9, 2017 at 9:45
  • objects are being populated in a drop-down as its possible options. And upon selection of any object from the 1st drop-down, a second drop-down is populated with fields associated with that object. Reference: stackoverflow.com/questions/46128410/… Commented Sep 9, 2017 at 9:49

4 Answers 4

3

here's a recursive solution to the problem

function updateArray(array $data)
{
    $result = array();
    foreach ($data as $key => $value) {
        if (is_integer($key) && is_string($value)) { // key is integer and value is string
            $result[$value] = $value; // update the key
        } elseif (is_array($value)) { // value is array
            $result[$key] = updateArray($value); // recurse
        } else {
            $result[$key] = $value; // leave key/value alone
        }
    }
    return $result;
}

print_r(updateArray($arr));
Sign up to request clarification or add additional context in comments.

1 Comment

Let me try this, seems elegant.
2

You can do like this

foreach($arr["data"] as $key=>&$data){
  $new = array();
  foreach($data as $key2=>$value){
    $new[$value] = $value;
  }
  $data = $new;
}
print_r($arr);

Live demo : https://eval.in/858599

5 Comments

It is not the required output.
Yes, something like this. This enlightened me with the idea to manipulate the main data array. Let me try that.
Exactly, that's what I wanted to process all in one go. Please update the original answer with this update so that I can accept it.
have updated my ans. Just a curiosity where you need to update array like this, I means in which code ?
Please see stackoverflow.com/questions/46128410/… This question was the sub-question (point 2) in that, In which I wanted a correct way and direction. That was my use case if that's what you are asking.
1

I modify my code to suits your needs, please, try it.

$data = [];
foreach($arr["data"] as $key => $example) {
    $new = [];
    foreach($example as $value) {
        $new[$value] = $value;
    }
    $data[$key] = $new;
}
$arr["data"] = $data;
print_r($arr);

1 Comment

I add $key's to keep your original keys like objects, deals, contacts, accounts.
0

This is a cleaner and more direct method than the others on offer. Use array_combine to bulk reassign keys using values from the subarray.

In my method using foreach, the & before $data means "passing by reference" . http://php.net/manual/en/language.references.pass.php This effectively allows the modification of the original array data without writing the keys that lead to the specific deep element.

Code:

$arr =  Array
(
    "statusCode" => 200,
    "data" => Array
        (
            "objects" => Array
                (
                    "0" => deals,
                    "1" => contacts,
                    "2" => accounts
                ),

            "deals" => Array
                (
                    "0" => dealName,
                    "1" => ApprovedBy,
                    "2" => ApprovedDate,
                    "3" => CloseDate
                ),
            "contacts" => Array
                (
                    "0" => contectName,
                    "1" => email,
                    "2" => firstName,
                    "3" => lastName 
                ),
            "accounts" => Array
                (
                    "0" => accountName,
                    "1" => creationDate,
                    "2" => ApprovedDate,
                    "3" => accountNumber
                )
        )
);
foreach($arr["data"] as &$data){
     $data=array_combine($data,$data);
}
var_export($arr);

Output:

array (
   'statusCode' => 200,
  'data' => 
  array (
    'objects' => 
    array (
      'deals' => 'deals',
      'contacts' => 'contacts',
      'accounts' => 'accounts',
    ),
    'deals' => 
    array (
      'dealName' => 'dealName',
      'ApprovedBy' => 'ApprovedBy',
      'ApprovedDate' => 'ApprovedDate',
      'CloseDate' => 'CloseDate',
    ),
    'contacts' => 
    array (
      'contectName' => 'contectName',
      'email' => 'email',
      'firstName' => 'firstName',
      'lastName' => 'lastName',
    ),
    'accounts' => 
    array (
      'accountName' => 'accountName',
      'creationDate' => 'creationDate',
      'ApprovedDate' => 'ApprovedDate',
      'accountNumber' => 'accountNumber',
    ),
  ),
)

Or if you prefer functional iteration, this provides the same output.

$arr["data"]=array_map(function ($a){return array_combine($a,$a);},$arr["data"]);
var_export($arr);

1 Comment

Nice & concise. Can you explain a little about ampersand sign before enumeration variable $data? Thanks

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.