Find key based on value PHP - php

[items] => Array
(
[0] => Array
(
[product_option_id] => 328
[option_id] => 26
[name] => Product Type
[type] => checkbox
[group_no] => 1
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 1473
[option_value_id] => 68
[option_boolean_value] => True
[option_cost] => 10.0000
[option_enable] => 1
[apply_default] => 1
[apply_option] => 0
)
[1] => Array
(
[product_option_value_id] => 1474
[option_value_id] => 151
[option_boolean_value] => False
[option_cost] => 0.0000
[option_enable] => 1
[apply_default] => 0
[apply_option] => 0
)
)
[required] => 1
[optionapply] => A
[booleanvalue] => True
[applyheading] => 1
[flagheading] => 0
[dhead_id] => 0
[routes_dephead_id] => Array
(
[0] => 44
[1] => 37
)
[routes_id] => Array
(
[0] => 948
[1] => 949
)
)
How can i get the key where option_value_id is 68. Here 68 is not static each time it will be dynamic i want dynamic solution.

<?php
function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
http://php.net/manual/en/function.array-search.php

Try This one.
function search_id($array, $id)
foreach ($array['items'] as $key => $val) {
foreach($val['option_value'] as $option_key => $option_value)
{
if ($option_value['option_value_id'] == $id)
{
return $option_key;
}
}
}

Related

How to check for a value in a multi dimensional array

My Array is below
$sample_arr = Array
(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 20
[ref] => ref 2
)
[2] => Array
(
[index] => 2
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 21
[ref] => ref 2
)
)
As you can see in the above array there is article_Id twice in the array with value 6
I would like to find the details of the second row so that I can make the second row qty 41 i.e my result array will be like the one below
I have tried the in_array function but still there is something missing
I also tried with the foreach but the problem is that how to get the first appearance row qty? i.e in the $sample_arr when the user adds the third record with article_Id 6 the second row must be updated as shown below
$result_arr = Array
(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref goes here
)
)
Looks like article ID is unique. So, we can kep storing data in this $newArray variable with the key as article ID.
Every time, it's checked if a record containing the article id exists. If so, the quantity is added. If not, it's appended to the $newArray.
$newArray = array();
foreach($sample_arr as $arr) {
if (isset($newArray[$arr['article_Id']])) {
$newArray[$arr['article_Id']]['qty'] += $arr['qty'];
$newArray[$arr['article_Id']]['ref'] = 'Ref goes here'; // If this the string that replaces the ref
} else {
$newArray[$arr['article_Id']] = $arr;
}
}
$newArray = array_values($newArray);
Output:
Array
(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref 2
)
)
Try the following:
$new_merged_array = array();
if(!empty($sample_arr )){
foreach($sample_arr as $sample_ar){
if(!empty($sample_ar) && isset($sample_ar['article_Id'])){
if(isset($new_merged_array[$sample_ar['article_Id']])){
$new_merged_array[$sample_ar['article_Id']]['qty'] += $sample_ar['qty'];
}else{
$new_merged_array[$sample_ar['article_Id']] = $sample_ar;
}
}
}
}
print_r($new_merged_array);
Output will be:
Array(
[4] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[6] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref 2
)
)
Another way:
$new_merged_array = $sample_array = array();
if(!empty($sample_arr )){
foreach($sample_arr as $sample_ar){
if(!empty($sample_ar) && isset($sample_ar['article_Id'])){
if(isset($new_merged_array[$sample_ar['article_Id']])){
$new_merged_array[$sample_ar['article_Id']]['qty'] += $sample_ar['qty'];
}else{
$new_merged_array[$sample_ar['article_Id']] = $sample_ar;
}
}
}
}
if(!empty($new_merged_array )){
foreach($new_merged_array as $new_merged_arr){
$sample_array[] = $new_merged_arr;
}
}
print_r($sample_array);
Output will be:
Array(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref 2
)
)
$sample_arr = array
(
0 => array
(
'index'=> 0,
'in_id' => 309,
'date' => '2016-08-01',
'article_Id' => 4,
'qty' => 50,
'ref' => 'ref'
),
1 => array
(
'index' => 1,
'in_id' => 309,
'date' => '2016-08-01',
'article_Id' => 6,
'qty' => 20,
'ref' => 'ref 2'
),
2 => array
(
'index' => 2,
'in_id' => 309,
'date' => '2016-08-01',
'article_Id' => 6,
'qty' => 21,
'ref' => 'ref 2'
)
);
$articleArray = [];
foreach($sample_arr as $key=>$value){
$articleId = $value['article_Id'];
if(array_key_exists($articleId,$articleArray)){
$articleArray[$articleId]['qty'] = $articleArray[$articleId]['qty'] + $value['qty'];
}else{
$articleArray[$articleId] = $value;
}
}
$articleArray = array_values($articleArray);
print_r($sample_arr);
print_r($articleArray);
Demo
in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
Usage:
$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';

get value from cake php multi dimention arrary

