How can send entire php array values on button click?

Tags: , , , ,



I want to send entire array information by the button click event, basically I store all information in the session send it on button, the problem is that just one value of array sent from session array not the whole array. I don’t know how can I do it? When I print the $WhatsApp_info it print complete array which item store in session but when I send on button one item information are sent.

foreach($_SESSION["WhatsApp_cart"] as $values){  
    $WhatsApp_info=array(                              
        $WhatsApp_name=$values[0], 
        $WhatsApp_price=$values[1], 
        $WhatsApp_quantity=$values[2] 

    );
    $WhatsApp_json=json_encode($WhatsApp_info);

    $link = 'https://wa.me/'.$phone.'?text='.$txt_final.' 
    '.$WhatsApp_json.' ';
}

Answer

Make sure you don’t overwrite your array in your loop. Create a new entry using the empty bracket methode.

$WhatsApp_info = [];
foreach($_SESSION["WhatsApp_cart"] as $values)
{
    // Add to info instead of overwriting it.
    $WhatsApp_info[] = [
        'WhatsApp_name' => $values[0], 
        'WhatsApp_price' => $values[1], 
        'WhatsApp_quantity' => $values[2],
    ];
}
$WhatsApp_json = json_encode($WhatsApp_info);
$link = 'https://wa.me/' . $phone . '?text=' . $txt_final . ' ' . $WhatsApp_json . ' ';


Source: stackoverflow