inserting an array - php

I have the following array that I want to insert into the table with the fields groupi_id, application_id and grant_id
the array is
array(
'ApplicationsGrant' => array(
'group_id' => array(
(int) 0 => '72',
(int) 1 => '72'
),
'application_id' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'grant_id' => array(
(int) 0 => 56,
(int) 1 => 57
)
)
)
I want to insert the rows that every sub array goes with array key. so there will be 2 rows inserted in above case like this
insert into table (group_id. application_id, grant_id) Values (72, 1, 56);
insert into table (group_id. application_id, grant_id) Values (72, 2, 57);
howd I do that?

You can use Cake's Set::classicExtract() to pull out the values. I assume you know how to then save them to the DB.
http://book.cakephp.org/2.0/en/core-utility-libraries/set.html
In your case, something like (untested):
$result1 = Set::classicExtract($a, '{n}.{s}.{s}.0');
$result2 = Set::classicExtract($a, '{n}.{s}.{s}.1');
If you need the keys, you can extract those first:
$fields = Set::classicExtract($a, '{n}.{s}.{s}');

I have tried this one
$d = array(
'ApplicationsGrant' => array(
'group_id' => array(
(int) 0 => '72',
(int) 1 => '72'
),
'application_id' => array(
(int) 0 => '1',
(int) 1 => '2'
),
'grant_id' => array(
(int) 0 => 56,
(int) 1 => 57
)
)
);
for ($i=0; $i<count($d['ApplicationsGrant']['group_id']); $i++) {
echo $d['ApplicationsGrant']['group_id'][$i]."<br/>"; //outputs 72, 72
}

Related

Check if key exist in array if so, count it and create a new array php

From my SQL query, I will get the following array as a result. This is the last result I can get from SQL.I cant change any SQL because of some constraint.
I need to check if the same id exists or not and if it does need to count them and remove one of the duplicate array having the same id.
Sample array is
$array = array(
0 => array(
'id' => '17',
'status' => 1,
),
1 => array(
'id' => '18',
'status' => 1,
),
2 => array(
'id' => '21',
'status' => 1,
),
3 => array(
'id' => '5',
'status' => 2,
),
4 => array(
'id' => '18',
'status' => 1,
),
5 => array(
'id' => '22',
'status' => 5,
),
6 => array(
'id' => '6',
'status' => 1,
),
);
I need to check if they have a duplicate id or not, if yes need to count them and remove one of the duplicates.We need to preserve the array structure.
End Results should be
array(
0 => array(
'id' => '17',
'status' => 1,
'count'=1,
),
1 => array(
'id' => '18',
'status' => 1,
'count'=2,
),
2 => array(
'id' => '21',
'status' => 1,
'count'=1,
),
3 => array(
'id' => '5',
'status' => 2,
'count'=1,
),
4 => array(
'id' => '22',
'status' => 5,
'count'=1,
),
5 => array(
'id' => '6',
'status' => 1,
'count'==>1,
),
)
You can Check it by this way but duplicate entry still in the array.
$ids = array();
foreach ($array as $key => $value)
{
$ids[] = $value['id'];
$count = array_count_values($ids);
}
for($i = 0; $i < count($array);$i++)
{
$array[$i]['count'] = $count[$array[$i]['id']];
}
You can do this with the array_unique function.
Use it like so:
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
Which outputs:
Array ( [a] => red [b] => green )

Query count and group by in cakephp 2.6.8

In cakephp 2.6.8, I am trying to query a table named "mytable" in order to count rows having common values.
So, this is my query :
$zone = $this->MyModel->find('all', array(
'fields' => array('zone_id', 'COUNT(zone_id) as count'),
'group' => 'zone_id'
));
Cakephp returns this result :
zone => array(
0 => array(
MyModel => array("zone_id" = 1) ),
0 => array("count" = 77)
),
1 => array(
MyModel => array("zone_id" = 2) ),
0 => array("count" = 55)
),
2 => array(
MyModel => array("zone_id" = 3) ),
0 => array("count" = 23)
)
);
All I need is something like this :
zone = array(
0 => array("zone_id" => 1, "count" => 77),
1 => array("zone_id" => 2, "count" => 55),
2 => array("zone_id" => 3, "count" => 23)
);
How could I do to get this working without creating any Virtual Field?
Thank you for your help !

How to merge two arrays without adding another index

I was trying to add some numbers from two or more arrays to one array. My problem is, that it always adds another index.
Source arrays looks like this:
array(
(int) 0 => array(
'Sale' => array(
'id' => '1',
'market_id' => '1',
'product_ids' => '1,2,3,4,5,6,7,8',
'date_and_time' => '2014-12-28 00:00:00',
'money_spent' => '2344',
'points_given' => '213'
)
),
(int) 1 => array(
'Sale' => array(
'id' => '2',
'market_id' => '1',
'product_ids' => '44,3,32,23,12,32',
'date_and_time' => '2014-12-28 15:25:38',
'money_spent' => '123',
'points_given' => '2'
)
)
)
PHP code that im using to merge arrays and explode numbers from product_ids field
$sales=array();
foreach ($sales_detailed as $sale_detailed): {
$sale_detailed_ids=explode( ',', $sale_detailed['Sale']['product_ids'] );
array_push($sales, $sale_detailed_ids);
} endforeach;
The result is
array(
(int) 0 => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5',
(int) 5 => '6',
(int) 6 => '7',
(int) 7 => '8'
),
(int) 1 => array(
(int) 0 => '44',
(int) 1 => '3',
(int) 2 => '32',
(int) 3 => '23',
(int) 4 => '12',
(int) 5 => '32'
)
)
While i want it to look like this
array(
(int) 0 => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5',
(int) 5 => '6',
(int) 6 => '7',
(int) 7 => '8'
(int) 8 => '44',
(int) 9 => '3',
(int) 10 => '32',
(int) 11 => '23',
(int) 12 => '12',
(int) 13 => '32'
)
)
Well, you're just merging it wrong from my view of point. Make a foreach to loop through the comma-separated list and add it manually instead of pushing the whole array.
$sales=array();
foreach ($sales_detailed as $sale_detailed) {
$sale_detailed_ids = explode( ',', $sale_detailed['Sale']['product_ids'] );
foreach($sale_detailed_ids as $ids) {
$sales[] = $ids;
}
}
http://3v4l.org/hoaVF
You'll have to write some custom code to "skip" the first level of arrays, and merge only the second.
function mergeKeepIds($a, $b) {
$keys = array_unique(array_keys($a) + array_keys($b)); // Grab all the keys.
$result = [];
foreach ($keys as $key) {
$valueA = array_key_exists($key, $a) ? $a[$key] : [];
$valueB = array_key_exists($key, $b) ? $b[$key] : [];
$result[$key] = array_merge($valueA, $valueB);
}
return $result;
}
Use a variadic push technique to avoid writing a nested loop -- array_push() is most appropriate to use when you have more than one value to push into a declared array.
Code: (Demo)
$sales = [];
foreach ($sales_detailed as $sale_detailed) {
array_push($sales, ...explode(',', $sale_detailed['Sale']['product_ids']));
}
var_export($sales);

