X
X
Xamer0072021-03-14 01:17:15
PHP
Xamer007, 2021-03-14 01:17:15

Error: Unable to execute code, syntax contains errors! Where is the question? In which place?

Hello! Everyone's favorite tasks on arrays ...
The question is where did I make a mistake in the code? The problem was solved in two ways with the help of Google naturally ...

$basket = [
    [
      'position' => "Книга по php",
      'quantity' => 1,
    ],
    [
      'position' => "Мышь беспроводная",
      'quantity' => 12,
    ]
  ];
  countBasket($basket);
  function countBasket($all)
  {
    $sum = 0;
    foreach ($all as ['quantity' => $quantity]) {
      $sum += $quantity;
    }
    echo  $sum;
    return $sum;
  }


How not to change countBasket($basket); the error remains. I mean like this:
$basket = [
    [
      'position' => "Книга по php",
      'quantity' => 1,
    ],
    [
      'position' => "Мышь беспроводная",
      'quantity' => 12,
    ]
  ];
  
  function countBasket($all)
  {
    $sum = 0;
    foreach ($all as ['quantity' => $quantity]) {
      $sum += $quantity;
    }
    echo  $sum;
    return $sum;
  }
countBasket($basket);


Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
smigles, 2021-03-14
@Xamer007

foreach ($all as $item) {
    $sum += $item['quantity'];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question