I have this Array but i don't know how to get the [discount_amount] based on the [object_ids].
For example i would like to get the 93 value if my [object_ids] contain 81.
Array
(
[0] => Array
(
[id] => rule_5b0d40cd1408a
[membership_plan_id] => 106
[active] => yes
[rule_type] => purchasing_discount
[content_type] => post_type
[content_type_name] => product
[object_ids] => Array
(
[0] => 81
)
[discount_type] => amount
[discount_amount] => 93
[access_type] =>
[access_schedule] => immediate
[access_schedule_exclude_trial] =>
)
[1] => Array
(
[id] => rule_5b0d4e0f3b0b4
[membership_plan_id] => 106
[active] => yes
[rule_type] => purchasing_discount
[content_type] => post_type
[content_type_name] => product
[object_ids] => Array
(
[0] => 110
)
[discount_type] => amount
[discount_amount] => 50
[access_type] =>
[access_schedule] => immediate
[access_schedule_exclude_trial] =>
)
)
You could use a foreach and use in_array to check if the array object_ids contains 81.
foreach ($arrays as $array) {
if (in_array(81, $array["object_ids"])) {
echo $array["discount_amount"];
}
}
Demo
Try with this code .
Assuming $dataArray is the array you have printed.
foreach ($dataArray as $key => $value){
if($value['object_ids'][0] == 83){
$discount_amount = $value['discount_amount'];
}
}
echo $discount_amount
The way I mostly do this is as followed:
# Save retrieved data in array if you want to.
$test = array();
foreach($array as $line){
# Count the row where discount_amount is found.
$test[] = $line['9'];
echo "Result: ".$line['9']. "<br \>\n";
# OR another method is
$test[] = $line['discount_amount'];
echo "Result: ".$line['discount_amount']. "<br \>\n";
}
Related
I am parsing A JSON file in PHP using PHP Decode
$json= file_get_contents ("resultate.json");
$json = json_decode($json, true);
and then I am trying to loop through the results and display them
foreach($json as $zh){
$name = $zh[0]["VORLAGEN"][0]["JA_PROZENT"];
$JA = $zh[0]["VORLAGEN"][0]["VORLAGE_NAME"];
$kreis = $zh[0]["NAME"];
$kreis1 = $zh[22]["NAME"];
echo $name;
echo $JA;
echo $kreis;
echo $kreis1;
...}
but I receive only one element e.g. position 0 or position 22. I would like to receive all the results and display them in a list. Below you can see the array
Array
(
[GEBIETE] => Array
(
[0] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 205
[STIMMBETEILIGUNG] => 28.11
[NEIN_STIMMEN_ABSOLUT] => 183
[VORLAGE_ID] => 2491
[JA_PROZENT] => 52.84
)
)
[NAME] => Aeugst a.A.
[WAHLKREIS] => 0
[BFS] => 1
)
[1] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 1000
[STIMMBETEILIGUNG] => 26.15
[NEIN_STIMMEN_ABSOLUT] => 851
[VORLAGE_ID] => 2491
[JA_PROZENT] => 54.02
)
)
[NAME] => Affoltern a.A.
[WAHLKREIS] => 0
[BFS] => 2
)
[2] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 661
[STIMMBETEILIGUNG] => 30.98
[NEIN_STIMMEN_ABSOLUT] => 454
[VORLAGE_ID] => 2491
[JA_PROZENT] => 59.28
)
)
[NAME] => Bonstetten
[WAHLKREIS] => 0
[BFS] => 3
)
can you please tell me how to print all the elements of this array?
There is GEBIETE layer in your json, so change foreach($json as $zh) to foreach($json["GEBIETE"] as $zh) will works.
You can nest multiple foreach:
$root = $json['GEBIETE'];
foreach ($root as $elem) {
foreach ($elem as $key => $value) {
// VORLAGEN is an array of its own so if you want to print the keys you should foreach this value as well
if(is_array($value)) {
foreach ($value[0] as $k => $v) {
echo $k."\t".$v;
}
}
else {
echo $value;
}
}
}
I have a multidimensional array as below:
$rows[] = $row;
Now I want to create variable from looping this array. This is how I tried it:
foreach ($rows as $k => $value) {
//echo '<pre>',print_r($value).'</pre>';
$id = $value['news_id'];
$title = $value['news_title'];
echo $title;
}
But it produce an error as below:
...... Illegal string offset 'news_id'
This is the output of - echo '<pre>',print_r($value).'</pre>';
Array
(
[news_id] => 1110
[news_title] => test
[news] => test des
)
1
Array
(
[news_id] => 1109
[news_title] => ශ්රී ලංකාවේ ප්රථම....
[news] => දහසක් බාධක....
)
1
Can anybody tell me what is the wrong I have done?
UPDATE
output for echo '<pre>',print_r($rows).'</pre>';
Array
(
[0] =>
[1] => Array
(
[news_id] => 1110
[news_title] => test
[news] => test des
)
[2] => Array
(
[news_id] => 1109
[news_title] => ශ්රී ලංකාවේ ප්රථම....
[news] => දහසක් බාධක....
)
)
1
use isset function because your 0 index is empty in $row
foreach ($rows as $k => $value) {
if(isset($value['news_id'])){
$id = $value['news_id'];
$title = $value['news_title'];
echo $title;
}
}
you should add check (condition) when you assign data to $rows
I want to remove array from multidimentional array using base of ID. I tried using foreach loop. But, It is not working.
Any one help me how to remove this?
Thanks.
=> Array :
Array
(
[0] => Array
(
[id] => 11109
[value] => Yes
[field_id] => 234
)
[1] => Array
(
[id] => 11109
[value] => Yes
[field_id] => 237
)
[2] => Array
(
[id] => 11110
[value] => No
[field_id] => 234
)
[3] => Array
(
[id] => 11110
[value] => No
[field_id] => 237
)
[4] => Array
(
[id] => 11111
[value] => No
[field_id] => 237
)
)
From this array, I want to remove array which
field_id is 234 and value is "No" && field_id is 237 and value is
"No".
field_id is 234 and value is "Yes" && field_id is 237 and value is
"Yes".
So, From this array only ID 11111 array is display otherwise all array remove from current array.
Here is my code using I tried to remove array.
foreach ($collection->getData() as $key => $value) {
if(($value['field_id'] == $ids[0] && $value['value'] == "No")){
echo $value['id'];
// exit;
break;
}
echo $value['field_id'];
echo $value['value'];
if(($value['field_id'] == $ids[1] && $value['value'] == "No")){
print_r($collection->getData()[$key]);
unset($collection->getData()[$key]);
unset($collection->getData()[$key-1]);
}
}
You can create a new Multidimensional array , and copy only the values you want to it .
also , when you use break , you get out of the foreach loop completely , i don't think this is the intended behavior , use continue to skip current iteration and test the next element
Is the function getData() returning a copy of the array or a reference? If is is only a copy the unset($collection->getData()[$key]) won't work.
Better you loop over the array as you do, but store all keys you want to delete in side a other array (e.g. $keysToRemove). When the for-each-loop is finished you start to loop the new array $keysToRemove containing all the keys you want to remove and remove then from array.
$keysToRemove = array();
$data = $collection->getData(); // copy or reference?
foreach ($data as $key => $value) {
if(($value['field_id'] == $ids[0] && $value['value'] == "No")){
echo $value['id'].'<br>';
break; // exit the loop? If you want to jump to the next element use "continue"
}
echo $value['field_id'].'<br>';
echo $value['value'].'<br>';
if(($value['field_id'] == $ids[1] && $value['value'] == "No")){
$keysToRemove[$key] = true;
$keysToRemove[$key-1] = true;
}
}
foreach(array_keys($keysToRemove) as $key){
echo '<p>remove key '.$key.' with content <pre>'.print_r($data[$key], true).'</pre></p>';
unset($data[$key]);
}
echo '<p>Result array is<pre>'.print_r($data, true).'</pre></p>';
So you don't manipulate the array your loop use at the moment.
Based on the comment that mentioned its a Magento Collection. I think, You could filter the ids in collection itself so that those are not fetched as part of the collection you want. Like:
$collection = Mage::getModel('your/model')->getCollection();
$collection->addAttributeToFilter('field_id', array('nin' => array(234, 237)));
OR, following if your model is non-eav entity model
$collection = Mage::getModel('your/model')->getCollection();
$collection->addFieldToFilter('field_id', array('nin' => array(234, 237)));
try this code which remove all array value which value is No
$cart = array(
0 => array
(
'id' => 11109,
'value' => 'Yes',
'field_id' => 234
),
1 => array
(
'id' => 11109,
'value' => 'Yes',
'field_id' => 123
),
2 => array
(
'id' => 11110,
'value' => 'No',
'field_id' => 234
),
3 => array
(
'id' => 11110,
'value' => 'No',
'field_id' => 237
),
4 => array
(
'id' => 11111,
'value' => 'No',
'field_id' => 237
),
);
$found = array();
$i = 0;
foreach ($cart as $key => $data) {
if ($data['field_id'] == 234 && $data['value'] == 'No') {
$found[$i]['id'] = $data['id'];
$found[$i]['value'] = $data['value'];
$found[$i]['field_id'] = $data['field_id'];
$i++;
}
}
echo "<pre>";
print_r($found);
then Output Is:
Array
(
[0] => Array
(
[id] => 11111
[value] => No
[field_id] => 234
)
)
I need to display a certain object from an array before showing the rest of the array.
The array looks like this:
Array
(
[0] => stdClass Object
(
[template_id] => 91
[template_name] => Alphabet
[template_thumbnail] => blank-template-thumbnail.jpg
[template_create_date] => 1456821665
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => simple
[sort] => 2
)
[1] => stdClass Object
(
[template_id] => 92
[template_name] => Blank Template
[template_thumbnail] => blank-template-thumbnail.jpg
[template_create_date] => 1456821670
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => simple
[sort] => 2
)
[2] => stdClass Object
(
[template_id] => 31
[template_name] => Holiday Specials
[template_thumbnail] => accommodation-1-20110926.jpg
[template_create_date] => 1456821660
[template_customer_id] => 0
[template_is_responsive] => no
[template_type] => builder
[template_category] => Accommodation
[sort] => 3
)
)
I need to show Blank Template first and then show the rest alphabetically (the order it is in now.
Is there a more elegant solution than looping through the array twice? The size of the array can be anything from 1 (the blank template) to countless objects.
$str="";
for($i=0;$i<=count($arr);$i++){
if($arr[$i]['template_name'] == "Blank Template"){
echo $arr[$i]['template_name'];
}else{
$str .= $arr[$i]['template_name']. "<br>";
}
}
echo $str;
Try this:
$firstItem = null;
$outArray = [];
foreach($yourArray as $item){
if($item->template_name == 'Blank Template'){
$firstItem = $item;
}else{
$outArray[$item->template_name] = $item;
}
}
ksort($outArray,SORT_STRING);
array_unshift($outArray,$firstItem);
Just one loop. Pay attention that this way of doing things, just work if you have uniqueness on the template_name!
Hope it helps.
This will work for you, try
<?php
$dataArray = array(0=>array('template_id'=>91,'template_name'=>'Alphabet'),
1=>array('template_id'=>92,'template_name'=>'Blank Template'),
2=>array('template_id'=>31,'template_name'=>'Holiday Specials')
);
$newArray = array();
foreach($dataArray as $key => $val)
{
if(in_array('Blank Template',$val))///find the key for black template
{
unset($dataArray[$key]); ///unset black temp from original array
$newArray[] = $val;///push black temp into new array at 0 index
foreach($dataArray as $k => $v)
{
$newArray[] = $v; ///push the renaming values into new array
}
}
}
echo "<pre>"; print_r($newArray);
?>
This will give you :
Array
(
[0] => Array
(
[template_id] => 92
[template_name] => Blank Template
)
[1] => Array
(
[template_id] => 91
[template_name] => Alphabet
)
[2] => Array
(
[template_id] => 31
[template_name] => Holiday Specials
)
)
LIVE EXAMPLE : CLICK HERE
How can I loop through an array like this and retrieve the id and echo it to the screen? Also how can I do a loop and find the one with the highest id?
Array
(
[articles] => Array
(
[0] => Array
(
[id] => 650
)
[1] => Array
(
[id] => 649
)
[2] => Array
(
[id] => 645
)
[3] => Array
(
[id] => 399
)
);
You can do this with foreach
foreach ($array['articles'] as $value)
{
echo "Id is: ".$value['id'];
}
And you can get with max() function:
foreach($array['articles'] as $article)
{
$ids[] = $article['id'];
}
echo "Max Id is: ".max($ids);
Or you can do get value and max id with one foreach.
foreach($array['articles'] as $article)
{
echo "Id is: ".$article['id'];
$ids[] = $article['id'];
}
echo "Max Id is: ".max($ids);
Say $arr['articles'] contains your array.Then using a foreach you can loop through the array and just echo it.
$arr = array('articles' => array(
'0' => array('id' => 650),
'1' => array('id' => 649),
'2' => array('id' => 645),
'3' => array('id' => 399)
)
);
foreach($arr['articles'] as $val){
echo $val['id'].'</br>';
}
Try
foreach ($arrayvar['articles'] as $value)
{
echo $value['id']."<br>";
}