I have an array in for format as below (consol.log in browser). How would I iterate the array in php?
stdClass Object
(
[SIEcat7] => stdClass Object
(
[text] => test1
[amount] => 1 000.00
)
[SIEcat8] => stdClass Object
(
[text] => test2
[amount] => 0.00
)
)
You can try below.
<?php
foreach ($array as $key => $arr){
echo 'Key: ' , $key, '<br>'; #This will print the keys SIEcat7, SIEcat8
echo 'Text: ', $arr->text, '<br>';
echo 'amount: ', $arr->amount, '<br> -------- <br>';
}
?>
Got it now
foreach($data as $i => $item) {
}
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 want to retrieve a single value from this php array.
My array in PHP is:
Array
(
[0] => stdClass Object
(
[id] => 27
[id_customer] => 19
[my_cart] => Array
(
[c81e728d9d4c2f636f067f89cc14862c] => Array
(
[id] => 2
[qty] => 1
[price] => 39000
[name] => HEBERGEMENT WEB MUTUALISE PREMIUM
[rowid] => c81e728d9d4c2f636f067f89cc14862c
[subtotal] => 39000
)
[a87ff679a2f3e71d9181a67b7542122c] => Array
(
[id] => 4
[qty] => 1
[price] => 150000
[name] => HEBERGEMENT WEB MUTUALISE ULTIMATE
[rowid] => a87ff679a2f3e71d9181a67b7542122c
[subtotal] => 150000
)
)
1
[created_at] => 2020-03-30
)
)
The problem is that I can not get qty, name, price
I tried by doing this in my foreach
foreach($cart_data as $cd){
echo 'ID Table: '.$cd->id.'<br>';
echo 'ID customer: '.$cd->id_customer.'<br>';
$data = $cd->my_cart;
if(is_array($data)){
foreach($data as $s){
echo 'My cart: '.$s;
}
}
}
But nothing is happened! I want to get price, name and qty.
In this line:
echo 'My cart: '.$s;
The variable $s holds an array, not a string. If you follow your data structure, the contents of $s would be similar to this:
Array (
[id] => 4
[qty] => 1
[price] => 150000
[name] => HEBERGEMENT WEB MUTUALISE ULTIMATE
[rowid] => a87ff679a2f3e71d9181a67b7542122c
[subtotal] => 150000
)
So, in order to get name, qty, price, you'd need to change your inner loop to something like this:
foreach ($data as $s) {
echo 'Product name: ' . $s['name'] . '<br>';
echo 'Quantity: ' . $s['qty'] . '<br>';
echo 'Price: ' . $s['price'] . '<br>';
}
What happens when you try to use an array as a string is that it shows up as the string "Array" and will trigger a PHP Notice: Array to string conversion in [location]. I'm guessing this error might be the reason you get nothing, instead of My cart: Array.
To get several keys from my_cart array, try to write the loop like this :
foreach($cart_data as $cd){
echo 'ID Table: '.$cd->id.'<br>';
echo 'ID customer: '.$cd->id_customer.'<br>';
$data = $cd->my_cart;
if(is_array($data)){
$filtered = [];
$keys = ['qty', 'name', 'price']; // the keys
$column_keys = array_flip($keys); // getting keys as values
foreach ($data as $key => $val) {
// getting only those key value pairs, which matches $column_keys
$item = array_intersect_key($val, $column_keys);
if (!empty($item))
$filtered[$key] = $item;
}
echo 'My cart: <br/>';
print_r($filtered);
}
}
I found the solution. When inserting into database I encode it like this htmlspecialchars(json_encode($data)) and decode in with json_decode while getting the $data. So now I have this:
foreach($cart_data as $cd){
echo 'ID Table: '.$cd->id.'<br>';
echo 'ID customer: '.$cd->id_customer.'<br>';
$data = json_decode($cd->my_cart);
foreach($data as $row){
echo $row->name;
echo $row->qty;
echo $row->price;
}
}
I have two arrays:
First:
Array
(
[0] => Catalog No.:
[1] => H-B No.
)
Second array:
Array
(
[0] => Array
(
[0] => GG
[1] => 692
)
[1] => Array
(
[0] => VV
[1] => 693
)
)
I want o to merge them into one array to get this:
Array
(
[0] => Array
(
[0] => Catalog No.: GG
[1] => H-B No. 692
)
[1] => Array
(
[0] => Catalog No.: VV
[1] => H-B No. 693
)
)
I tried with array merge, but that works with keys and values, I want to merge only values from this two arrays into one, any help?
<?php
$first = ['Catalog No.:', 'H-B No.'];
$second = [['GG', 692],['VV', 693]];
$result = [];
foreach ($second as $key => $values) {
foreach ($values as $number => $value) {
if (!isset($first[$number])) {
continue;
}
$result[$key][] = $first[$number] . ' ' . $value;
}
}
var_dump($result);
Another way could be using array_map and prepend the values from $first to the current $arr
$result = array_map(function($arr) use ($first){
foreach($first as $key => $value) {
$arr[$key] = $value . ' ' . $arr[$key];
}
return $arr;
}, $second);
Php demo
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>";
}
}
$classconducted = json_encode(array('segment'=>$segment,'Board'=>$board,'classFiveSubject'=>$subject5,'classeightboard'=>$eightboard,'classeightsubject'=>$subject8,'classTenthboard'=>$tenthboard,'classTenthsubject'=>$subject8,'engineering'=>$engineering));
$in = '{"segment":["Class I-V Tuition","Class VI-VIII Tuition","Class IX-X Tuition"],"Board":["cbse","cse/Ise","State"],"classFiveSubject":["allsubject","science"],"classeightboard":["cbse","cse/Ise"],"classeightsubject":null,"classTenthboard":["cbse","cse/Ise"],"classTenthsubject":null,"engineering":null}';
print_r( json_decode($in) );
myoutput:
stdClass Object
(
[segment] => Array
(
[0] => Class I-V Tuition
[1] => Class VI-VIII Tuition
[2] => Class IX-X Tuition
)
[Board] => Array
(
[0] => cbse
[1] => cse/Ise
[2] => State
)
[classFiveSubject] => Array
(
[0] => allsubject
[1] => science
)
[classeightboard] => Array
(
[0] => cbse
[1] => cse/Ise
)
[classeightsubject] =>
[classTenthboard] => Array
(
[0] => cbse
[1] => cse/Ise
)
[classTenthsubject] =>
[engineering] =>
)
I have used the following code to retrieve the data. but I could not workout it.can any one guide me how to solve this?
<?php
$sql=mysql_query("select * from tinfo");
while($row=mysql_fetch_array($sql))
{
$in=$row['classconducted'];
}
echo "<pre>";
$value=json_decode($in, true);//echo count($in); exit;
foreach ($value as $k => $val)
{
echo "$k | $val <br />";
}
?>
The output is:Here it is listing the indexing key and the value is not coming how to user the foreach here.I want to retrieve the key and value.
segment | Array
Board | Array
classFiveSubject | Array
classeightboard | Array
classeightsubject | Array
classTenthboard |
classTenthsubject | Array
engineering | Array
I want the output form my json_decode like this:
segment:Class I-V Tuition
Board:CBSE,STATE
classFiveSubject:AllSubject,Maths,Science
segment:Class VI-VIII Tuition
Board:CBSE, STATE
classEightSUbject:AllSubject,Maths,Science
can anyone give me the propercode? what are the ways to list the json_decode data.
Untested, but try:
foreach (json_decode($in, true) as $key => $val) {
echo $key.': ';
if (is_array($val))
echo implode(',', $val) . "\n";
else
echo $val . "\n";
}
define('BR', '<br />');
$json = json_decode($in, true);
foreach ($json as $key => $arr ) {
echo $key.BR;
foreach( $arr as $value ) {
echo $value.BR;
}
}