How to transform this php associative array?

I have this php associative input array.
array(
(int) 0 => array(
'Data' => array(
'id' => '12',
'type_id' => '1',
'data_value' => '35.5000'
),
'Type' => array(
'id' => '1',
'name' => 'Temperature'
)
),
(int) 1 => array(
'Data' => array(
'id' => '11',
'type_id' => '1',
'data_value' => '33.7000'
),
'Type' => array(
'id' => '1',
'name' => 'Temperature'
)
)
I want to convert it to this output array;
array(
(int) 0 => array(
(int) 0 => array(
'v' => (int) 1
),
(int) 1 => array(
'v' => '35.5000'
)
),
(int) 1 => array(
(int) 0 => array(
'v' => (int) 2
),
(int) 1 => array(
'v' => '33.7000'
)
)
The element data_value is extracted from the input array into the output array.
The code I have written looks like this;
$data2 = array();
foreach ($InputArray as $key=>$value)
{
$data2[] = array(
array(
array('v' => $key),
array('v' => $value['Data']['data_value'])
)
);
}
Unfortunately, this code does not work. The output from this code looks like this;
array(
(int) 0 => array(
(int) 0 => array(
(int) 0 => array(
[maximum depth reached]
),
(int) 1 => array(
[maximum depth reached]
)
)
),
(int) 1 => array(
(int) 0 => array(
(int) 0 => array(
[maximum depth reached]
),
(int) 1 => array(
[maximum depth reached]
)
)
)
What did I do wrong? Why do I get the error "maximum depth reached"? How can I retrieve the desired output array? I am actually doing this in cakephp.
Thank you very much for any help.
That is one wrapping array() to many:
$data2 = array();
foreach ($InputArray as $key=>$value)
{
$data2[] = array(
array('v' => $key),
array('v' => $value['Data']['data_value'])
);
}
You can see this working here. It is good use to prepare your code in services like ideome or plnkr in order to make it easier for people to help you with debugging.
As far as I know, the problem is related to a setting for PHP http://www.hardened-php.net/suhosin/configuration.html#suhosin.executor.max_depth . Could you check with your hosting provider?

Push array inside another array

I have a site develop in php and I have a function where I want to create an array inside other array.
My query are this (i'm using cakephp but in this case is only a problem of array don't tell me to use contain or something like that from cakephp because is a large query and I need to construct my query and my array in this mode)
My array $product_ingredient contain some value.
foreach ($product_ingredient as $key) {
$ingredient_level1 = $this->ProductIngredientVersion->query('SELECT * FROM ingredients_ingredients
WHERE ingredients_ingredients.ingredient_id = :ingredient_id
AND ingredients_ingredients.product_id = :id
AND ingredients_ingredients.version_id = :version_id
AND ingredients_ingredients.level = 1', array('id' => $id, 'ingredient_id' => $key['products_ingredients']['ingredient_id'], 'version_id' => $key['products_ingredients']['version_id']));
foreach($ingredient_level1 as $key2){
$ingredient_level2 = array($this->ProductIngredientVersion->query('SELECT * FROM ingredients_ingredients
WHERE ingredients_ingredients.ingredient_id = :ingredient_id
AND ingredients_ingredients.product_id = :id
AND ingredients_ingredients.version_id = :version_id
AND ingredients_ingredients.level = 2', array('id' => $id, 'ingredient_id' => $key2['ingredients_ingredients']['ingredient2_id'], 'version_id' => $key2['ingredients_ingredients']['version_id'])));
array_push($ingredient_level1, $ingredient_level2);
}
array_push($ingredient_ingredient, $ingredient_level1);
}
The result is that:
array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '34',
'level' => '1'
)
),
(int) 1 => array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '35',
'level' => '2'
)
)
)
)
)
But I would like this result
array(
(int) 0 => array(
(int) 0 => array(
'ingredients_ingredients' => array(
'id' => '34',
'level' => '1',
array(
'ingredients_ingredients' => array(
'id' => '35',
'level' => '2'
)
)
)
)
How can I solve?
Replace
array_push($ingredient_ingredient, $ingredient_level1);
by
array_push(
$ingredient_ingredient[0][0]['ingredients_ingredients'],
$ingredient_level1
);
This pushes the array to the appropriate cascade level. However it is very likely that this is the most efficient way, if you're unable to modify SQL or array structure elsewhere.

Categories