I have the following structure (i use json_decode to loop through it on PHP):
{"721":{"forms":0},"722":{"forms":"[82]","ope":"48723","ini":"01/03/2015 00:00","fin":"01/03/2015 00:20"},"723":{"forms":0},"724":{"forms":0},"725":{"forms":0},"726":{"forms":0},"727":{"forms":0},"729":{"forms":0},"730":{"forms":0},"731":{"forms":0}}
How can I access the property "forms"?
Use a foreach loop to go through every key/value pair in your array, then just access the forms property of the value array.
$a = json_decode($stuff, true); // where $stuff is that JSON string
foreach ($a as $key => $value) {
print $value['forms']."<br />"
}
Related
I get my data from the database, but want to add 2 keys to them. So I add them in a for loop. If I dump (simple function that prints the array with pre tags) the single result in the for loop, it's correct, when I dump the 2dimensional array outside of it, it doesn't have the keys anymore..
For some reason it doesn't push it to the 2dimensional array?
$results is a 2dimensional array btw.
//add amount and subtotal to the array's elements
foreach ($results as $result) {
$result['amount'] = $sessionShoppingCart[$result['artikelnummer']][1];
$result['subtotal'] = $result['amount'] * $result['Verkoopprijs'];
$this->dump($result);
}
$this->dump($results);
To change an array within the foreach you can do two things.
Reference the array value with &:
foreach ($results as &$result) {
Or use the key and modify the array:
foreach ($results as $key => $result) {
$results[$key]['amount'] = $sessionShoppingCart[$result['artikelnummer']][1];
$results[$key]['subtotal'] = $result['amount'] * $result['Verkoopprijs'];
}
I'm trying to unset() some elements from an array but when using a foreach loop to go through 1 array to delete these elements from another array it does not seems to be working.
if (isset($this->request->post['merge'])) {
$merge_orders = $this->request->post['merge'];
}
$selected_order = min($merge_orders); // Fetch the max value order_id
unset($merge_orders[$selected_order]); // Take it out of the array.
$orders_list = explode(',', $this->request->post['order_id_list']);
$removeKeys = $merge_orders;
foreach($removeKeys as $key) {
unset($orders_list[$key]);
echo $key;
}
echo print_r($orders_list);
The first unset works fine but the second does not, the array is set and properly formatted but it still does not seem to remove the elements from the $orders_list array.
If you only use one parameter on a foreach loop you are delivered the value of the occurance and not the key for the occurance.
Try this so that you are getting a key from the foreach loop and not a value
foreach($removeKeys as $key => $val) {
unset($orders_list[$key]);
echo $key;
}
While I map all the links on a page that is contained in an array, I want to check if each of the links is inserted in this array and, if not, insert it.
I'm trying to use the code bellow without success because "foreach $arr" doesn't pass by in the new values.
include_once('simple_html_dom/simple_html_dom.php');
$arr = array('http://www.domain.com');
foreach ($arr as $key => &$item) {
$html = file_get_html($item);
// Find category links
foreach($html->find('a[href^=http://www.domain.com/dep/]') as $element) {
if (!in_array($element->href, $arr))
$arr[] = $element->href;
}
}
print_r($arr);
Important: I need to search and add value in the original array, not in the copy (foreach).
First of all
In foreach ($arr as $key => &$item) { every $item is a STRING. (As a warning told you). So you shouldn't use $item[] here.
Next pitfall: if you want to add new items to your $arr array symtax should be
$arr[] = $some_var;
But you shouldn't do this because every time you add items to $arr, this array increases and you iterate not over two elements array, but for example 3-elements or 4 elements. Do you expect this?
You should find new values, put them in some other array and then merge both arrays.
Or use #splash58 solution. It's even simplier.
Hello I can't figure out how to loop through this json encoded array and for each object, get all its values. I need each value as a variable for itself.
echo json_encode($formulars);
This is what i get when i echo it out
[{"project_name":"polle","type":"support","title":"vi","reason":"prover","solution":"igen","comments":"okay ","date_stamp":"2013-08-20 14:06:37","used_time":132},{"project_name":"dolla","type":"support","title":"lolol","reason":"skl","solution":"dskal","comments":"kflafda ","date_stamp":"2013-08-20 14:11:36","used_time":210},{"project_name":"polle","type":"fejl","title":"lol","reason":"aksdl","solution":"fdjks","comments":"djsks ","date_stamp":"2013-08-20 14:13:27","used_time":1230}]
I have tried this piece of code and I managed to get out the project_name from the first object and that's it:
foreach ($formulars as $current => $project_name) {
$project_name['project_name'];
}
So is there any way i can get all the variables for each object in my array instead of just the project_name?
Like this:
foreach ($formulars as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
Thanks in advance
Seems like you have an objects inside an array. So you will need to loop through the array and get each object. Just JSON_DECODE your encoded string like below.
Perhaps:
$data = json_decode($formulars,true);
/* Since it's only one object inside the array, you could just select element zero, but I wil loop*/
//You should now be able to do this
foreach ($data as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
The reason I loop is because there is a object inside an array(Javascript way I think).
Use json_decode to convert the json object to an array; then use foreach to loop through the array. That should work.
<?php
$arr_json = json_decode($formulars);
foreach($arr_json as $key => $value)
//Code to perform required actions
?>
This should give you some ideas.
Use json_decode (with TRUE for getting an associative array)to convert your JSON object to an associative array. After that, you can use a foreach loop to traverse through your multidimensional array and print the required values.
Code:
$json = json_decode($string, true);
foreach ($json as $key => $value) {
foreach($value as $key2 => $value2) {
echo $value2."\n";
}
}
Working Demo!
I have the following array that is in PHP (this is a print_r() output).
I'm currently struggling to loop through this data - need to be able to process each part and access the values in each array item. How can I do this.
I've tried the following unsuccessfully...
foreach (array as $key => $value) {
echo $key;
}
Try this. Since you have an array of objects, you should be able to access each object property using ->
foreach($array as $value) {
echo $value -> userid;
}
It should echo out all the user id in that array of objects
You have an array of objects, so try something like this:
<?php
foreach ($array as $value) {
echo $value->userid;
echo $value->action;
echo $value->photo_name;
}
You don't need the $key since you're not using it in the loop. Each iteration will put the object in the $value variable, on which you can access it's properties.