1

Ex :

First array:

Array
(
    [0] => id
    [1] => ADDRESS
    [2] => ADDRESS1
    [3] => name
)

Second array:

Array
(
    [id] => 1
    [name] => Ankit
    [city] => SURAT
)

Required OUTPUT :

    [id] => 1
    [ADDRESS]=>
    [ADDRESS1]=>
    [name] => Ankit

here we can see that value of first array ADDRESS,ADDRESS1 doesn't exist in array 2 key, so i need value to be set null for ADDRESS,ADDRESS1 and unnecessary field of array 2 is city which key doesn't exist in first array values is need to be unset from result array

CODE :

    $field_arr= array('0'=>"id",
        "1"=>"ADDRESS",
        "2"=>"ADDRESS1",
        '3'=>"name",
        );
    $arr=array("id"=>"1",
        'name'=>"Ankit",
        "city"=>"Ahmedabad");
    $matching_fields =(array_diff_key(array_flip($field_arr),(array_intersect_key($arr,array_flip($field_arr)))));
    if(!empty($matching_fields)){
        foreach($matching_fields as $key=>$value){
            $new_arr[$key]=null;
        }
    }
    print_r($new_arr);
    exit;

CURRENT OUTPUT OF NEW ARRAY :

Array
(
    [ADDRESS] => 
    [ADDRESS1] => 
)

but this is long process.as well as performance also matter. i want whole code reconstruct which i have made and just get output which is required output

Here some more need help need i want same sequence of key of output array same as first array value

 my required output  :
        [id] => 1
        [ADDRESS]=>
        [ADDRESS1]=>
        [name] => Ankit

current output :
        [id] => 1
        [name] => Ankit
        [ADDRESS]=>
        [ADDRESS1]=>

Thanks in advance

2
  • 1
    array_diff, array_diff_key Commented Apr 14, 2014 at 8:36
  • @Ankit Doshi You can simply achieve it iterating first array, and make desired changes on second array. See my answer and demo for further detail Commented Apr 14, 2014 at 8:43

3 Answers 3

2

Just try with:

$keys = array('id', 'name', 'ADDRESS', 'ADDRESS1');
$data = array(
  'id'   => 1,
  'name' => 'Ankit',
  'city' => 'SURAT',
);


$output = $data + array_fill_keys($keys, null);

Output:

array (size=5)
  'id' => int 1
  'name' => string 'Ankit' (length=5)
  'city' => string 'SURAT' (length=5)
  'ADDRESS' => null
  'ADDRESS1' => null
Sign up to request clarification or add additional context in comments.

Comments

0
$keys = array_unique(array_merge($field_arr, array_keys($arr)));
$new_array = array();

foreach ($keys as $key)
{
    $new_array[$key] = isset($arr[$key]) ? $arr[$key] : '';
}
echo "<pre>";
print_r($new_array);

Comments

0

You can use following;

$first = array(
    "id",
    "name",
    "ADDRESS",
    "ADDRESS1"
);

$second = array(
    "id" => "1",
    "name" => "Ankit",
    "city" => "SURAT"
);

foreach ($first as $key) {
    if ($second[$key] == null) {
        $second[$key] = null;
    }
}

var_dump($second);

Here is working demo: Demo

3 Comments

hello i getting some issue if i have defined value 0(zero) then also its make null value which is not good –
@Ankit Use hsz's much more elegant answer, then you won't have that problem.
@AnkitDoshi I have updated my answer and demo. You can see corrected answer

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.