I have tried many things but could not get the output, would really appreciate any help
Thank you
Array (
[0] => Array ( [Toolrepos] =>
Array (
[id] => 28
[created] => 2014-12-13
[tool_type] => new1
[tool_partnum] => new3
[tool_vernum] => 57.0.5
[box_id] => 28
[request_date] => 2014-12-14
[delivered_date] => 2014-12-14 ) )
[1] => Array ( [Toolrepos] =>
Array (
[id] => 29
[created] => 2014-12-13
[tool_type] => new4
[tool_partnum] => new5
[tool_vernum] => 1.2.56
[box_id] => 28
[request_date] => 2014-12-14
[delivered_date] => 2014-12-14 ) )
[2] => Array ( [Toolrepos] =>
Array ( [id] => 29
[created] => 2014-12-13
[tool_type] => SeatApp
[tool_partnum] => sw2
[tool_vernum] => 1.1.2
[box_id] => 34
[request_date] => 2014-12-13
[delivered_date] => 2014-12-13 ) ) )
I need the output like below
if box_id = '28' then i need their corresponding values for 'created','tool_type','tool_vernum'. Sometimes I need only 'created' value for matching box_id. Thank you
$box28s = array();
$i=0;
if (! empty($arr)) {
foreach ($arr as $elem) {
$curr = ! empty($elem['Toolrepos']) ? $elem['Toolrepos'] : NULL;
if (! empty($curr)) {
foreach ($curr as $k => $v) {
if ($k == 'id' && $v == 28) {
$box28s[$i] = $curr;
}
}
}
++$i;
}
}

How to loop over associative array using foreach loop construct in following scenairo?

I've an array titled $rebate_by_product:
Array
(
[op] => preview
[id] =>
[form_submitted] => yes
[company_id] => 46
[1] => Array
(
[pack] => 10
[quantity] => 20
[volume] => 30
[units] => 9
[amount] => 40
[rebate_start_date] => 2014-05-01
[rebate_expiry_date] => 2014-05-05
[applicable_states] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[rebate_total_count] => 5000
[products] => Array
(
[1] => 9
[2] => 10
)
)
[2] => Array
(
[pack] => 50
[quantity] => 60
[volume] => 70
[units] => 10
[amount] => 80
[rebate_start_date] => 2014-05-06
[rebate_expiry_date] => 2014-05-10
[applicable_states] => Array
(
[0] => 14
[1] => 15
[2] => 16
)
[rebate_total_count] => 10000
[products] => Array
(
[1] => 11
[2] => 8
)
)
[3] => Array
(
[pack] => 100
[quantity] => 200
[volume] => 300
[units] => 7
[amount] => 400
[rebate_start_date] => 2014-05-21
[rebate_expiry_date] => 2014-05-30
[applicable_states] => Array
(
[0] => 26
[1] => 33
[2] => 42
)
[rebate_total_count] => 9999
[products] => Array
(
[1] => 9
[2] => 8
)
)
[multiselect] => 42
)
You can observe from above array that it has few elements which are not array but it has three such elements which are themselves array and even few of its data elements are also arrays so how to loop over this kind of array using foreach loop?
If you just want to print each one the just use foreach loop. Consider this example:
$product_keys = array(); // edited
// loop them, if its an array, loop inside it again
foreach($rebate_by_product as $index => $element) {
if(is_array($element)) {
foreach($element as $key => $value) {
if(is_array($value)) {
// EDITED
if($key == 'products') {
$product_keys = array_merge($product_keys, $value);
}
$value = implode(',', $value);
echo "$key => $value <br/>";
} else {
echo "$key => $value <br/>";
}
}
} else {
echo "$index => $element <br/>";
}
}
// if product items has duplicates check here (edited)
if(count($product_keys) != count(array_unique($product_keys))) {
echo "<script>alert('This array has duplicate products');</script>";
} else {
echo "<script>alert('Products are ok');</script>";
}
Or if you want, you cant just use iterators on this one:
$recursive = new RecursiveIteratorIterator(new RecursiveArrayIterator($rebate_by_product));
foreach($recursive as $key => $value) {
echo "$key => $value <br/>";
}
I'd propose you're use a recursive approach to bring all the entries of the array on the same level and then print this array:
function loopArray($inputVal,$inputKey = "") {
if(is_array($inputVal)) {
$output = array();
foreach($inputVal as $key => $value) {
$output = array_merge($output,loopArray($value,$key));
}
return $output;
} else {
return array($inputKey => $inputVal);
}
}
// Just for presenting:
$yourArray = array(
"1" => "1",
array(
"2.1" => "2.1",
array(
"2.2.1" => "2.2.1"
)
),
"3" => "3",
array(
"4.1" => "4.1"
)
);
$newArray = loopArray($yourArray);
// > array("1" => 1,"2.1" => "2.1","2.2.1" => "2.2.1","3" => "3","4.1" => "4.1")
foreach($newArray as $key => $value) {
echo $key." => ".$value."<br/>";
}
// > 1 => 1
// > 2.1 => 2.1
// > 2.2.1 => 2.2.1
// > 3 => 3
// > 4.1 => 4.1

