Jump to content

How do I get specific values from a json encoded string?


Go to solution Solved by requinix,

Recommended Posts

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}

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.