2

Given the fallowing array $array:

Array
 (
  [1] => Array
    (
        [0] => 267
        [location_id_keep] => 261
    )
  [2] => Array
    (
        [0] => 266
        [location_id_keep] => 262
    )
  [3] => Array
    (
        [0] => 2669
        [1] => 2670
        [location_id_keep] => 2668
    )
  [4] => Array
    (
        [0] => 266
        [1] => 2670
        [location_id_keep] => 2668
    )
  )

I want to add to the each arrays values that doesn't have the [location_id_keep] key the fallowing key: [location_id_delete] so that I will get this output:

Array

(

[1] => Array
    (
        [location_id_delete] => 267
        [location_id_keep] => 261
    )

[2] => Array
    (
        [location_id_delete] => 266
        [location_id_keep] => 262
    )
[3] => Array
    (
        [location_id_delete] => [2669, 2670]
        [location_id_keep] => 2668
    )

[4] => Array
    (
        [location_id_delete] => [266, 2670]
        [location_id_keep] => 2668
    )
)

Is there any method to add keys inside array assigned to already existing values that doesn't have a key ?

6
  • How did you want to have the same key location_id_delete? Commented Sep 5, 2019 at 9:17
  • 1
    Your desired output is not valid; PHP arrays cannot have more than one element with the same key. Commented Sep 5, 2019 at 9:18
  • Its wrong [location_id_delete] => 2669 [location_id_delete] => 2670 should be,for example, [location_id_delete] => [2669,2670] Commented Sep 5, 2019 at 9:18
  • Something like this [location_id_delete] => [2669, 2670] etc Commented Sep 5, 2019 at 9:19
  • I made the correction to my desired output. Commented Sep 5, 2019 at 9:21

6 Answers 6

3

This should works, but I suggest you shourld reconstruct the structure of your data. Demo

foreach($arrays as &$array){
    if($array["location_id_keep"]){
        $temp_array["location_id_keep"] = $array["location_id_keep"];
        unset($array["location_id_keep"]);
    }
    $count = count($array);
    if($count == 1) {
        $temp_array["location_id_delete"] = current($array);
    }elseif($count > 1){
        $temp_array["location_id_delete"] = array_values($array);
    }
    $array = $temp_array;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the snippet,

array_walk($arr, function (&$v) {
    // keeping location_id_keep aside to perform operation of replacing keys
    $temp = array_diff_key($v, ['location_id_keep' => '']);
    if (!empty($temp)) {
        // getting the count of sub arrays other than location_id_keep
        $cnt = count($temp);
        foreach ($temp as $key => $value) {
            // if one record then directly assign data
            if ($cnt == 1) {
                $v['location_id_delete'] = $value;
            } else{ // assign multiple data in an arrat
                $v['location_id_delete'][] = $value;
            }
            // unset numeric index or other than location_id_keep
            unset($v[$key]);
        }
    }
});

array_walk — Apply a user-supplied function to every member of an array
array_diff_key — Computes the difference of arrays using keys for comparison

Demo

Output:-

Array
(
    [1] => Array
        (
            [location_id_keep] => 261
            [location_id_delete] => 267
        )

    [2] => Array
        (
            [location_id_keep] => 262
            [location_id_delete] => 266
        )

    [3] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => Array
                (
                    [0] => 2669
                    [1] => 2670
                )

        )

    [4] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => Array
                (
                    [0] => 266
                    [1] => 2670
                )

        )

)

Comments

1

This will Works

  foreach($newArray as $key => $val){
            if(array_key_exists('location_id_keep',$val)){
                $array[$key]['location_id_keep'] = $val['location_id_keep'];
                unset($val['location_id_keep']);
            }
            $count = count($val);
            if($count>1){
                 $array[$key]['location_id_delete'] = implode(',',$val);
            }else{
                $array[$key]['location_id_delete'] = $val[0];
            }       
        }

OUTPUT

Array
(
    [1] => Array
        (
            [location_id_keep] => 261
            [location_id_delete] => 267
        )

    [2] => Array
        (
            [location_id_keep] => 262
            [location_id_delete] => 266
        )

    [3] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => 2669,2670
        )

    [4] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => 266,2670
        )

)

Comments

1

Functional solution

foreach($arrays as &$array){
    // get numeric key items 
    $temp = array_filter($array, 'is_numeric',ARRAY_FILTER_USE_KEY);
    // remove them from array and return under new key
    $array = array_diff($array, $temp) + ['location_id_delete'=>$temp];
}

demo

Comments

1

You can use foreach with implode and unset

  foreach($a as $k => &$v){
    $location = $v['location_id_keep'];
    unset($v['location_id_keep']);
    $r[] = [
      'location_id_delete' => implode(',',$v), 
      'location_id_keep'   => $location
    ];
  }

Live example : https://3v4l.org/kqCVH

Comments

1

You can try using array_walk() and array_filter(). array_filter is to filtering the values who's key is number (you mean location_id_delete here).

$array = [[0 => 267, 'location_id_keep' => 261],[0 => 266, 'location_id_keep' => 262],[0 => 2669, 1 => 2670, 'location_id_keep' => 2668], [0 => 266, 1 => 2670, 'location_id_keep' => 2668]];

array_walk($array, function (&$val) {
    $val = [
       'location_id_delete' => array_filter($val, 'is_numeric', ARRAY_FILTER_USE_KEY), 
       'location_id_keep' => $val['location_id_keep']
    ];
});

print_r($array);

Working demo.

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.