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
  • 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). Commented Feb 6, 2014 at 1:34

2 Answers 2

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"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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>";
}

3 Comments

Well, he probably wants to display more than one item.
Dear downvoter, may I know what’s wrong with my answer?
I already explained to you what was wrong with your answer, now that you fixed it I'll remove the downvote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.