简体   繁体   中英

I have var_dump a variable and it is empty

I want to know the reasons why this variable is passing empty.

<form action="cart.php" method="POST">
<input style="width:10px; margin-left:9px; " name="price[]" type="checkbox" value="' . $variety['price'].'_'. $variety['variety']. '_'. $product['name'] . ' "  /></form>

Can you see $product['name'] how can I print the it's value after extracting it's values in cart.php as

extracts values

list($aDoor, $variety,$productname) = split('_', $_POST['price']);
$aDoor = array();
$variety = array();
$productname= array();

foreach ($_POST['price'] as $p)
{
  list($a, $b,$c) = explode('_', $p);
  $aDoor[] = $a;
  $variety[] = $b;
  $productname[] = $c;
} 

Now below the foreach loop how can I echo the print of productname once..?

foreach($productname as $name) {
  echo $name . '<br />';
}

or if you want to associate the product names with their other values in $aDoor and $variety you could do:

foreach($productname as $index => $name) {
  echo 'Name: ' . $name . '<br />';
  echo 'Variety: ' . $variety[$index] . '<br />';
  echo 'Price: ' . $aDoor[$index] . '<br />';
}

EDIT:

If I can take your comment to mean that all of the names are the same in the $productname array then you can do this instead:

if(count($productname) > 0) {
  echo 'Product Name: ' . $productname[0] . '<br />';
  foreach($variety as $index => $name) {
    echo $name . ': $' . $aDoor[$index] . '<br />';
  }
}

Now below the foreach loop how can I echo the print of productname once..?

print_r ($productname);

But if you want to see each value of product name inside the loop:

foreach ($_POST['price'] as $p)
{
  list($a, $b,$c) = explode('_', $p);
  $aDoor[] = $a;
  $variety[] = $b;
  $productname[] = $c;

  echo $c . '<br />'; // show product name
} 

Also, i don't see your echoing this line of code with php:

<input style="width:10px; margin-left:9px; " name="price[]" type="checkbox" value="' . $variety['price'].'_'. $variety['variety']. '_'. $product['name'] . ' "  /></form>

in which case this is not how to show value in above line:

value="' . $variety['price'].'_'. $variety['variety']. '_'. $product['name'] . ' "

instead you need to wrap it in php tags:

value="<?=$variety['price'].'_'. $variety['variety']. '_'. $product['name']?>"

As I stated in another of your questions, you cannot use split() on an array. You're forcing PHP to treat $_POST['price'] as an array by naming it "price[]" in your form. WHen you split on an array:

$arr = array('a', 'b', 'c');
list($a, $b, $c) = split($arr);

you end up with the following:

$a = 'Array';
$b = NULL;
$c = NULL;

You will have to do the following:

list($aDoor, $variety,$productname) = explode('_', $_POST['price'][0]);
                                                                  ^^^   (note the array notation)

You're also not creating a $product variable in your extractions cript. You are creating a $productname array, but $productname is not the same as $product['name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM