Remove array 0 in array PHP - 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.

Related

Find and get array key from multilevel array from other array

i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;

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

Sort multidimensional arrays recursively after setting up child-parent realtions

I have a database structure like this:
ID name sort parent
1 item1 1 0
2 subitem1 2 1
3 subsubitem1 1 2
4 subitem2 1 1
I write the database into an array
array (size=4)
0 =>
array (size=4)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
1 =>
array (size=4)
'id' => string '2' (length=1)
'name' => string 'subitem1' (length=8)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
2 =>
array (size=4)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
3 =>
array (size=4)
'id' => string '4' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
and restructure that array to set up child-parent relations with this function:
function generateNavArray($arr, $parent = 0)
{
$items = Array();
foreach($arr as $item)
{
if($item['parent'] == $parent)
{
$item['child'] = isset($item['child']) ? $item['child'] : GenerateNavArray($arr, $item['id']);
$items[] = $item;
}
}
return $items;
}
and the generated array looks like this
array (size=1)
0 =>
array (size=5)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=2)
0 =>
array (size=5)
'id' => string '2' (length=1)
'name' => string 'subitem' (length=4)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
'child' =>
array (size=1)
0 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
1 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
now i need to sort every dimension of the array by the sort value, (my "real array" has more subarrays then this one).
i played around with multisort but i can't seem to find the solution
any ideas?
Sort the array when it is 1 dimension before you build the multi-dimensional array. Even better if you are using a query, sort it there. Sort by parent then sort. When you build your multidimensional array, each child will be appended to the parent. If they are already in the correct order, they will end up in the same order.

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