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>";
}
Related
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 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";
}
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 have an array like so...
$myarray = Array (
[docs] => Array(
[0] => Array ([property_imgurl] => http://www.example.com/image1.jpg)
[1] => Array ([property_imgurl] => http://www.example.com/image2.jpg)
[2] => Array ( [property_imgurl] => http://www.example.com/image3.jpg)
[3] => Array ( [property_imgurl] => http://www.example.com/image4.jpg)
)
);
I am trying to echo out
foreach ($myarray as $myarrays) {
echo $myarray[property_imgurl];
}
But this isn't returning any results, what am I doing wrong?
Your key is invalid..
foreach ($myarray["docs"] as $myarrays) {
echo $myarrays["property_imgurl"];
}
Live preview
you need to add one more loop try
foreach ($myarray as $v) {
foreach ($v as $v1) {
echo $v1['property_imgurl'];
}
}
Your Array seems to be wrong here..
Try this:
$myarray =
Array (
"docs"=>
Array(
"0" => Array ( "property_imgurl" => "http://www.example.com/image1.jpg" ),
"1" => Array ( "property_imgurl" => "http://www.example.com/image2.jpg" ) ,
"2" => Array ( "property_imgurl" => "http://www.example.com/image3.jpg" ) ,
"3" => Array ( "property_imgurl" => "http://www.example.com/image4.jpg" ) )
);
And then iterate your loop like this:
foreach($myarray['docs'] as $key=>$value)
{
echo $value['property_imgurl'];
}
Here is the output of some complex multidimensional array:
Array (
[0] => Array (
[#attributes] => Array ( [ID] => 114037469 [Name] => MJBDESIGN.ORG [Traffic] => 4 [BidCount] => 0 [Price] => $8 USD [ValuationPrice] => - [TimeLeft] => 3H 27M [RowID] => 1 )
)
[1] => Array (
[#attributes] => Array ( [ID] => 114136929 [Name] => TRAININGPROGRAMMEDESIGN.COM [Traffic] => 11 [BidCount] => 0 [Price] => $8 USD [ValuationPrice] => $3 USD [TimeLeft] => 3H 27M [RowID] => 2 )
)
)
How to iterate through each item and get the 'ID', 'Name' and etc of individual item? Thanks
This array is not so much complex.
$simpleArray = __YOUR_COMPLEX_ARRAY__;
foreach ($simpleArray as $array) {
$id = $array['#attributes']['ID'];
$name = $array['#attributes']['Name'];
}
foreach ($array as $r) {
$name = $r['#attributes']['Name'];
$id = $r['#attributes']['ID'];
# code ....
}
If you would like all attributes of each row:
foreach ($array as $n=>$r){
echo "Row {$n}\n";
foreach ($r as $k=>$v){
// Echo the data
echo "{$k}=>${v}\n";
}
}
Just easy way:
$array = // your long array;
foreach($array as $row)
{
$id = $row['#attributes']['Id'];
$name = $row['attributes']['Name'];
// do something with these variables
}
Try it like,
foreach ($yourarray as $val) {
echo $id = $val['#attributes']['ID'];
echo $name = $val['#attributes']['Name'];
}