2

I added the data to Joomla session as:

$cart['items'][] = array('id' => 1, 'name' => 'Laptop');
$cart['items'][] = array('id' => 2, 'name' => 'Tablet');
$session->set('cart', $cart);

If I want to retrieve the data and use

$cart_result = $session->get('cart');
echo $cart_result;

I get only the word Array. How can I retrieve the data properly? Please write different options: for whole array and single row. I don't mean echo print_r($cart_result, TRUE)

  • Could you add the result to your question you get when you use $session->get('cart', $cart);? Note the get-> instead of set-> – Lodder Apr 17 '15 at 9:03
  • The question is updated – stckvrw Apr 17 '15 at 9:45
  • Sorry, I meant var_dump($session->get('cart', $cart));. This should display an array of results for you. Please copy the results you see and I'll compile an answer for you – Lodder Apr 17 '15 at 10:06
  • array(1) { ["items"]=> array(2) { [0]=> array(2) { ["id"]=> int(1) ["name"]=> string(6) "Laptop" } [1]=> array(2) { ["id"]=> int(2) ["name"]=> string(6) "Tablet" } } } – stckvrw Apr 17 '15 at 10:20
0

Ok I've some up with the following which will hopefully work for you.

So firstly, you need to define the session variable like so:

$cart_result = $session->get('cart');

Then you will need to iterate though the array that gets returned, like so:

$keys = array_keys($cart_result);
$count = count(array_shift(array_values($cart_result)));

for ($i = 0; $i<$count; $i++) 
{
    $results = array();
    foreach ($keys as $key) 
    {
        $results[$key] = $cart_result[$key][$i];
    }

    foreach ($results as $result)
    {
        echo $result['id'];
        echo $result['name'];
    }
}

From the dump you provide in your comment, this will output the following:

1 Laptop
2 Tablet

Update (credit goes to @stckvrw)

For PHP 5.4+ where strict errors will be thrown, replace:

$count = count(array_shift(array_values($cart_result)));

with:

$tmp = array_values($cart_result);
$count = count(array_shift($tmp));
  • Yes, it displays what I need but there is also the error: Strict Standards: Only variables should be passed by reference in ... line # $count = count(array_shift(array_values($cart_result))); – stckvrw Apr 17 '15 at 10:58
  • Yup, you simply need to disable strict error reporting which most people do: stackoverflow.com/questions/9983286/… – Lodder Apr 17 '15 at 10:59
  • Is there some way to eliminate the cause of the error? – stckvrw Apr 17 '15 at 11:03
  • I'm not a massive PHP expert but it's due to array_shift. The iteration may need to be rewritten a little – Lodder Apr 17 '15 at 11:06
  • I've found: $tmp = array_values($cart_result);$count = count(array_shift($tmp)); You may update your answer – stckvrw Apr 17 '15 at 11:18
0

The simpler solution is:

foreach($cart_result['items'] as $result) {
    echo $result['id'];
    echo $result['name'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.