merge array and replace values [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two arrays.
First, lets call it array1:
array (size=8)
0 =>
array (size=6)
'id' => string '2' (length=1)
'domacin' => string 'Man City' (length=8)
'gost' => string 'Liverpool' (length=9)
'tip' => string '1' (length=1)
'kvota' => string '1.8' (length=3)
'status' => string 'Aktivan' (length=7)
1 =>
array (size=6)
'id' => string '4' (length=1)
'domacin' => string 'Inter' (length=5)
'gost' => string 'Milan' (length=5)
'tip' => string '1' (length=1)
'kvota' => string '2.5' (length=3)
'status' => string 'Aktivan' (length=7)
And second, array2:
'id' => string '2' (length=1)
'domacin' => string 'Man City' (length=8)
'gost' => string 'Liverpool' (length=9)
'tip' => string '1' (length=1)
'kvota' => string '1.8' (length=3)
'status' => string 'Gubitan' (length=7)
I want to merge this two arrays but to use values from second where id from first is equal with id from second.
Expected result would be:
array (size=8)
0 =>
array (size=6)
'id' => string '2' (length=1)
'domacin' => string 'Man City' (length=8)
'gost' => string 'Liverpool' (length=9)
'tip' => string '1' (length=1)
'kvota' => string '1.8' (length=3)
'status' => string 'Gubitan' (length=7)
1 =>
array (size=6)
'id' => string '4' (length=1)
'domacin' => string 'Inter' (length=5)
'gost' => string 'Milan' (length=5)
'tip' => string '1' (length=1)
'kvota' => string '2.5' (length=3)
'status' => string 'Aktivan' (length=7)
Check the status where index is 0. It changed from 'Aktivan'(from array1) to 'Gubitan' like we had in array2.
How to do that?

foreach ($firstArr as &$item) {
if ($item['id'] == $secondArr['id']) {
$item = $secondArr;
}
}

You can do this easily using two foreach loops :
foreach( $array1 as $key1=>$element1 ){
foreach( $array2 as $element2 ){
if ($element1['id'] == $element2['id'])
{
$array1[$key1]['status'] = $element2['status'];
}
}
}

Related

How to append an associative into another associative array by continuing the increment of keys? [duplicate]

This question already has answers here:
PHP append one array to another (not array_push or +)
(11 answers)
Closed 2 years ago.
I have the following associative array:
0 =>
array (size=2)
'id' => string '0000' (length=4)
'polling_id' => string '0' (length=1)
1 =>
array (size=2)
'id' => string '0001' (length=4)
'polling_id' => string '1' (length=1)
And I have this second associative array:
0 =>
array (size=3)
'id' => string '0002' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
1 =>
array (size=3)
'id' => string '0003' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
I want it to look like:
0 =>
array (size=2)
'id' => string '0000' (length=4)
'polling_id' => string '0' (length=1)
1 =>
array (size=2)
'id' => string '0001' (length=4)
'polling_id' => string '1' (length=1)
2 =>
array (size=3)
'id' => string '0002' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
3 =>
array (size=3)
'id' => string '0003' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
How can I achieve this?
Try array_merge($a1,$a2)to merge these two arrays.

Objects of a Doctrine Collection is replaced by DB values in second time callling from database

Need to change some object values of a doctrine collection fetched from database and save again to the database. For this, there is a old written code and In that they have used a loop to save objects one by one. The reason of saving objects one by one is because there some other processes are done in the middle. In the end of the first loop there is a function which is trying to fetch another doctrine collection from the database. Now that entire collection is existed of db values. Because of that 1st collection is replaced by existing database values. So updating process is not working. Why is this happening ?
Example Code :
class mainAction extends sfAction {
function execute($request) {
$a = new ExampleA();
$collection = $a->updateDoctrineCollectionWithNewValues(6);
$b = new ExampleB();
$b->saveDoctrineCollectionToDB($collection);
}
}
class ExampleA {
function updateDoctrineCollectionWithNewValues($empNumber){
$empSalaryComponents = Doctrine_Core::getTable('EmployeeSalaryComponent');
$empSalComCollection = $empSalaryComponents->findBy('employee_number', $empNumber);
foreach ($empSalComCollection as $empSalComponent) {
$empSalComponent->setValue(9999999999999);
}
return $empSalComCollection;
}
}
class ExampleB {
function saveDoctrineCollectionToDB($doctrineCollection){
var_dump($doctrineCollection->toArray()); // 1st var_dump
foreach ($doctrineCollection as $object) {
$this->addToLogger($object);
var_dump($doctrineCollection->toArray()); //2nd var_dump
die;
}
}
function addToLogger($object) {
$empSalaryComponents = Doctrine_Core::getTable('EmployeeSalaryComponent')->findBy('employee_number', $object->getEmployeeNumber());
/**
* Do something
**/
}
}
Results
1st var_dump :
array (size=4)
0 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '1' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
1 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '2' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
2 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '3' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
3 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '4' (length=1)
'value' => int 9999999999999
'effectiveDate' => string '2019-01-19' (length=10)
'is_active' => string '1' (length=1)
2nd var_dump:
0 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '1' (length=1)
'value' => string '6787' (length=4)
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
1 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '2' (length=1)
'value' => string '71111' (length=5)
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
2 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '3' (length=1)
'value' => string '34%' (length=3)
'effectiveDate' => string '2019-01-15' (length=10)
'is_active' => string '1' (length=1)
3 =>
array (size=5)
'employee_number' => string '6' (length=1)
'salary_component_id' => string '4' (length=1)
'value' => string '6787' (length=4)
'effectiveDate' => string '2019-01-19' (length=10)
'is_active' => string '1' (length=1)
2nd var_dump is filled with db values since calling db in the middle of the loop.
Env : Doctrine 1.2, Php7.2, Symfony 1.4

Remove array 0 in array PHP

I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.

Compare arrays and add missing key value to array [duplicate]

This question already has answers here:
PHP, Merging arrays with common keys
(2 answers)
Closed 6 years ago.
I have two arrays:
First
array (size=6)
0 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '45' (length=2)
1 =>
array (size=2)
'Age' => string '55-64' (length=5)
'Count' => string '1' (length=1)
2 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '46' (length=2)
5 =>
array (size=2)
'Age' => string '45-54' (length=5)
'Count' => string '2' (length=1)
Second:
array (size=5)
0 =>
array (size=2)
'Age' => string '65+' (length=3)
'Count' => string '1' (length=1)
1 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '4' (length=1)
2 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '20 |' (length=4)
Now what here I am getting is that First array size is larger than Second one, so I need a solution for making the small size array similar to larger size array.
With same keys, and add value zero to new added keys value.
array_merge( $firstArray, $secondArray );
Please see array_merge();

Merge 1 multidimensional array with a simple array

I am trying to merge 2 arrays: 1 multidimensional and another one normal:
Multidimensional array - $_SESSION["products"]
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '1' (length=1)
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '1' (length=1)
'price' => string '2300.50' (length=7)
Simple array - $qty
array (size=2)
0 => string '2' (length=1)
1 => string '3' (length=1)
EXPECTED OUTPUT
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '2' (length=1) // notice the qty change
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '3' (length=1) // notice the qty change
'price' => string '2300.50' (length=7)
I tried:
foreach ($_SESSION["products"] as $cart_itm){
foreach($qty as $qt) {
$cart_itm['qty'] = $qt;
}
}
But did not work, cart_itm['qty'] remained the same (1).
Try with this:
foreach ($_SESSION["products"] as $key => &$cart_itm){
$cart_itm['qty'] = $qty[$key];
}

Categories