0

I'm trying to get specific value by array name:

<?php
$json = json_decode($_POST['json'], true);

print_r($json);
?>

I'm getting this varray:

Array
(
    [0] => Array
        (
            [name] => pav
            [value] => g
        )

    [1] => Array
        (
            [name] => ppav
            [value] => f
        )

    [2] => Array
        (
            [name] => kiekis
            [value] => g
        )

    [3] => Array
        (
            [name] => kaina
            [value] => g
        )

    [4] => Array
        (
            [name] => ppav
            [value] => f
        )

    [5] => Array
        (
            [name] => kiekis
            [value] => g
        )

    [6] => Array
        (
            [name] => kaina
            [value] => f
        )

    [7] => Array
        (
            [name] => ppav
            [value] => g
        )

)

Tried using foreach function, but cant get specific value:

foreach ($json as $key => $value) {
    echo "name".$key['name']." value".$value['value']."<br />";
}

It prints all array values:

name value<br />name valueasd<br />name valueasd<br />name values<br />name values<br />name values<br />name values<br />name valuea<br />name valueasd<br />name valued<br />

But I cant select specific value by name to add to nysql. How to do that?

5
  • "But I cant select specific value by name" <-- why not? Commented Jul 11, 2012 at 20:46
  • @H2CO3 maybe I'm writing wrong syntax Commented Jul 11, 2012 at 20:47
  • 1
    'name' and 'value' are both elements in your $value array, not in the $key which is always a scalar type (either integer or string). Commented Jul 11, 2012 at 20:48
  • @lanzz I need to print something all selected values like: $value['ppav']->value; Commented Jul 11, 2012 at 21:51
  • see this post Commented Jul 12, 2012 at 8:35

2 Answers 2

2

Following is the tested code

<?php
    $json_array = array(
                    array('name'=>'pav', 'value'=>'g'),
                    array('name'=>'ppav', 'value'=>'f'),
                    array('name'=>'kiekis', 'value'=>'g'),
                    array('name'=>'ppav', 'value'=>'f')
                    );

    echo "<pre>";
    print_r($json_array);
    echo "</pre>";

    $assoc_array = array();

    for($i = 0; $i < sizeof($json_array); $i++)
    {
        $key = $json_array[$i]['name'];
        $assoc_array[$key] = $json_array[$i]['value'];
    }

    echo "<pre>";
    print_r($assoc_array);
    echo "</pre>";

    echo "assoc_array['pav'] = ".$assoc_array['pav'];
?>

output of the code is given below and you can see that exactly same array as yours is converted into associative array, there is one problem as your array has repeated names eg. ppav or kiekis so there will only 1 index for kiekis or ppav having the latest value.

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

now code work without errors, but outside loop it prints only last value, but what if I have several fields with same name, inside loop as I have mentioned before I'm getting item1 item1 item1 item2 item2 item2 item3 item3
You cannot have same multiple index of same name. Index is always unique. You can see i took same array as yours, used same code to convert that array into associative array, then i printed it and accessed it using its name. The code is running fine. Well if you can send me your code then i can see what the real problem is.
Sow how to name fields when it is array? I tried to get what I want in other way, and almost worked.. deividas.experts.lt/code.txt but problem is that I cant get the price value value..
you mean there is some problem at $json_array[$i]['name'] == 'price'
Yes, all values are being printed exept this one. gettin undefined index
|
1

you have to recreate array

$json_array = json_decode($_POST['json'], true);
$assoc_array = array();

for($i = 0; $i < sizeof($json_array); $i++)
{
     $key = $json_array[$i]['name'];
     $assoc_array[$key] = $json_array[$i]['value'];
}

after this you will get $assoc_array and you can access its elements by keys.

7 Comments

so how to get value by name (lets say need to get all values with name "pav")
resultant $assoc_array will be like $assoc_array("pav"=>"g", "ppav"=>"f", "kiekis"=>"g", and so on......); and you can access it like echo $assoc["pav"];
when try to echo it I'm getting Undefined index: pav
i will again test this and will reply you after 6 hours, till then you can use echo "<pre>"; print_r($assoc_array); echo "</pre>"; to see array result.
$assoc_array = array(); is outside the loop also instead $json_array[i] I did $json_array[$i]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.