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?

2 Answers2

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

AndroidLearner
  • 1,360
  • 7
  • 24
  • 40
  • 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` – Osvalda Kazlaučiūnaitė Jul 13 '12 at 13:04
  • **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. – AndroidLearner Jul 13 '12 at 13:14
  • Sow how to name fields when it is array? I tried to get what I want in other way, and almost worked.. http://deividas.experts.lt/code.txt but problem is that I cant get the price value value.. – Osvalda Kazlaučiūnaitė Jul 13 '12 at 13:24
  • you mean there is some problem at `$json_array[$i]['name'] == 'price'` – AndroidLearner Jul 13 '12 at 13:33
  • Yes, all values are being printed exept this one. gettin undefined index – Osvalda Kazlaučiūnaitė Jul 13 '12 at 14:01
  • well this looks strange, i am sorry actually without JSON you get in `$_POST['json']` i cannot tell what is the problem – AndroidLearner Jul 13 '12 at 15:38
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.

AndroidLearner
  • 1,360
  • 7
  • 24
  • 40