sorry if the questions seams stupid but I have created an array in my controller and I pass it to the view with the $data[] array.
And I don't get it right to print the array with a for loop.
Can you please help me?
$measurearray = array(
'nr' => $answerNr,
'responsible' => $responsible,
'creationdate' => $InvDate2,
'activity' => $reason
);
$arr[$i] = $measurearray;
$data["failedMeasures_nr"] = $arr;
Output :
Array (
[50] => Array (
[0] => Array (
[nr] => 5d
[responsible] => werner
[creationdate] => asdfgdf
[activity] => appointed.
)
)
[73] => Array (
[0] => Array (
[nr] => 9g
[responsible] => 42887
[creationdate] => Zuzana
[activity] => r the training.
)
)
)
This is what happens when I print_r() it in the view.
My target is to print every single element for itself!
Have you tried this?
foreach ( $failedMeasures_nr[0] as $key => $val ) {
echo $key . ' is ' . $val;
}
With a foreach you can print separately the array elements
Please note that if you want to print more than the first index ( the 0 in my example ) you should use another foreach, resulting in something like this:
foreach ( $failedMeasures_nr as $new_arr ) {
foreach ( $new_arr as $key => $val ) {
echo $key . ' is ' . $val;
}
}
this way you will print the whole array separately
You can use foreach method to print each value.
<?php
foreach ($failedMeasures_nr as $print):
echo $print['nr'];
echo $print['responsible'];
echo $print['creationdate'];
echo $print['activity'];
endforeach;
?>
// controller page try this,
$data[$i]["failedMeasures_nr"] =array('nr' => $answerNr,
'responsible' => $responsible,
'creationdate' => $InvDate2,
'activity' => $reason);
$this->load->view('view_page',$data);
In View Page try this code,
for($i =0 ; $i <count($data);$i++){
foreach ($data[$i] as $key => $value) {
echo "$value";
echo "<br>";
}
}
Related
Hey guys i am new of the laravel, how can i parse this output array? This is my array is coming from repeater using jquery.
Array
(
[tour_baslik] => 1. Day
[tour_icerik] => content here....
[lunch] => Array
(
[0] => 2
)
[dinner] => Array
(
[0] => 1
[1] => 2
)
)
Array
(
[tour_baslik] => 2.Day
[tour_icerik] => content 2 here...
[lunch] => Array
(
[0] => 1
[1] => 2
)
[dinner] => Array
(
[0] => 2
)
)
I need parse like that but i'm stuck:
foreach($myarray as $key => $data){
echo $key . '-' . $data; }
Output must be:
tour_baslik - 1.day
tour_icerik - content here..
lunch - 2
dinner - 1,2
If you need to iterate through all items of your input, you could use a recursive function like the following:
function iterator($key, $value) {
if (is_array($value)) {
foreach ($value as $key => $value) {
iterator($key, $value);
}
} else {
echo !empty($key) ? "$key - " : "";
echo $value."\n";
}
}
iterator(null, $input);
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'];
}
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>";
}
I am fetch facebook user's working history, and when I print_r, I get an array like this:
Array (
[work] => Array (
[0] => Array (
[employer] => Array (
[id] => 111178415566505
[name] => Liputan 6 SCTV
)
) [1] => Array (
[employer] => Array (
[id] => 107900732566334
[name] => SCTV
)
)
)
[id] => 502163984
)
How do I display only the value from name, so the output will be like this:
Liputan 6 SCTV
SCTV
I used foreach, but always an error always happens.
Try this:
foreach ($array['work'] as $arr) {
echo $arr['employer']['name']."<br>\n";
}
This is assuming your data looks like:
$array = array(
'work' => array(
array(
'employer' => array('id' => 111178415566505, 'name' => 'Liputan 6 SCTV'),
),
array(
'employer' => array('id' => 107900732566334, 'name' => 'SCTV'),
),
),
'id' => 502163984,
);
for instance your array variable name is $test, then you can get name value by
$test['work'][0]['employer']['name']
you can check your array structure using pre tag, like
echo '<pre>';print_r($test);
You can use array_reduce, implode, and closure (PHP 5.3+) to do this.
echo implode("<br/>", array_reduce($array["work"],
function(&$arr, $v){
$arr[] = $v["employer"]["name"];
},array()
));
I assume $arr is working history array. You can use for Then array look like this :
for($i=0, $records = count( $arr['work']); $i < $records; $i++) {
echo $arr['work'][$i]['employer']['name'] ."<br>";
}
Using foreach
foreach( $arr['work'] as $works) {
echo $works['employer']['name'] ."<br>";
}
foreach ($your_array as $your_array_item)
{
echo $your_array_item['work'][0]['employer']['name'] . '<br>';
}
$count=count($array);
for($i=0,$i<=$count;$i++){
echo $array['work'][$i]['employer']['name'];
}
This will work.. Dynamically..
foreach ($array['work'] as $val_arr) {
echo $val_arr['employer']['name']."<br />";
}
I'm trying to concatenate an array, but when PHP comes across an empty array-item, it stops concatenating.
My array looks like this:
Array
(
[0] => Array
(
[0] => Test1
[1] => Test1
[2] =>
)
[1] => Array
(
[0] => Test2
[1] => Test2
[2] =>
)
[2] => Array
(
[0] => Test3
[1] => Test3
[2] => Test3
)
)
The 3th item on the first 2 Array-items are empty. And when I loop over them like this:
$keys = array('uid', 'type', 'some_column', 'other_column');
foreach ($csv as $i => $row) {
$uid = $row[0] . $row[1] . $row[2];
array_unshift($row, $uid);
$csv[$i] = array_combine($keys, $row);
}
I only get Test3Test3Test3 back, instead of the expected
Test1Test1
Test2Test2
Test3Test3Test3
So it looks like PHP is skipping items when concatenating an empty value.
Is this normal PHP behavior? And if so, how can I tackle this?
Try like
$uid = array();
foreach ($array as $i => $row) {
$uid[] = $row[0] . $row[1] . $row[2];
}
var_dump($uid);
Just you are giving $uid and it is taking it as an type variable and it appends the last occurance of loop into that variable.If you want your desired output you need to declare it as an array before.
I'm sorry, but if that is your desired output, you're overcomplicating things:
$foo = array(
array("Test1","Test1"),
array("Test2","Test2"),
array("Test3","Test3","Test3")
);
echo implode(PHP_EOL,
//implode all child arrays, by mapping, passes no delimiter
//behaves as concatenation
array_map('implode',$foo)
);
Returns:
Test1Test1
Test2Test2
Test3Test3Test3
In your case, you can use bits of this code like so:
$csv = array(array("Test1","Test1",''),array("Test2","Test2",''),array("Test3","Test3","Test3"));
$keys = array('uid', 'type', 'some_column', 'other_column');
foreach($csv as $k => $row)
{
array_unshift($row,implode('',$row));
$csv[$k] = array_combine($keys,$row);
}
gives:
Array
(
[0] => Array
(
[uid] => Test1Test1
[type] => Test1
[some_column] => Test1
[other_column] =>
)
[1] => Array
(
[uid] => Test2Test2
[type] => Test2
[some_column] => Test2
[other_column] =>
)
[2] => Array
(
[uid] => Test3Test3Test3
[type] => Test3
[some_column] => Test3
[other_column] => Test3
)
)
Try this:
$uid = array();
foreach ($array as $i => $row) {
$uid[] = $row[0] . $row[1] . $row[2];
}
var_dump($uid);
This outputs:
Array
(
[0] => Test1Test1
[1] => Test2Test2
[2] => Test3Test3Test3
)
You can do something similar to produce a string:
$uid = '';
foreach ($arr as $i => $row) {
$uid .= $row[0] . $row[1] . $row[2] . "\n";
}
echo $uid;
Output:
Test1Test1
Test2Test2
Test3Test3Test3
To get the expected output
$uid = "";
foreach ($array as $i => $row) {
$uid .= $row[0] . $row[1] . $row[2] . "\n";
}
echo $uid;
You need to use two foreach loop..
Procedure:
Initialize your first foreach loop to get the key => value as $key=> $value of each array inside.
Then initialize your second loop to $value variable since value inside of this is another array key => value = $innerKey => $innerValue.
Then inside your second loop echo $innerValue to display values inside of the secondary array
Lastly put an echo '<br/>' inside your first loop to put each secondary array into another newline.`
.
$data = array(
0 => array(
0 => 'Test1',
1 => 'Test1',
2 => ''
),
1 => array(
0 => 'Test2',
1 => 'Test2',
2 => ''
),
2 => array(
0 => 'Test3',
1 => 'Test3',
2 => 'Test3'
)
);
foreach($data as $key => $value){
foreach($value as $innerKey => $innerValue){
echo $innerValue;
}
echo '<br/>';
}
// Output would be..
Test1Test1
Test2Test2
Test3Test3Test3
Code Tested at : PHP Fiddle