1

Hello Developer I am trying to check one single array into multiple array value and My exact requirement is the value of one array should be check in each indexes of second array and replace if same otherwise append it in new array.

My Two array was like this:- One array:-

$array1 = array(
    "extension_date" => "2017-05-19",
    "extended_date" => "2017-05-27"
);

Second Array:-

$array2 = array(
    0 => array(
        "extension_date" => "2017-05-19",
        "extended_date" => "2017-05-27"
    ),
    1 => array(
        "extension_date" => "2017-05-31",
        "extended_date" => "2017-05-31"
    ),
);

I am try it from yesterday but it's not be succeed so please help me to solve out this issue.

2
  • use foreach() with == or === operator Commented May 26, 2017 at 6:48
  • show your expected out put Commented May 26, 2017 at 6:48

3 Answers 3

1

You can make use of array_search and array_push. You don't need to replace if you find the search array in the main array since it's the exact same thing.

$search = [
    "extension_date" => "2017-05-19",
    "extended_date" => "2017-05-27"
];

$data = [
    [
        "extension_date" => "2017-05-19",
        "extended_date" => "2017-05-27"
    ],
    [
        "extension_date" => "2017-05-31",
        "extended_date" => "2017-05-31"
    ]
];

if (array_search($search, $data) === false) {
    array_push($data, $search);
}

// $data contains $search if it's missing
Sign up to request clarification or add additional context in comments.

Comments

0

Here we are using array_search if needle doesn't exist then we add that in array.

Try this code snippet here

<?php

ini_set('display_errors', 1);

$array1 = array(
    "extension_date" => "2017-05-19",
    "extended_date" => "2017-05-27"
);
$array2 = array(
    0 => array(
        "extension_date" => "2017-05-19",
        "extended_date" => "2017-05-27"
    ),
    1 => array(
        "extension_date" => "2017-05-31",
        "extended_date" => "2017-05-31"
    ),
);

if(array_search($array1, $array2)===false)
{
    $array2[]=$array1;
}
print_r($array2);

Comments

0

Inputs:

$array1 = array(
    "extension_date" => "2017-05-19",
    "extended_date" => "2017-05-27"
);
$array2 = array(
    array(
        "extension_date" => "2017-05-19",
        "extended_date" => "2017-05-27"
    ),
    array(
        "extension_date" => "2017-05-31",
        "extended_date" => "2017-05-31"
    ),
);

Method:

if(!in_array($array1,$array2)){
    $array2[]=$array1;
}

Since you are only checking to see if the subarray exists and you don't care about its key, it doesn't make sense to use array_search(). in_array() was specifically designed to return true false -- so use it!

var_export($array2) output:

array (
  0 => 
  array (
    'extension_date' => '2017-05-19',
    'extended_date' => '2017-05-27',
  ),
  1 => 
  array (
    'extension_date' => '2017-05-31',
    'extended_date' => '2017-05-31',
  ),
)

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.