cannot find value in array

I can't see where I am wrong with this code so I kindly ask for your help.
I have two arrays:
Array (
[0] => Array (
[description] => Generali di Proprieta'
[idmov] => 34
[mov] => Manutenzioni
[total] => 8000
)
[1] => Array (
[description] => Generali di Proprieta'
[idmov] => 35
[mov] => Assicurazioni
[total] => 6000
)
[2] => Array (
[description] => Generali di Proprieta'
[idmov] => 36
[mov] => Cancelleria Postali
[total] => 1850
)
[3] => Array (
[description] => Generali di Proprieta'
[idmov] => 37
[mov] => Bancarie passive
[total] => 700
)
[4] => Array (
[description] => Generali di Proprieta'
[idmov] => 38
[mov] => Amministrazione
[total] => 15000
)
)
and
Array (
[0] => Array (
[center] => 8
[caus] => 34
[total] => 38175.04
)
[1] => Array (
[center] => 8
[caus] => 35
[total] => 6132.00
)
[2] => Array (
[center] => 8
[caus] => 36
[total] => 223.80
)
[3] => Array (
[center] => 8
[caus] => 37
[total] => 114.70
)
[4] => Array (
[center] => 8
[caus] => 38
[total] => 14625.07
)
[5] => Array (
[center] => 8
[caus] => 39
[total] => 7450.48
)
I use this function
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['caus'] === $id) {
return $key;
}
}
return null;
}
to look in array B for each item of array A with this code:
for($i=0;$i<$length;$i++){
if(searchForId($voce_bdg[$i]['idmov'], $voce_actual)){
$key=searchForId($voce_bdg[$i]['idmov'], $voce_actual);
$actual=$voce_actual[$key]['importo'];
echo '<td class="report">'.number_format($actual,2,',','.').'</td>';
}else{
echo '<td class="report">0,00</td>';
}
}
It works for every item like a charm except for the first item where it returns 0.
Where am I wrong??
Thanks in advance for your help!
Lelio
PHP treats the index 0 as a false. As such, if you find your result in index zero, it won't pass the if() statement you have.
Since your function returns null if no record found, why not try to check for null?
for($i = 0; $i < $length; $i++)
{
// Use is_null() check below. If it is not null, it is found.
// Also, instead of doing searchForId() twice, just do it once and check for the result.
$key = searchForId($voce_bdg[$i]['idmov'], $voce_actual);
if(! is_null ($key))
{
$actual = $voce_actual[$key]['importo'];
echo '<td class="report">'.number_format($actual,2,',','.').'</td>';
}
else
{
echo '<td class="report">0,00</td>';
}
}
try replacing operator === for ==
It does return something. It return 0 since the key is 0. But your if() interpret it as a "false"
change
if(searchForId($voce_bdg[$i]['idmov'], $voce_actual)){
with
if(searchForId($voce_bdg[$i]['idmov'], $voce_actual) != null){

PHP search array in array

I have array result like this:
Array
(
[0] => stdClass Object
(
[id_global_info] => 78
[name] => rfhd
[body] => dhfdhdf
[contact_author] => mirko
[date_created] => 2012-03-15 16:11:54
[date_expires] => 2012-04-14 16:11:54
[email] =>
[location_id] => 1
[category_id] => 26
[tag] => fhdhfdhfd
[info_type_id] => 4
[user_id] => 3
)
[1] => stdClass Object
(
[id_global_info] => 79
[name] => rfhd
[body] => dhfdhdf
[contact_author] => mirko
[date_created] => 2012-03-15 16:11:56
[date_expires] => 2012-04-14 16:11:56
[email] =>
[location_id] => 1
[category_id] => 26
[tag] => fhdhfdhfd
[info_type_id] => 4
[user_id] => 3
)
[2] => stdClass Object
(
[id_global_info] => 80
[name] => rfhd
[body] => dhfdhdf
[contact_author] => mirko
[date_created] => 2012-03-15 16:11:56
[date_expires] => 2012-04-14 16:11:56
[email] =>
[location_id] => 1
[category_id] => 26
[tag] => fhdhfdhfd
[info_type_id] => 4
[user_id] => 3
)
.
.
.
)
How can I search a multidimensional array and count number of results (for example I want to search for info_type_id with value of 4)?
Use array_filter to filter the array:
function test($arr) {
return $arr["info_type_id"] == 4;
}
echo count(array_filter($yourArray, "test"));
with foreach ?
function searchMyCoolArray($arrays, $key, $search) {
$count = 0;
foreach($arrays as $object) {
if(is_object($object)) {
$object = get_object_vars($object);
}
if(array_key_exists($key, $object) && $object[$key] == $search) $count++;
}
return $count;
}
echo searchMyCoolArray($input, 'info_type_id', 4);
You should try this :
$counter = 0;
$yourArray; // this var is your current array
foreach($yourArray as $object){
if($object->info_type_id == 4){
$counter++;
}
}

Categories