I have an array of objects where the reference is not the same as the code_cip.
Array (
[9921279] => lignecatalogue Object ( [id] => 3013181 [reference] => 9921279 [commentaire] => [code_cip] => 9884064 )
[9884064] => lignecatalogue Object ( [id] => 3013193 [reference] => 9884064 [commentaire] => [code_cip] => 9884064 )
)
What i am trying to accomplish is to update only the [reference] => 9921279
on the first item to [reference] => 9884064
. The [reference]
will be updated only if it is not equal to its code_cip. So far i am stuck on the code below.
$arrLigneCats = getAllCat(); //returns above array
foreach($arrLigneCats as $ligneCat) {
if ($ligneCat->code_cip != $ligneCat->reference) {
$reference = $ligneCat->reference;
} else {
$reference = $ligneCat->code_cip;
}
}
Anyone has any idea of how to replace the key without the removing the entire line?
CodePudding user response:
If I have understood your question correctly, the problem is that $reference is just a variable, what you have to change is the reference property of the object. So it will be:
$arrLigneCats = getAllCat(); //returns above array
foreach($arrLigneCats as $ligneCat) {
if ($ligneCat->code_cip != $ligneCat->reference) {
$ligneCat->reference = $ligneCat->code_cip;
}
}
Does it answer your question?