I am trying to using the in_array function to check the value exists in the second array or not. I want to search 556729685 from the following array:
$_SESSION["cart_item"] =
Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
I am using following functions to check but it is giving wrong out put:
in_array(556729685, array_keys($_SESSION["cart_item"]));
I have tried this one also:
in_array(556729685, array_values($_SESSION["cart_item"]));
None is working so help me to solve this issue.
Associative array consist of key, column and values. So, in order to check existence of value you need to reach key of array.
for($i=0;$i<count($_SESSION["cart_item"]);$i++)
{
if( in_array( 556729685 ,$_SESSION["cart_item"][$i] ) )
{
echo "exist in id:".$i.'<br/>';
}
}
Related
I'm using Google Analytics API to populate a dashboard. I'm curious if there is a setting I am unaware of that can return the rows as follows:
[0] => Array
(
[ga:pagePath] => /
[ga:pageViews] => 856
)
Instead of just 0, 1, .etc. numerical arrays:
[columnHeaders] => Array
(
[0] => Array
(
[name] => ga:pagePath
[columnType] => DIMENSION
[dataType] => STRING
)
[1] => Array
(
[name] => ga:pageViews
[columnType] => METRIC
[dataType] => INTEGER
)
)
[totalsForAllResults] => Array
(
[ga:pageViews] => 2099
)
[rows] => Array
(
[0] => Array
(
[0] => /
[1] => 856
)
[1] => Array
(
[0] => /portfolio
[1] => 268
)
I know how to do this with array logic but was just curious if there was a setting or something I could query GA with to get the first format. Couldn't find any info elsewhere.
No there isn't, neither in v3 nor in v4 (it would be terribly inefficient to repeat the dimension names for each row).
What you can do is define the index of dimensions & metrics so you can refer to values by name instead of index (below example in JavScript):
var ga = {
pagePath: 0,
pageView: 1
}
rows[1][ga.pagePath]; // /portfolio
rows[2][ga.pageViews]; // 268
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => User Object
(
[original:protected] => Array
(
[user_id] => 123456
[active] => 1
[name] => ABC XYZ
[first_name] => ABC
[last_name] => XYZ
[email] => abc#xyz.com
[username] => abcxyz
[secret_code] => S4#$sdD
)
)
[1] => User Object
(
[original:protected] => Array
(
[user_id] => 987654
[active] => 1
[name] => CBD IHK
[first_name] => CBD
[last_name] => IHK
[email] => abc#xyz.com
[username] => seCdils
[secret_code] => S4#$sdD
)
)
)
)
Identify both object array has same secret_code return boolean if exist true else false tried array_count_values return only string and number please guide thanks
I just want to check if detect secret_code same in array give me true else false
<?php
$dupes = []; // keep track of duplicates
foreach ($users as $user1) { // iterate over all items
$dupeCount = 0; // because we iterate over the same array, we always find at least the item itself (1 dupe minimum)
foreach ($users as $user2) { // check the array again
if ($user1 === $user2) { // if they are exactly the same: http://php.net/manual/en/language.oop5.object-comparison.php
$dupeCount++;
}
if ($dupeCount > 1) { // because we always find at least 1, push only when we find more than that
array_push($dupes, $user1); // add it to the result
}
}
}
However for you comment
I just want to check if detect secret_code same in array give me true
else false
which should be in your question to begin with.
collect($users)->unique(function ($item) {
return $item['secret_code'];
});
When I fire this:
$this->model->find('all',array());
I get an array with the data of the model:
Array
(
[0] => Array
(
// some data
)
[1] => Array
(
// some data
)
[2] => Array
(
// some data
)
...
)
Now is it possible instead of 0,1,2 to get the id as key for each of the data?
For example:
Array
(
[365] => Array
(
[model] => Array
(
[id] => 365
// some data
)
)
[442] => Array
(
[model] => Array
(
[id] => 442
// some data
)
)
[1000] => Array
(
[model] => Array
(
[id] => 1000
// some data
)
)
...
)
I know that find('list') can do something like that but only for 2 fields max (one key and one value).
you can build it maybe this code can do it :
$final_array = array();
foreach($returned_array as $k => $v){
$final_array[$v['id']] = $v;
}
print_r($final_array);
It's still an after-find operation but take a look at Set::combine (http://book.cakephp.org/2.0/en/core-utility-libraries/set.html) as a more elegant solution. Something like:
$newArray = Set::combine($yourArray, '{n}.Model.id');
/* $newArray now looks like:
Array
(
[365] =>
[442] =>
[1000] =>
)
*/
(more or less just the the docs example) might work in your case.
I've got two tables that are setup to use the tree behavior, manufacturers, and categories.
Products can only belong to one category and only one manufacturer, however some manufacturers(child) are owned by other manufacturers(parent), and likewise some categories(child) are a subcategory of another(parent).
I want to do the following:
given a category id (parent), find all products in subcategories
given a manufacturer id (parent), find all products in child manufacturers
I have tried the following (in products controller):
$conditions['Product.category_id'] = $this->Product->Category->children($id,false,'id');
$this->paginate = array(
'conditions' => $conditions,
'limit' => 21
);
$products = $this->paginate('Product');
$this->set(compact('products'));
but it gives me this:
WHERE `Product`.`category_id` IN (Array, Array, Array, Array, Array, Array)
if I do a print_r I can see it is grabbing the information I need(see below), but how can I get to it, and is there better way to do this?
Array
(
[Product.category_id] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
)
)
[1] => Array
(
[Category] => Array
(
[id] => 12
)
)
[2] => Array
(
[Category] => Array
(
[id] => 23
)
)
[3] => Array
(
[Category] => Array
(
[id] => 24
)
)
[4] => Array
(
[Category] => Array
(
[id] => 25
)
)
[5] => Array
(
[Category] => Array
(
[id] => 26
)
)
)
)
The query is failing because it expects an array containing only the category-ids, like this:
$conditions['Product.category_id'] = array(1,4,5,6);
You can achieve this by 'extracting' those values from your array using Hash::extract() (or Set::extract() if you're using CakePHP 1.3)
$categoryIds = $this->Product->Category->children($id,false,'id');
$conditions['Product.category_id'] = Hash::extract($categoryIds, '{n}.Category.id);
Read the documentation on the Hash Utility here:
http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::extract
So here is my array, what I want to do is unset the [detail][$x] keys leaving just the lowest total. Can anyone assist? Thank you in advance..
There is alot of Products to loop though, what I mean is that its not just one item in the array.
[1] => Array
(
[name] => Product Name 1
[detail] => Array
(
[1] => Array
(
[total] => 10.14
)
[2] => Array
(
[total] => 12.18
)
[3] => Array
(
[total] => 9.90
)
You can find out the lowest total and overwrite the whole detail. Something like that:
$lowestValue = false;
foreach ($array[1]['detail'] as $detail) {
if ($lowestValue === false || $lowestValue > $detail['total']) {
$lowestValue = $detail['total'];
}
}
$array[1]['detail'] = array(0 => array('total' => $lowestValue));