PHP how to retrieve array values
$name = $array['name'];
$comment = $array['comment'];
$tags = $array['item']['tags']; // this will be an array of the tags
You can then loop over the tags like:
foreach ($tags as $tag) {
// do something with tag
}
Or access each one individually
echo $tags[0];
echo $tags[1];
How can I retrieve data from array
You can use the array_walk function
$total = [];
array_walk($sales, function($value) use(&$total) {
foreach ($value as $key => $arr) {
echo $arr. "<br>";
}
});
Get single array value
I Understand What You wants to say
use foreach loop insted of print_r()
<?php
$array = array("https://example.com/page-1");
foreach($array as $key => $value)
{
echo $value;
}
echo "<hr>";
echo $array [0];
?>
Also check Result Here.
How to get specific Array value from an Array in Laravel
The $user
variable seems to be holding an array containing 1 element, not the User itself. You must first retrieve the User object from the array and then access the username attribute.
$user = $request->instance()->query('user')['user'];
var_dump($user->username); exit;
How to get values only array in php?
Please read answer carefully.
$arr = array('UK', 'France', 'USA'); // It has 0 ,1 ,2 keys but you cannot see in the code`
But in browser you can see it.
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
Just use loop to print each country
foreach($arr as $country){
echo $country."<br>";
}
You can understand it by below loop
foreach($arr as $k=>$country){
echo "$k => $country"."<br>"; // Here $k is key like 0,1,2..
}
If you have array like below:-
$arr = array('uk'=>'UK', 'france'=>'France', 'usa'=>'USA') // It has uk ,france ,usa keys
now array_values($arr)
will give you output as below
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
It will remove all keys and regenerate index of key from 0.
Refer below links to understand PHP array:-
Link1
Link2
Link3
Hope it will help you :)
How to get specific array value in php
You can get the productcode
using explode()
function, like this,
$product_code = explode("productcode: ", $option)[1];
Here's the reference:
- explode()
So your code should be like this:
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$product_code = explode("productcode: ", $option)[1];
$total = $content[$quantity]*$content[$price];
}
?>
Get Array values in a loop in PHP
This is my suggestion for a different way to do this. It cuts down on the repeated code, and minimizes the PHP/JS mixing.
First, build your array a little differently as you fetch the rows from your query:
while ($row = $results->fetch_assoc()) {
// Loop over each of the temp_x columns
for ($i=0; $i < 4; $i++) {
$x = str_pad($i, 2, '0', STR_PAD_LEFT);
// add the value for each temp_x to a temporary array, using time for a key
$temp = is_null($row["temp_$x"]) ? 'null' : round($row["temp_$x"], 2);
$times[$x][] = $row['iddevice'] . ':' . $temp;
}
}
Next, format the values of each array in the temporary array
foreach ($times as $time => $temps) {
$data[] = "{ y: '{$time}h', " . implode(', ', $temps) . '}';
}
Then convert the $data
array to a string:
$data = implode(",\n", $data);
That way, all the PHP you'll need in the JS part is:
Morris.Line({
element: 'morris-area-chart',
data: [ <?php echo $data ?> ]
There may be a better way using json_encode
. If you change the code inside the while
loop that fetches rows from your database just slightly:
for ($i=0; $i < 4; $i++) {
$x = str_pad($i, 2, '0', STR_PAD_LEFT);
$temp = is_null($row["temp_$x"]) ? null : round($row["temp_$x"], 2);
$times[$x]['y'] = $x.'h';
$times[$x][$row['iddevice']] = $temp;
}
Then $data
can be formed using json_encode
. (The array_values
is used to reindex the array with numeric indexes so it will render as an array in the JSON output instead of an object.)
$data = json_encode(array_values($times));
And in the JS:
Morris.Line({
element: 'morris-area-chart',
data: <?php echo $data ?>
// note: no [] brackets around $data in this version
Whether or not this will work depends on whether or not it's okay for all the keys to be strings, because JSON keys are strings, so you'll get output like
{ "y": "01", "1": 17.62, "2": 19.52 }
instead of
{ y: '01', 1: 17.62, 2: 19.52 }
I think it should probably work anyway.
Related Topics
Generating Random Numbers from Skewed Normal Distribution
How to Change the Woocommerce_Form_Field HTML Structure
How to Convert Pascalcase to Snake_Case
How to Downgrade or Install a Specific Version of Composer
Curl Error 60: Ssl Certificate in Laravel 5.4
Use One Laravel Migrations Table Per Database
Generating a Screenshot of a Website Using Jquery
Jquery $.Ajax Request of Datatype JSON Will Not Retrieve Data from PHP Script
Understanding Imagemagick's Convert and Translating to Ruby Rmagick
PHP Mail Special Characters Utf8
Warning: MySQL_Result() Expects Parameter 1 to Be Resource, Boolean Given
Fatal Error - 'Mongo' Class Not Found
How to Get the Request Parameters in Symfony 2
Detect Exif Orientation and Rotate Image Using Imagemagick