-2

I have below array, I need to append a new array inside $newData['_embedded']['settings']['web/vacation/filters']['data'], How can I access and append inside it ?

$newData  = [ 
  "id" => "47964173",
  "email" => "abced@gmail.com",
  "firstName" => "Muhammad",
  "lastName" => "Taqi",
  "type" => "employee",
  "_embedded" => [
      "settings" => [
    [
        "alias" => "web/essentials",
        "data" => [],
        "dateUpdated" => "2017-08-16T08:54:11Z"
    ],
    [
        "alias" => "web/personalization",
        "data" => [],
        "dateUpdated" => "2016-07-14T10:31:46Z"
    ],
    [
        "alias" => "wizard/login",
        "data" => [],
        "dateUpdated" => "2016-09-26T07:56:43Z"
    ],
    [
        "alias" => "web/vacation/filters",
        "data" => [
          "test" => [
            "type" => "teams",
            "value" => [
              0 => "09b285ec-7687-fc95-2630-82d321764ea7",
              1 => "0bf117b4-668b-a9da-72d4-66407be64a56",
              2 => "16f30bfb-060b-360f-168e-1ddff04ef5cd"
            ],
          ],
          "multiple teams" => [
            "type" => "teams",
            "value" => [
              0 => "359c0f53-c9c3-3f88-87e3-aa9ec2748313"
            ]
          ]
        ],
        "dateUpdated" => "2017-07-03T09:10:36Z"
      ],
    [
        "alias" => "web/vacation/state",
        "data" => [],
        "dateUpdated" => "2016-12-08T06:58:57Z"
    ]
    ]
  ]
];

$newData['_embedded']['settings']['web/vacation/filters']['data'] = $newArray;

Any Hint to quickly append it, I don't want to loop-in and check for keys inside loops.

Muhammad Taqi
  • 5,356
  • 7
  • 36
  • 61
  • Possible duplicate of [Difference between array\_push() and $array\[\] =](https://stackoverflow.com/questions/14232766/difference-between-array-push-and-array) – ficuscr Aug 22 '17 at 19:19
  • Possible duplicate of [PHP multidimensional array search by value](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – mickmackusa Aug 22 '17 at 21:00

2 Answers2

1

You need to find the key that corresponds to web/vacation/filters. For Example you could use this.

foreach ($newData['_embedded']['settings'] as $key => $value) {
 if ($value["alias"]==='web/vacation/filters') {
   $indexOfWVF = $key;
 }
}
$newData['_embedded']['settings'][$indexOfWVF]['data'][] = $newArray;

From the comments. Then you want to merge the arrays. Not append them.

$newData['_embedded']['settings'][$indexOfWVF]['data'] = array_merge($newData['_embedded']['settings'][$indexOfWVF]['data'],$newArray);

Or (if it's always Filter1):

  $newData['_embedded']['settings'][$indexOfWVF]['data']['Filter1'] = $newArray['Filter1'];
ficuscr
  • 6,975
  • 2
  • 32
  • 52
jh1711
  • 2,288
  • 1
  • 12
  • 20
  • It becomes this now ``` "alias" => "web/vacation/filters" "data" => [ "test" => [] "multiple teams" => [] 0 => [ "Filter1" => [] ] ]``` – Muhammad Taqi Aug 22 '17 at 19:42
  • `Filter1` is the new array. It should be as ` "alias" => "web/vacation/filters" "data" => [ "test" => [] "multiple teams" => [] "Filter1" => [] ]` – Muhammad Taqi Aug 22 '17 at 19:43
  • Good example. Alternately, make the array structure work for you. – ficuscr Aug 22 '17 at 19:50
  • @mtaqi. I edited the answer with 2 suggestions that should work. You need to keep the for loop for both of them – jh1711 Aug 22 '17 at 19:52
1

The settings subarray is "indexed". You first need to search the alias column of the subarray for web/vacation/filters to find the correct index. Using a foreach loop without a break will mean your code will continue to iterate even after the index is found (bad coding practice).

There is a cleaner way that avoids a loop & condition & break, use array_search(array_column()). It will seek your associative element, return the index, and immediately stop seeking.

You can use the + operator to add the new data to the subarray. This avoids calling a function like array_merge().

Code: (Demo)

if(($index=array_search('web/vacation/filters',array_column($newData['_embedded']['settings'],'alias')))!==false){
    $newData['_embedded']['settings'][$index]['data']+=$newArray;
}
var_export($newData);

Perhaps a more considered process would be to force the insert of the new data when the search returns no match, rather than just flagging the process as unsuccessful. You may have to tweak the date generation for your specific timezone or whatever... (Demo Link)

$newArray=["test2"=>[
            "type" =>"teams2",
            "value" => [
              0 => "09b285ec-7687-fc95-2630-82d321764ea7",
              1 => "0bf117b4-668b-a9da-72d4-66407be64a56",
              2 => "16f30bfb-060b-360f-168e-1ddff04ef5cd"
            ],
          ]
          ];
if(($index=array_search('web/vacation/filters',array_column($newData['_embedded']['settings'],'alias')))!==false){
    //echo $index;
    $newData['_embedded']['settings'][$index]['data']+=$newArray;
}else{
    //echo "couldn't find index, inserting new subarray";
    $dt = new DateTime();
    $dt->setTimeZone(new DateTimeZone('UTC'));  // or whatever you are using
    $stamp=$dt->format('Y-m-d\TH-i-s\Z');

    $newData['_embedded']['settings'][]=[
                                            "alias" => "web/vacation/filters",
                                            "data" => $newArray,
                                            "dateUpdated" => $stamp
                                        ];
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @mtaqi This is the way that you are "supposed" to do it. The foreach loop in jh1711's answer doesn't include an exit, so it continues to iterate even after it has sucessfully added the new data. My way also avoids the call of array merge. – mickmackusa Aug 22 '17 at 20:06
  • I guess we should add that my loop returns the index of the last occurence of the alias, while your array_search returns the first. Maybe we could also mention that array_merge replaces values with the same index, while the union operator doesn't. But everybody always wants first and old, and not last and new, or God forbid a combination. – jh1711 Aug 22 '17 at 20:55
  • The OP's input data does not suggest that there will be multiple matches, so there is no benefit in searching for multiple occurrences. If we are going to discuss theoretical extensions of the question, then I should note that if the targeted subarray is not found, your methods will generate: `Notice: Undefined variable: indexOfWVF` and insert a non-indexed subarray (the key is an empty string). – mickmackusa Aug 22 '17 at 23:23
  • Touche. The difference between array_merge and the union operator, could still interest somebody. But I'll admit that it's only an edge case. – jh1711 Aug 23 '17 at 00:50