I am trying to comparing array value, if value found if(isset($row['height']) == ['3455']) then print the array value but it is not working it is return all the array which includes not matching value too.
How will I compare a value and if value found then print that single value of array not all value.
Here is my print_r value
Array
(
[0] => Array
(
[id] => 1
[name] => Bradeley Hall Pool
[postcode] => CW1 5QN
[lat] => 53.10213
[lon] => -2.41069
[size] => 1.60
[pegs] => 21
[distance] => 26.6
[suitability] => Array
(
[0] => Array
(
[species] => Barbel
[species] => 1
[record] => 1
[weight] => 2.721554
[length] => 40
[height] => ['abc','345m','3455']
)
)
)
[1] => Array
(
[id] => 2
[name] => Farm Pool
[postcode] => CW9 6JQ
[lat] => 53.320502
[lon] => -2.549049
[size] => 0.88
[pegs] => 8
[distance] => 15.4
[suitability] => Array
(
[0] => Array
(
[species] => Barbel
[species] => 1
[record] => 1
[weight] => 2.721554
[length] => 40
[height] => ['33','3455','3mnc']
)
)
)
)
My code -
foreach( $cursor as $row){
foreach ($row['suitability'] as $item) {
if(isset($item['height']) == ['3455']){
echo 'yes';
echo '<pre>';
print_r($item['height']);
} else{
echo 'no';
}
}
}
I think the most obvious issue may be that [height] => ['abc','345m','3455'] is not an array, otherwise the print_r() would have shown it as one. So it must be a string.
So this would work
foreach( $cursor as $row){
foreach ($row['suitability'] as $suit) {
// can we find the string in there somewhere
if (strpos($suit['height'], '3455') !== false) {
echo 'yes ';
echo $suit['height'];
echo PHP_EOL;
} else{
echo 'no';
}
}
}
There are shorter ways, but you should use isset to check for the existence of the array key. Once you know it is present, you can in_array to check for a specific value within the height array.
<?php
foreach($cursor as $row){
// Check if the row has the key `suitability`.
if (isset($row['suitability'])) {
foreach ($row['suitability'] as $item) {
// Check if the item has the key `height`.
if (isset($item['height'])) {
// Assuming `height` is always an array.
if (in_array('3455', $item['height']) {
// Yes.
}
}
}
}
}
Related
I tried to parse a string array using the code below but the required data never printed! could any one tell me how to fix it ?Thanks
$data Array structure :
Array
(
[js] => Array
(
[total_items] => 20
[max_page_items] => 2
[selected_item] => 0
[cur_page] => 0
[data] => Array
(
[0] => Array
(
[tmp] => 1
[name] => mango
[abc] => abcd4 http://mysite/items/1234
[number] => 1123
[itemCategory_title] => fruits
[logo] => 2123.png
[itemCategory_id] => 90
)
[1] => Array
(
[tmp] => 0
[name] => cherry
[abc] => abcd4 http://mysite/items/1235
[number] => 1124
[itemCategory_title] => fruits
[logo] => 2124.png
[itemCategory_id] =>
)
)
)
[text] => no error
)
php code:
<?
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2);
foreach( $data as $item ) {
echo $item['tmp'];
echo $item['name'];
echo $item['abc'];
echo $item['number'];
echo $item['itemCategory_title'];
echo $item['log'];
echo $item['itemCategory_id'];
}
?>
It should be:
foreach ($data['js']['data'] AS $item)
because the array is nested several levels down in $data.
Note that you need to call json_decode($code2, true) to get an associative array like that. By default, it returns an object, not an array, so you would do:
foreach ($data->js->data as $item) {
echo $item->tmp;
echo $item->name;
...
}
I'm implementing the first answer shown here: Finding Highest Value in Associative Array
but what's happening is it assigns the first value it finds as the "max", then when it finds the next highest value, it ALSO assigns "max" to that value.
of course my goal is to only assign "max" to the highest value.
the issue must be the way I present the values.
Here is the array (i'm assigning "max" to the 'sum' index on a PER WEEK basis):
Array
(
[0] => stdClass Object
(
[userid] => 1
[week] => 1
[sum] => 28
[user] => al
)
[1] => stdClass Object
(
[userid] => 1
[week] => 2
[sum] => 33
[user] => al
)
[2] => stdClass Object
(
[userid] => 1
[week] => 3
[sum] => 29
[user] => al
)
...
[17] => stdClass Object
(
[userid] => 26
[week] => 1
[sum] => 14
[user] => bob
)
[18] => stdClass Object
(
[userid] => 26
[week] => 2
[sum] => 23
[user] => bob
)
[19] => stdClass Object
(
[userid] => 26
[week] => 3
[sum] => 35
[user] => bob
)
...
[34] => stdClass Object
(
[userid] => 27
[week] => 1
[sum] => 36
[user] => chuck
)
[35] => stdClass Object
(
[userid] => 27
[week] => 2
[sum] => 23
[user] => chuck
)
[36] => stdClass Object
(
[userid] => 27
[week] => 3
[sum] => 29
[user] => chuck
)
But I'm delivering the data through a couple of foreach loops:
<?php
$resultsByUser = array();
$temp = array();
foreach($results as $result) {
$resultsByUser[$result->user][$result->week] = $result->sum;
}
foreach($resultsByUser as $user => $resultsByWeek) {
print "<tr><td>$user</td>";
ksort($resultsByWeek);
foreach($resultsByWeek as $week => $sum) {
$temp[$week] = max(
isset($temp[$week]) ? $temp[$week] : $sum,
$sum);
if($week == $week) {
if($temp[$week] == $sum) { ?>
<td><?php echo $sum; ?><span><i class='fa fa-trophy'></i></span></td>
<?php } else { ?>
<td><?php echo $sum; ?></td>
<?php }
}
}
} ?>
How can I force the $temp[week] aka "max" function to loop through the whole associative array of weekly values before assigning the trophy icon?
EDIT:
After the comments, I've modified the final couple of stanzas to the below, but now I don't get the trophy assigned to any value. What am I missing?
foreach($resultsByWeek as $week => $sum) {
$temp[$week] = max(
isset($temp[$week]) ? $temp[$week] : $sum,
$sum); ?>
<td><?php echo $sum; }}?>
<?php if($sum == $temp[$week]) { ?> <span><i class='fa fa-trophy'></i></span></td>
<?php } ?>
In the answer you received on the other post:
$temp = array();
foreach ($data as $item) {
foreach ($item as $key => $value) {
$temp[$key] = max(
isset($temp[$key]) ? $temp[$key] : $value,
$value);
}
}
You go through all the array of objects, updating the max value as you go through all the data elements.
That means that you will only have the maximum value (with 100% certainty) when you reach the end of that loop.
If what you want is to print a trophy icon alongside the maximum value, you need to know beforehand what is that element.
You have 2 ways to do that:
1. Use two loops
Use a first loop, without writes, just to find the maximum values.
Then use a second loop, with writes, writing the object data. And, if the maximum value gathered in the first loop equals the object data, draw the trophy icon.
Something like:
$temp = array();
foreach ($data as $item) {
foreach ($item as $key => $value) {
$temp[$key] = max(
isset($temp[$key]) ? $temp[$key] : $value,
$value);
}
}
foreach ($data as $item) {
foreach ($item as $key => $value) {
echo $user_data;
if ($user_data_sum == $temp[$key])
echo "<trophy image>";
}
}
2. Save on the object that it is the maximum value (or the trophy)
If you can edit the objects, save the maximum value on the object. That will reduce the processing time, since you'll only need a single loop instead of two as above.
Something like:
[0] => stdClass Object
(
[userid] => 1
[week] => 1
[sum] => 28
[user] => al
[is_max] => false
)
[1] => stdClass Object
(
[userid] => 1
[week] => 2
[sum] => 33
[user] => al
[is_max] => true
)
Using on the loop:
foreach ($data as $item) {
foreach ($item as $key => $value) {
echo $user_data;
if ($user_data_is_max)
echo "<trophy image>";
}
}
Or even:
[0] => stdClass Object
(
[userid] => 1
[week] => 1
[sum] => 28
[user] => al
[trophy] => ""
)
[1] => stdClass Object
(
[userid] => 1
[week] => 2
[sum] => 33
[user] => al
[trophy] => "<trophy image>"
)
Using on the loop just prints (without needing the if):
foreach ($data as $item) {
foreach ($item as $key => $value) {
echo $user_data . $user_data_trophy;
}
}
I have an array stored in $result like this
$result=$array
by using echo $result i get the following array
Array
(
[success] => 1
[product] => Array
(
[id] => 83
[seo_h1] =>
[name] => Beer Week
[manufacturer] => The Boxer Store
[model] => WPEB/0413/74/BW
[sku] => WPEB/0413/74/BW
[reward] => 0
[points] => 0
[image] => asd
[images] => Array
(
[0] => asd
)
[quantity] => 4
[price] => Rs.599
[special] =>
[discounts] => Array
(
)
[options] => Array
(
[0] => Array
(
[product_option_id] => 42
[option_id] => 25
[name] => Size Option
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 165
[option_value_id] => 72
[name] => Large
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 166
[option_value_id] => 73
[name] => XL
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 163
[option_value_id] => 70
[name] => Small
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 164
[option_value_id] => 71
[name] => Medium
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
[minimum] => 1
[rating] => 0
[description] => as
[attribute_groups] => Array
(
)
[date_added] => 2014-09-30 12:35:12
[date_modified] => 2014-10-17 17:33:46
[currency] => INR
[status] => 1
)
)
i am able to fetch the result individually like this
$finalid = $array['product']['id'];
echo $finalid;
but wish to fetch each and every data even the inner most values using for loop. for this i tried this
$c=count($result);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['id'];
echo $array[$i]['images'][0];
echo $array[$i]['options'][0]['product_option_id'];
echo $array[$i]['images'][0]['option_value'][0]['product_option_value_id'];
echo $array[$i]['images'][0]['option_value'][1]['product_option_value_id'];
}
but it didn't displayed any result can anyone tell how it can be done
Use:
$products = $result['product'];
foreach ( $products as $product )
{
echo $product['id'];
echo $product['images'][0];
echo $product['options'][0]['product_option_id'];
echo $product['images'][0]['option_value'][0]['product_option_value_id'];
echo $product['images'][0]['option_value'][1]['product_option_value_id'];
}
You may want a recursive function like this to print your multidimensional arrays:
function printIt($data){
if (is_array($data)){
foreach ($data as $index=>$slice){
if (is_array($slice)){
printIt($slice);
}else{
echo $index.": ".$slice."<br>";
}
}
}else{
echo $data."<br>";
}
}
printIt($yourArray);
Use foreach instead,
foreach ( $array['product'] as $product)
{
echo $product['id'];
echo $product['images'][0];
echo $product['options'][0]['product_option_id'];
echo $product['options'][0]['option_value'][0]['product_option_value_id'];
echo $product['options'][0]['option_value'][1]['product_option_value_id'];
}
Here you will be looping inside each element of $array['product'] and for each loop $product will have the current element of $array['product'].
The mistake in your attempt was in [$i]. For each loop $i would have 0,1,2 etc and the array index would be $array['product'][0],etc but there is no element of that sort. Your method works if your array is an Indexed array, but here it is an Associative Array.
Try this,
<?php
foreach($result as $res){
echo '<pre>'; echo $res['id'];
if(is_array($res['options']) && !empty($res['options'])) {
foreach($res['options'] as $option) {
echo '<pre>'; echo ($option['product_option_id']);
if(is_array($option['option_value']) && !empty($option['option_value'])) {
foreach($option['option_value'] as $option_value) {
echo '<pre>'; echo $option_value['product_option_value_id'];
}
}
}
}
?>
I have an array which have 3 level
Array
(
[BIOCHEMISTRY] => Array
(
[BloodRoutine] => Array
(
[DifferentialCount] => Array
(
[itemId] => 552
[catId] => 2
[testId] => 1
[parent] => 0
[title] => FBS
[unit] => mg%
[amount] => 20
[catName] => BIOCHEMISTRY
)
[ESR]=>2
[PPR]=>4
)
[PCV]=>3
)
)
i need to populate an array if testId has value
Array
(
[BIOCHEMISTRY] => Array
(
[BloodRoutine] => Array
(
[DifferentialCount] => Array
(
[itemId] => 552
[catId] => 2
[testId] => 1
[parent] => 0
[title] => FBS
[unit] => mg%
[amount] => 20
[catName] => BIOCHEMISTRY
)
)
)
)
i need to populate an array if ESR has value, no value for any of the child for DifferentialCount
Array
(
[BIOCHEMISTRY] => Array
(
[BloodRoutine] => Array
(
[ESR]=>2
)
)
)
i need to populate an array if PCV has value, no value for any of the child for BloodRoutine
Array
(
[BIOCHEMISTRY] => Array
(
[PCV]=>3
)
)
Please give me a dynamic function to do this.
Thank you.
Try
foreach($input as $key1=>$level1) {
if(is_array($level1)) {
foreach($level1 as $key2=>$level2) {
if(is_array($level2)) {
foreach($level2 as $key3=>$level3) {
if(is_array($level3)) {
foreach($level3 as $key4=>$level4) {
if(isset($level4)) {
$output[$key1][$key2][$key3][$key4]=$level4;
}
}
}
else {
if(isset($level3)) {
$output[$key1][$key2][$key3]=$level3;
}
}
}
}
else {
if(isset($level2)) {
$output[$key1][$key2]=$level2;
}
}
}
}
else {
if(isset($level1)) {
$output[$key1]=$level1;
}
}
}
echo '<pre>';print_r($output);echo '</pre>';
use foreach function and loop through the array and using the $key in the foreach check
if($key=='testid')
{//populate}
if($key=='ESR') and like wise. You need to do it in seperate foreach for the coding comfort.
Im using cakephp 2.0 and i have data submitted that i want to cleanup, the array structure is below how do i delete the element (quoteitem) where the quantity=null?
i have this but it doesnt work;
foreach($this->request->data['Quoteitem'] as $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($qi);
}
}
structure of array called ($this->request->data)
Array
(
[Quote] => Array
(
[customer_id] => 72
[user_id] => 104
)
[Range] => Array
(
[id] =>
)
[Quoteitem] => Array
(
[0] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 1
)
[1] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 22
)
[2] => Array
(
[product_id] => 339
[unitcost] => 5
[quantity] =>
)
)
)
You can remove it using the array keys:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity'] == null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}
Note that this will create gaps in the array (nonexistent indexes), usually this won't be a problem but if it is you can re-index the array with array_values().
Foreach makes a copy, try this:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}