print multidimensional array with foreach loop 3 - php

msgs is a 2 dimension array
Is it possible to do:
foreach ($msgs['error'] as $msg)
?
I want to print only the value in "error":
msgs['error']['first value']
msgs['error']['second value']
msgs['error']['third value']
etc...

Just like this:
foreach ($msgs as $msg_outer)
print_r($msg_outer);
PHP will loop through the outer values. If you want to access the inner values, just add another foreach in the foreach:
foreach ($msgs as $msg_outer)
foreach ($msg_outer as $msg_inner)
print_r($msg_inner);

foreach ($msgs['error'] as $msg)
{
print $msg;
}

Check the array keys if you're having trouble accessing them with foreach();
print_r(array_keys($msgs));
Then work forwards from there.
Src: http://www.php.net/manual/en/function.array-keys.php

Related

Displaying data from a CSV file with multi-dimensional PHP array

I'm trying to pull data from a CSV file that contains vehicle make, model, mileage etc...
Using this example from php -
<?php
$csv = array_map('str_getcsv', file('csv/csvin.csv'));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv);
foreach($csv as $car){
foreach($car as $key=>$value){
echo "<div id='car'>".$key.":".$value."</div></br>";
}
}
?>
This is the array I get -
BodyStyle:"Station Wagon"
"DaysInStock":"27"
"Make":"Toyota"
"Model":"Prius v"
"MSRP":"0"
"SellingPrice":"26995"
"StockNumber":"387515"
"Trim":"Three"
"VIN":"JTDZN3EU9E3306528"
"Year" :"2014"
Now when I attempt to manipulate it or pull any individual values I simply cannot. How would I go about displaying this information with HTML tags for each value?
I have tried this:
print_r ($csv[0]['Make'];
echo $csv[0]['Make'];
Just to try and display a value but still nothing. I noticed for some reason the "BodyStyle" doesn't contain quotes like the rest so something definitely seems fishy.
From here how would I strip the quotes and break out each value?
This is the error being thrown -
Invalid argument supplied for foreach() in
Thanks in advance!
foreach($csv as $car){
echo "<tr><td>Make:</td><td>".$car['Make']."</td></tr>";
}
alternatively:
foreach($csv as $car){
foreach($car as $key=>$value){
echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
}
}
alternatively:
echo $csv[0]['Make'];
Try this one :
print_r ($csv[0]['Make']);
Basically the $csv variable is an array, to get its values you have to use the loop function, something like :
foreach ($csv as $data) {
foreach ($data as $index => $value) {
if ($index == "make") {
echo $value;
}
}
}
If you notice it a bit, the outer array is called indexed array (array with indexes) and to traverse the values use foreach ($csv as $data), while the inner array is called associative array (this array does not use number as index, but a name), and to traverse it use foreach ($data as $index => $value).
Try it and you'll see :)
PS: Sorry I didn't notice the inner array, for this case Richard's answer is the correct one. I have added a credit to his answer for giving a correct answer.
So I figured out that I only need 1 foreach loop like so -
foreach($csv as $car){
$type = $car[0];
$echo $type;
}
Works now and thanks all for the help!

can we skip/jump in array while displaying PHP

I have a multidimensional array returned by drupal render function in that array i want to choose some last values
for example
$array['static_name'][number]['changes_every_time']['some_name']['value_i_need'];
is there ant way we can skip the "changes_every_time" level while printing array
is there a way we can use wild character in there
like if i want to print
echo $array['static_name'][number][*]['some_name'][value_i_need];
some thing like this
Store answer "no".
But what you can do is do a foreach over the keys of $array['static_name'][number]
foreach($array['static_name'][number] as $val) {
echo $val['some_name'][value_i_need];
}
This way you don't have to care.
well, you cold iterate the array at that "level" with a foreach loop
Untested code:
foreach ($array['static_name'][number] as $key => $value) {
echo $value['some_name'][value_i_need];
}

Can't access original array in foreach loop

I have a problem with my foreach loop. My foreach loop basically looks like this:
echo $values[0];
echo $values[1];
foreach ($values as $key => $value)
{
echo $values[0];
echo $values[1];
}
$values[0] should be "new york city" and $values[1] should be "new york". The problem is that in the foreach loop, both echos give the same value, whereas outside the loop they give the different (correct) values.
How do I access the original array ($values) normally inside of the foreach loop?
EDIT: I don't want to access the value $value. I want to be able to access any index of $values regardless of which iteration my foreach loop is on. I hope I am making sense.
Also, obviously I have alot more going on in this foreach loop, it was just meant as an example. The purpose of the foreach loop is not to print out these values.
Basically what the actual foreach loop I am using does is that it iterate through an array, and when it finds a certain value in that array, it iterates through the whole array again (inside the foreach loop). So basically I want a foreach loop inside a foreach loop, both for the same array, but it doesn't seem to work.
Try to use $val instead of $values[$i];
// Code goes here
foreach ($values as $key => $value)
{
echo $value;
}

Getting the name of an array dynamically

I have an array which looks like this:
$example = [
['rendered'][0]['rendereditem1']
['rendered'][4]['rendereditem2 and more']
['rendered'][2]['rendereditem3']
]
Now I want to iterate with foreach to get the contents of 0,4,2!
Normally I would write:
foreach($example as $value){
print $value['rendered'][int which is the same everywhere];
}
But that is obviously not working because the array name is always different...how could I iterate in this case?
Simply add a second loop to iterate over the members :
foreach($example as $value) {
foreach($value['rendered'] as $key=>$item) {
// Do what you want here, $key is 0,4,2 in your example
}
}

How to get an array repeat in foreach loop

This is my $settings array.... and I would like to look this array in foreach so that I can read the $settings['node']['type'][$value['type']]['modifiers'] values in foreach loop and print them.
I'm can only guess you want to iterate over each element in your array.
foreach($settings as $node=>$types){
foreach($types as $values=>$modifiers){
\\ etc etc etc
echo "Settings: $node $types $values $modifiers";
}
}
Just keep nesting the foreach loops for each element you want to iterate.

Categories