This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed last year.
I know this question is very basic to you all, and I'm truly sorry for asking, but I'm studying arrays right now and I've been asked to do the following learning from textbook, which I hate.
I need to loop through the first level and get the FIELD_NAME and FIELD_LABEL.
Then to loop through again and get all CUSTOM_FIELD_OPTIONS > OPTION_VALUE for each first level array.
My problem is that it fails at the first loop. And I can't get my head around second loops.
Can you provide some guidance please? I need some help on this :(
$getAllCustomFields = $api->getAllCustomFields();
foreach ($getAllCustomFields as $item) {
echo addslashes($item->FIELD_NAME) . ": '" . addslashes($item->FIELD_LABEL) . "',";
}
In the array I need to be able to loop and get
array(46) {
["Secondary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(25) "Secondary_Volunteering__c"
["FIELD_ORDER"]=>
int(2)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(22) "Secondary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(9) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(3)
["OPTION_VALUE"]=>
string(6) "Events"
["OPTION_DEFAULT"]=>
bool(false)
}
[1]=>
array(3) {
["OPTION_ID"]=>
int(14)
["OPTION_VALUE"]=>
string(28) "Flagship Programme"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
["Primary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(23) "Primary_Volunteering__c"
["FIELD_ORDER"]=>
int(3)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(20) "Primary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(27) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(87)
["OPTION_VALUE"]=>
string(9) "Animal Me"
["OPTION_DEFAULT"]=>
bool(false)
}
[1]=>
array(3) {
["OPTION_ID"]=>
int(51)
["OPTION_VALUE"]=>
string(18) "Animals inc Plants"
["OPTION_DEFAULT"]=>
bool(false)
}
[2]=>
array(3) {
["OPTION_ID"]=>
int(13)
["OPTION_VALUE"]=>
string(3) "Art"
["OPTION_DEFAULT"]=>
bool(false)
}
[3]=>
array(3) {
["OPTION_ID"]=>
int(21)
["OPTION_VALUE"]=>
string(12) "Bright Stars"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
}
foreach($getAllCustomFields as $fields){
foreach($fields as $field){
echo( '<strong>'.$field["FIELD_LABEL"].'</strong><br>');
foreach ($field["CUSTOM_FIELD_OPTIONS"] as $cfield){
echo($cfield["OPTION_VALUE"].'<br>');
}
echo('<br>');
}
}
What is the shortest way to change multiple values in array key that has a specific value in it?
For example, I have this array:
array(2) {
[0]=>
array(5) {
["state"]=>
string(6) "active"
["payer_mail"]=>
string(12) "mail#none.com"
["start"]=>
string(12) "06/05/2015"
["end"]=>
string(8) "08/07/2017"
["price"]=>
string(8) "45.00"
["keystring"]=>
string(8) "493457025"
}
[1]=>
array(6) {
["place"]=>
string(2) "47"
["state"]=>
string(8) "canceled"
["payer_mail"]=>
string(12) "mail#none.com"
["start"]=>
string(9) "20/8/2014"
["end"]=>
string(10) "20/10/2017"
["price"]=>
string(5) "95.00"
["keystring"]=>
string(8) "34879205"
}
}
And I want to change the "state" value of the key that has "34879205" value for his "keystring" sub-key.
You can use foreach():-
foreach($array as &$value) {
if ($value['keystring'] == '34879205'){
$value['state'] = "";//change value here
}
}
Output:- https://eval.in/838789
Another way i found is:
$key = array_search('34879205', array_column($array, 'keystring'));
$array[$key]['state'] = 'newvalue';
You can use array_map function
$result = array_map(function ($element) {
if ($element['keystring'] === '34879205') {
$element['state'] = 'new_state';
}
return $element;
}, $array);
I got this array
array(5) {
[0]=>
array(5) {
["id"]=>
int(1411667077)
["nachricht"]=>
string(13) "iiiiiiiiiiiii"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "25.09.2014 19:44"
["deleted"]=>
string(0) ""
}
[1]=>
array(5) {
["id"]=>
int(1411701734)
["nachricht"]=>
string(2) "dd"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[2]=>
array(5) {
["id"]=>
int(1411701737)
["nachricht"]=>
string(6) "swfsfs"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[3]=>
array(5) {
["id"]=>
int(1411701739)
["nachricht"]=>
string(7) "egwegeg"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 05:22"
["deleted"]=>
string(0) ""
}
[4]=>
array(5) {
["id"]=>
int(1411742201)
["nachricht"]=>
string(3) "sss"
["user"]=>
string(15) "334607943355808"
["datum"]=>
string(16) "26.09.2014 16:36"
["deleted"]=>
string(0) ""
}
}
I want to cut the where the id is 1411701737 so I tryed:
foreach($array as $arr => $sub_arr)
{
if $sub_arr['id'] == 1411701737
{
break;
}
}
I know I need to create a whole new array in the foreach, but isn't there maybe a build in function?
$arr_new = array(); //Define a new array
foreach($arr_old as $a) { //Loop through the old one
if($a['id'] === 1411701737) break; //If the value of id matches, leave the foreach loop
$arr_new[] = $a; //Else, copy this array to the new array
}
print_r($arr_new); //Print the new array
$new = array();
foreach($array as $k) {
if($k['id'] == 1411701737) break;
array_push($new, $k);
}
var_dump($new);
The answer by Mooseman is likely a faster way to do this since it does not rely on a PHP function call to place the array into the new array. However unless you are doing this on a set of arrays that is in the 100k+ it really won't matter.
Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>
I have this multidimensional array in PHP:
array(4) {
["took"]=> int(2)
["timed_out"]=> bool(false)
["_shards"]=> array(3) {
["total"]=> int(5)
["successful"]=> int(5)
["failed"]=> int(0)
}
["hits"]=> array(3) {
["total"]=> int(3)
["max_score"]=> float(2.3578677)
["hits"]=> array(1) {
[0]=> array(5) {
["_index"]=> string(13) "telephonebook"
["_type"]=> string(6) "person"
["_id"]=> string(22) "M5vJJZasTGG2L_RbCQZcKA"
["_score"]=> float(2.3578677)
["_source"]=> array(8) {
["Mob"]=> string(19) "XXX"
["Title"]=> string(13) "Analyst"
["Department"]=> string(8) "Analysis"
["Country"]=> string(6) "Sweden"
["Tlf"]=> string(0) ""
["Name"]=> string(16) "XXX"
["Email"]=> string(29) "XX#retro.com"
["thumbnailPhoto"]=> string(0) ""
}
}
}
}
}
The array has several "hits" inside "hits" and I want to loop trough and print out the stuff inside "_source". I have tried a few different approaches but I cant figure out any way to do this. Please help me.
foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
//do stuff
}
Try this
foreach ($arr['hits']['hits'] as $val)
{
echo $val['_source']['Mob'];
}
like this
I think this may handle it for you. Replace $the_array_you_provided with your "main" array variable (you've not specified it in the post).
$hits = $the_array_you_provided['hits']['hits'];
foreach ($hits as $hit) {
echo $hit['_source']['Title'];
//print everything in the array
//print_r($hit['_source']);
}
Any help feel free to ask.
Try this:
foreach ($array['hits']['hits'] as $hit) {
foreach ($hit['_source'] as $source) {
echo $source, '<br>';
}
}