I am using the following code:
$row_arr=$_POST['row_driver'];
print_r($row_arr);
returns:
Array ( [0] => d1 [1] => d2 [2] => d3 [3] => d5 )
but
echo count($row_arr);
is returning me a value of
1
Any reason why?
Here row_driver is an array being received through a form from a previous PHP page using hidden element property of HTML form. Also,
foreach($row_arr as $driver)
{
//code here
}
is returning:
Warning: Invalid argument supplied for foreach() in
D:\XAMPP\htdocs\Carpool\booking_feed.php on line 36
The issue you are facing is with the fact that $_POST['row_driver'] is not an array.
If you have one hidden HTML input:
<input type="hidden" name="row_driver" value="<?php print_r($rows); ?>">
...then $_POST['row_driver'] would be a string, e.g.:
$_POST['row_driver'] = "Array ( [0] => d1 [1] => d2 [2] => d3 [3] => d5 )";
, and therefore, your count() function results in 1.
This would also explain the second issue you are facing, with foreach(), where the function expects an array, but you are providing a string.
A solution would be to use a foreach loop for your hidden HTML inputs like this:
<?php foreach($rows as $row_driver){?>
<input type="hidden" name="row_driver[]" value="<?php echo $row_driver; ?>"/>
<?php }?>
This would then turn your $_POST['row_driver'] into an array.
You might just store the count value in some variable :
$row_arr=Array('d1','d2','d3','d4');
print_r($row_arr);
$count = count($row_arr);
echo 'Your Count is:- '.$count;
PHP document:
expression
The expression to be printed. return
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will
return the information rather than print it.
Return Values
If given a string, integer or float, the value itself will be printed.
If given an array, values will be presented in a format that shows
keys and elements. Similar notation is used for objects.
When the return parameter is TRUE, this function will return a string.
Otherwise, the return value is TRUE.
print_r() can use as special printing method to display all values in arrays and for associative arrays(more helpful for this).
Associative array:
Associative arrays are arrays that use named keys that you assign to them.
If you use echo you have print it with a array index. As a example $row_arr[0] or if you use for associative array instead of index, key is used. it may be string.
The problem lies with the hidden field
foreach ($rows as $value){
<input type="hidden" name="row_driver[]" value="<?php echo $value; ?>">
}
Related
I fetched data and fill the value inside the table with for each loop like:
<?php
foreach ($data['rows'] as $value) {
?>
<tr>
<td> <?php echo $value->rsdntname; ?> </td>
<td> <?php echo $value->rsdntemail;?></td>
<td> <?php echo $value->rsdntphone; ?> </td>
</tr>
<?php } ?>
but in top section i have three columns which also filled with the fetch data like:
<h3 class="form-section">Building
<span class="pull-right" style="font-size:15px;color:#000;">
Core <?php $data['rows']->aptcore; ?>,
Floor,
Apartment No
</span>
</h3>
But it is returning me this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: admin/page.php
Line Number: 52
Why I am getting this error .. Provide me the solution Not able to understand.and give me the solution i am not able to understand .It saying tryng to access non object y this error returning with that. what i am doing wrong there
working with same page to access data for another level with loop its working outside the loop its not working
You're trying to read a property from an object:
$data['rows']->aptcore;
But the error is telling you that it's not an object. So what is it? This previous line of code implies to me that it's probably an array:
foreach ($data['rows'] as $value) {
Because you're looping over the array and reading properties from each object therein.
In a comment elsewhere on this page you specify some debugging output (print_r($data);):
Array (
[res] => 1
[rows] => Array (
[0] => stdClass Object (
[buldname] => BT Tower
[aptno] => 901
[aptcore] => 2
[aptfloor] => 2
[rsdntname] => Gaurav
[rsdntemail] => Gaurav#gmail.com
[rsdntphone] => 9891110987 )
(formatting mine)
If I understand that output correctly, $data is an associative array with two named elements, res and rows. rows is itself also an array containing one object. (Or does it contain more that you're just not showing us?) An array of one object is still an array, not an object. (A basket containing one apple is still a basket, not an apple.)
You could try to index the array to access the first element therein:
$data['rows'][0]->aptcore
Or if there are more elements and you want all of them, you can loop over the array exactly like you already do:
foreach ($data['rows'] as $value) {
echo $value->aptcore;
}
You are calling $value as if it is an object, but more than likely your $value is actually a nested array. Assuming your data in $data['rows'] are arrays then instead of using $value->rsdntname you should call it as $value['rsdntname']... and so on for each $value.
Why does the below code not assume an empty 'value' pair for the specified 'key'?
Take the following example:
$key1 = "An element";
$key2 = "Another, without a pair";
$key3 = "A third, with a pair";
$check=Array($key1=>21, $key2, $key3=>23);
If outputted using print_r, returns the following:
Array ( [An element] => 21 [0] => Another, without a pair [A third, with a pair] => 23 )
Rather than:
Array ( [An element] => 21 [Another, without a pair] => null [A third, with a pair] => 23 )
I want to have an array containing an unknown number of items, all of which may or may not have a key=>value pair. What are my options for ensuring that I get the second result?
Essentially, I want to pass a list of keys from my controller to a function, and for the function to identify them as key->value even if the value is null. Some of the keys might have values set, others might not.
It may be that the best solution lies in the foreach $key as $value {} code space, or that I can wrap $key1 in some form of parenthesis... I'm not sure!
Thanks
Add NULL as shown here.
$check=Array($key1=>21, $key2=>NULL, $key3=>23);
To Initialize the array:
$keys = array($key1, $key2, $key3);
$check = array_fill_keys($keys,NULL);
It depends in the type of the values.
Using NULL is a very common technique, But, if all values, are intended to be of the same type, maybe you would like to use an "empty" value for them, that is not NULL.
Example, if the values are integers, you may want to use 0 or -1 to indicate the value is not assigned. Or "" for strings.
In case that you are storing different types, then, you may like to use NULL as a non typed value.
How would I setup an associative array to reference specific values at different sections of a page. My function:
<?php
function park_data($park_page_id) {
$data = array();
if($park_page_id){
$data = mysql_fetch_assoc(mysql_query("SELECT * FROM `park_profile` WHERE `park_id` = $park_page_id"));
return $data;
}
}
?>
My print_r:
<?php
print_r (park_data(1));
?>
Produces the following associative array:
Array ( [park_id] => 1 [park_name] => Kenai Fjords [park_address] => 1212 4th Avenue [park_city] => Seward [park_state] => Alaska [park_zip] => 99664)
How would I print just the [park_name] value from this array?
From the docs:
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
// on PHP 5.4
print_r(park_data(1)['park_name']);
// earlier versions
$tmp = park_data(1);
print_r($tmp['park_name']);
$park=park_data(1);
echo $park['park_name'];
To output custom formatted text in general, and in this case to output only a single array's key value, use echo, because print_r() called on an array displays the whole array's structure and content, and that's not what you want:
<?php
// code
$park_data=park_data(1);
echo $park_data["park_name"];
// code
?>
Hi I recently came across an example of json_encode function. Very confused about 1 part:
<?php
$runners=array{
'fname'=>5
'you' => 6
};
echo json_encode (array("runners"=>$runners));
?>
Question is, why can't the code on the last row simply be:
echo json_encode ($runners);
Thanks,
First of all, your array declaration is incorrect and you will get a syntax error if you run the code. You should use array(...) not array{...}. And the values need to be comma-separated. For example:
array(
key => value,
key2 => value2,
key3 => value3,
...
)
The following should work:
$runners = array(
'fname' => 5,
'you' => 6
);
echo json_encode($runners);
Output:
{"fname":5,"you":6}
How are these two different
The end result is different for both cases. When you do json_encode(array("runners"=>$runners));, the array is multi-dimensional, and the JSON output will change, too:
{"runners":{"fname":5,"you":6}}
Which one should you use
Depends. In the first array, you are simply creating two keys named fname and you, and in the second, you also add another key, runners, thereby making the array multi-dimensional. If you want that information to be present in the resulting JSON string, you should use the second one. If not, use the first one.
First you have use { in array it is not correct. and you have not have , between array elements.
you can use both. but you have to access 2 json by different ways. depending in your choice.
for first (it should be best choice) echo json_encode ($runners); you have one dimensional array.
$runners=array(
'fname'=>5,
'you' => 6
);
echo json_encode ($runners);
OUTPUT:
{"fname":5,"you":6}
In second you have 2d array.
$runners=array(
'fname'=>5,
'you' => 6
);
echo json_encode (array("runners"=>$runners));
OUTPUT:
{"runners":{"fname":5,"you":6}}
Live Demo : https://eval.in/92104
Firstly your array is wrogly used. It will use small brackets () not curly {}. So your array will become :
$runners=array(
'fname' => 5,
'you' => 6
);
Now when you do json_encode() as: echo json_encode ($runners); you will get the output:
{"fname":5,"you":6}
And if you do : echo json_encode (array("runners"=>$runners)); you will get output:
{"runners":{"fname":5,"you":6}}
I have the following array (in php after executing print_r on the array object):
Array (
[#weight] => 0
[#value] => Some value.
)
Assuming the array object is $arr, how do I print out "value". The following does NOT work:
print $arr->value;
print $val ['value'] ;
print $val [value] ;
So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!
echo $arr['#value'];
The print_r() appears to be telling you that the array key is the string #value.
After quickly checking the docs, it looks like my comment was correct.
Try this code:
print $arr['#value'];
The reason is that the key to the array is not value, but #value.
You said your array contains this :
Array (
[#weight] => 0
[#value] => Some value.
)
So, what about using the keys given in print_r's output, like this :
echo $arr['#value'];
What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']
You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)