There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a single array element
If you want to delete just one array element you can use unset()
or alternatively \array_splice()
.
If you know the value and don’t know the key to delete the element you can use \array_search()
to get the key. This only works if the element does not occur more than once, since \array_search
returns the first hit only.
Note that when you use unset()
the array keys won’t change. If you want to reindex the keys you can use \array_values()
after unset()
, which will convert all keys to numerically enumerated keys starting from 0.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
// ↑ Key which you want to delete
Output:
[
[0] => a
[2] => c
]
If you use \array_splice()
the keys will automatically be reindexed, but the associative keys won’t change — as opposed to \array_values()
, which will convert all keys to numerical keys.
\array_splice()
needs the offset, not the key, as the second parameter.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
\array_splice($array, 1, 1);
// ↑ Offset which you want to delete
Output:
[
[0] => a
[1] => c
]
array_splice()
, same as unset()
, take the array by reference. You don’t assign the return values of those functions back to the array.
Deleting multiple array elements
If you want to delete multiple array elements and don’t want to call unset()
or \array_splice()
multiple times you can use the functions \array_diff()
or \array_diff_key()
depending on whether you know the values or the keys of the elements which you want to delete.
If you know the values of the array elements which you want to delete, then you can use \array_diff()
. As before with unset()
it won’t change the keys of the array.
Code:
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = \array_diff($array, ["a", "c"]);
// └────────┘
// Array values which you want to delete
Output:
[
[1] => b
]
If you know the keys of the elements which you want to delete, then you want to use \array_diff_key()
. You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.
Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = \array_diff_key($array, [0 => "xy", "2" => "xy"]);
// ↑ ↑
// Array keys which you want to delete
Output:
[
[1] => b
]
If you want to use unset()
or \array_splice()
to delete multiple elements with the same value you can use \array_keys()
to get all the keys for a specific value and then delete all elements.
In Microsoft SQL Server Management Studio check the login configuration of your linked servers properties on the *Security" tab and create a mapped remote login for your local sa
account or configure an alternative login for all connections.
To create a linked server with a login mapping by executing sp_linkedserver
, try
sp_addlinkedsrvlogin @rmtsrvname = N'EFG', @locallogin = N'sa', @useself = N'False',
@rmtuser = N'your_remote_user', @rmtpassword = N'remotepassword'
To create a linked server with an alternative login, try
sp_addlinkedsrvlogin @rmtsrvname = N'EFG', @locallogin = NULL , @useself = N'False',
@rmtuser = N'your_remote_user', @rmtpassword = N'remotepassword'
Best Answer
From your description it seems that
Household
andPetType
has a cardinality of m-to-one; that means that an Household record could have only aPetType
while aPetType
could be associated to more than oneHousehold
record.From DB point of view that means foreign key into
Household
table. If you want to make possible a "multiple" connection betweenHousehold
andPetType
, you have to modify your relationship between entities.Just an example (disclaimer: your entities could be named differently and I didn't test this code. I'm explaining here a concept, not working on runnable code as your example didn't came with snippet examples)
After that you need to run again
php app/consolle doctrine:schema:update --force
This will update your DB schema and, because new cardinality is m-to-n, a relationship table named
household_pettype
will be created (that will hold only foreign keys from other two tables)After that you could alternatively use two methods (from household point of view)
->addPetType($petType);
that will append aPetType
object toHousehold
collection->setPetTypes($petTypeArrayCollection);
that will set in a shot allPetTypes