1

I'm not very good when it comes to arrays so here's probably something very simple but not for me! I'm getting an array of values via POST and I need to parse them and store the values in a table. How should I use the classic parsing such as:

foreach($array as $a) {
  $text = $a->text;
  $name = $a->user->name;
}

etc to parse an array looking like this:

[item] => Array
        (
            [tags] => Array
                (
                    [0] => Bluetooth
                    [1] => WiFi
                    [2] => USB
                )

        )

This is the entire POST array:

Array
(
    [prodid] => 
    [Submit] => Save
    [productcode] => 797987
    [cat_id] => 66
    [brand] => Fysiomed
    [name] =>  asdc asdc asd c
    [productnew] => yes
    [item] => Array
        (
            [tags] => Array
                (
                    [0] => Bluetooth
                    [1] => WiFi
                    [2] => USB
                )

        )

    [size] => 1
    [barcode] => 7979871
    [price] => 233.00
    [priceoffer] => 0.00
    [stock] => 50
    [weight] => 0.30
    [orderby] => 1
)
5
  • Could you tell us what are your POST inputs? Commented May 4, 2012 at 15:00
  • How does your POST array look like? Commented May 4, 2012 at 15:00
  • POST array added to 1st post. Commented May 4, 2012 at 15:02
  • It's not clear wht you want to do. You can't put a multi-dimensional array into a 2 dimensional table. You need to expand a bit. Commented May 4, 2012 at 15:08
  • I need to get all the tags as strings and add them to a table in my db. Commented May 4, 2012 at 15:14

4 Answers 4

3
if( isset($_POST['item']) && isset($_POST['item']['tags']) ){
  foreach($_POST['item']['tags'] as $tag){
    //do stuff...e.g.
    echo $tag;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1
if(isset($_POST) && !empty($_POST)) {
  foreach($_POST as $key => $value) {
    if($key == 'item') {
      echo $value[$key]['tag'][0]. '<br>';
      echo $value[$key]['tag'][1]. '<br>';
      echo $value[$key]['tag'][2]. '<br>';
    } 
  }
}

1 Comment

You don't have to iterate through teh whole $_POST array
1

Looks like your array is shaped like this, check this

$array = array( "item" => array( "tags" => array("Bluetooth", "Wifi", "USB" ) ) );
var_dump($array);

You will see something like this

array(1) {
  ["item"]=>
  array(1) {
    ["tags"]=>
    array(3) {
      [0]=>
      string(9) "Bluetooth"
      [1]=>
      string(4) "Wifi"
      [2]=>
      string(3) "USB"
    }
  }
}

Now for parsing this array,

foreach($array as $in => $val) {
    // as $array has key=>value pairs, only one key value pair
    // here $in will have the key and $val will have the value
    // $in will be "item"
    print $in; // this will print "item"
    foreach($val as $in2 => $val2 ){
        // only one key : "tags"
        print $in; // this will print "tags"
        print $val2[0];  // this will print "Bluetooth"
        print $val2[1];  // this will print "Wifi"
    } 
}

I hope this clears you doubt regarding arrays.

1 Comment

I will use this as an example to try and understand them more. Thank you.
1

Are you just trying to get the text out? try this.

foreach($array['item']['tags'] as $tag) {
   $text = $tag;
}

3 Comments

This outputs the following Warning: Invalid argument supplied for foreach()
'tag' should be 'tags', I think.
You have to replace array with whatever your array is called. $_POST maybe.

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.