1
$item= "stackoverflow";
$price = "30.00";

$cs1_array = array();
$cs1_array['item'] = $item;
$cs1_array['price'] = $price;

    if($cs1_array > 0){
            echo "<table>";
            foreach ($cs1_array as $item){
                echo "<tr>";
                echo "<td>{$item['item']}</td><td>{$item['price']}</td>";
                echo "</tr>";
            }
            echo "</table>";
        }

This will output the first alphabet of the array only. Example it will display letter s only of the stackoverflow. I want to display the whole stackoverflow word, how to do so?

  • 1
    `if ($cs_array > 0)` doesn't make too uch sense. What do you actually want to check here? If you want to check if the array has any members, use `if (count($cs_array) > 0)`, if you want to check if the array is set to anything that does not equal "false" (emtpy arrays do, `false` does, `null` does, empty strings do, `0` does) , use `if ($cs1_array)`. – Johannes H. Feb 06 '14 at 01:34

2 Answers2

2

You did create a single level array, while what you need is a multidimensional array (i.e. an array in an array):

$cs1_array = array();
$cs1_array[] = array(
    'item' => $item,
    'price' => $price
);

Compare this:

// wrong
array(2) {
  ["item"]=>
  string(13) "stackoverflow"
  ["price"]=>
  string(5) "30.00"
}

vs.

// right
array(1) {
  [0]=>
  array(2) {
    ["item"]=>
    string(13) "stackoverflow"
    ["price"]=>
    string(5) "30.00"
  }
}
TimWolla
  • 31,849
  • 8
  • 63
  • 96
1

You don’t need the loop at all if you have only one item and one price in your array:

$item= "stackoverflow";
$price = "30.00";

$cs1_array = array();
$cs1_array['item'] = $item;
$cs1_array['price'] = $price;

    if(sizeof($cs1_array) > 0){
            echo "<table>";
            echo "<tr>";
            echo "<td>{$cs1_array['item']}</td><td>{$cs1_array['price']}</td>";
            echo "</tr>";
            echo "</table>";
    }

However, if you’d like to have multiple instances of item and price, you need an array of arrays:

$cs1_array = array();
$cs1_array[] = array(
    "item" => "stackoverflow",
    "price" => "30.00"
);
$cs1_array[] = array(
    "item" => "superuser",
    "price" => "40.00"
);
$cs1_array[] = array(
    "item" => "serverfault",
    "price" => "20.00"
);
// and so on

As a more concise alternative to the above code, you may create the array and fill it with values in a single statement:

$cs1_array = array(
    array(
        "item" => "stackoverflow",
        "price" => "30.00"
    ),
    array(
        "item" => "superuser",
        "price" => "40.00"
    ),
    array(
        "item" => "serverfault",
        "price" => "20.00"
    ),
    // and so on
);

Then the foreach loop will work correctly:

if(sizeof($cs1_array) > 0){
    echo "<table>";
        foreach($cs1_array as $item){
        echo "<tr>";
        echo "<td>{$item['item']}</td><td>{$item['price']}</td>";
        echo "</tr>";
        }
    echo "</table>";
}
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27