jonnyenglish89 Posted November 23, 2017 Share Posted November 23, 2017 Hey guys, I needed to covert the $main array in to a string so I used json_encode to do it but now I’m unsure how to get just the key values for $main[0] and $main[1] – can anyone help?main array: Array ( [0] => Array ( [25256] => 1 ) [1] => Array ( [50005] => 1 ) [2] => Array ( [301] => 1 [2318] => 1 [13403] => 1 [49489] => 1 ) ) current code and output: $com_id = json_encode($main[0]); = {"25256":1} $dom_id = json_encode($main[1]); = {"50005":1} $car_id = json_encode($main[2]); = {"301":1,"2318":1,"13403":1,"49489":1} Desired output: $com_id = {"25256"} $dom_id = {"50005"} $car_id = {"301":1,"2318":1,"13403":1,"49489":1} Quote Link to comment https://forums.phpfreaks.com/topic/305744-how-do-i-get-specific-values-from-a-json-encoded-string/ Share on other sites More sharing options...
Solution requinix Posted November 23, 2017 Solution Share Posted November 23, 2017 array_keys Quote Link to comment https://forums.phpfreaks.com/topic/305744-how-do-i-get-specific-values-from-a-json-encoded-string/#findComment-1554053 Share on other sites More sharing options...
jonnyenglish89 Posted November 23, 2017 Author Share Posted November 23, 2017 ok, I'm still stuck... I've tried this: foreach($main as $key => $value) { $t[] = array_keys($value); } $com_id = json_encode($t[0]); = [25256] $dom_id = json_encode($t[1]); = [50005] when I do a var_dump it says it's a string: string(7) "[25256]"what am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/305744-how-do-i-get-specific-values-from-a-json-encoded-string/#findComment-1554056 Share on other sites More sharing options...
jonnyenglish89 Posted November 23, 2017 Author Share Posted November 23, 2017 nevermind... $com_array = array_keys($main[0]); $com_id = $commander_array[0]; $dom_array = array_keys($main[1]); $dom_id = $dominion_array[0]; $cards = json_encode($main[2]); this does the trick. Quote Link to comment https://forums.phpfreaks.com/topic/305744-how-do-i-get-specific-values-from-a-json-encoded-string/#findComment-1554060 Share on other sites More sharing options...
jonnyenglish89 Posted November 23, 2017 Author Share Posted November 23, 2017 where's the best hint button? Quote Link to comment https://forums.phpfreaks.com/topic/305744-how-do-i-get-specific-values-from-a-json-encoded-string/#findComment-1554062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.