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

Salman
  • 1,380
  • 7
  • 25
  • 41
  • 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. – Salman 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'` – Salman 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 – Salman 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.

Salman
  • 1,380
  • 7
  • 25
  • 41
  • so how to get value by name (lets say need to get all values with name "pav") – Osvalda Kazlaučiūnaitė Jul 12 '12 at 21:06
  • 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"]; – Salman Jul 13 '12 at 06:23
  • when try to echo it I'm getting `Undefined index: pav` – Osvalda Kazlaučiūnaitė Jul 13 '12 at 06:31
  • i will again test this and will reply you after 6 hours, till then you can use `echo "
    "; print_r($assoc_array); echo "
    ";` to see array result.
    – Salman Jul 13 '12 at 06:37
  • hmm, now function is working partialy.. I'm still getting undefined error, but also values also is being printed. but insted `item1 item2 item3` I'm getting `item1 item1 item1 item2 item2 item2 item3 item3 item3` if I echo outside the loop I get `item3` – Osvalda Kazlaučiūnaitė Jul 13 '12 at 09:29
  • make sure `$assoc_array = array();` is out of loop. if this is placed inside loop that may declare array from beginning. In a couple of hours i will send you the correct solution after retest. – Salman Jul 13 '12 at 09:39
  • 1
    `$assoc_array = array();` is outside the loop also instead $json_array[i] I did $json_array[$i] – Osvalda Kazlaučiūnaitė Jul 13 '12 at 09:46