could someone explain me why I can not get the data from that array?
My echo only returns "array". where I'm going wrong?
<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));
foreach($people as $vperson => $person){
echo $person;
}
?>
Well as you said you have an array, your variable $person is also an array so you can't just make echo on that. You can access values by the names "age, name and colour".
Try this:
<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));
foreach($people as $vperson => $person){
echo $name = $person['name'];
echo $age = $person['age'];
echo $colour = $person['colour'];
echo "<br>";
}
?>
Because, your are printing array with echo.
You can print the array like:
<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));
foreach($people as $vperson => $person){
echo '<pre>';
print_r($person);
echo '</pre>';
echo $name = $person['name'];
echo $age = $person['age'];
echo $colour = $person['colour'];
}
Use this to display key and equivalent value
foreach($people as $pe)
{
foreach($pe as $key => $person)
{
echo $key.' : '.$person.' </br> ';
}
}
Related
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$index]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
how to pass $index to array value? I am trying to check value of the index is greater than or not in if condition. How to get this?
With your code now it couldn't work since your print_r($val) result was :
Array
(
[S0001] => 2
[S0002] => 1
)
The key of your $vals are the result of your $suppliername array, so try like this maybe :
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$suppliername[$index]]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
}
}
The result :
S0001+++Multiple Entries
S0002+++Single Entry
S0001+++Multiple Entries
Is this what you are looking for?
I'm not sure i understood well what you wanted, but here it does what you want as result :
<?php
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
echo "<pre>";
var_dump($suppliername);
echo "</pre>";
echo "<pre>";
var_dump($vals);
echo "</pre>";
foreach($Product as $index => $value)
{
if($index>1)
{
echo "<br />" . $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo "<br />" . $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
I have a foreach loop and have a problem with it. My loop show duplicate some item and I want show item only once and don't duplicate item. My loop it is.
foreach($result as $res){
$id = $res->id;
$title = $res->title;
echo $title;
}
$partial=array();
foreach($result as $res){
if (!in_array($res->id, $partial)) {
$id = $res->id;
$title = $res->title;
echo $title;
array_push($partial, $res->id);
}
}
try this one..
$present_array = array();
foreach($result as $res){
if(!in_array($res->title, $present_array)){
$present_array[] = $res->title;
$id = $res->id;
$title = $res->title;
echo $title;
}
}
<?php
function remove_dup($matriz) {
$aux_ini=array();
$entrega=array();
for($n=0;$n<count($matriz);$n++)
{
$aux_ini[]=serialize($matriz[$n]);
}
$mat=array_unique($aux_ini);
for($n=0;$n<count($matriz);$n++)
{
$entrega[]=unserialize($mat[$n]);
}
return $entrega;
}
foreach(remove_dup($result) as $res){
$id = $res->id;
$title = $res->title;
echo $title;
}
another solution is, if you are getting this data from database then use DISTINCT in your select query.
Do a fast check & make your code execute faster like this:
$check_dup = array();
foreach ($items as $item){
if (in_array($item['id'], $check_dup))
continue;
else {
array_push($check_dup, $item['sid']);
/* your code in foreach loop */
}
}
Try this:
$result = array(
array(
'id' => "1",
'title' => "My title"
),
array(
'id' => "2",
'title' => "My title 2"
)
);
foreach($result as $res){
$id = $res['id'];
$title = $res['title'];
echo $title . "<br>";
}
I am having a array like so and I am looping trough it like this:
$options = array();
$options[0] = 'test1';
$options[1] = 'test2';
$options[2] = 'test3';
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
It outputs as expected:
Value=test
Value=test2
Value=test3
Now I want to add some options to my array and loop trough them:
$options = array();
$options['first_option'] = 'test';
$options['second_option'] = get_option('second_option');
$options['third_option'] = get_option('third_option');
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
But it does not work as I want. Because it outputs:
Value=first_option
Value=second_option
Value=third_option
So now I do not know how to access stored values using foreach from these guys?
Something like:
Value=first_option='test'
So when I use print_r($options)
Output is:
Array
(
[first_options] => test
[second_option] =>
[third_option] =>
)
1
your loop should look like this:
foreach($options as $key => $val){
echo "Val: ".$val;
echo "<br/>";
}
Your code is working just as expected and producing the desired result. You must have something else changing the values in $options. Correction: now that I see your edit, your functions are not returning any values, so options 1 and 2 are blank. Make sure that function returns something. Other than that, all of this code is good.
By the way, I recommend this:
$options = [
'first_option' => 'test',
'second_option' => get_option('second_option'),
'third_option' => get_option('third_option')
];
foreach($options as $key) {
echo "Value = {$key}<br>";
}
you can also use:
foreach($options as $key => $value) {
echo "Value - {$value} = {$key}<br>";
}
or you could at least replace array() with just []. Those are just some suggestions for neatness.
I have been looking at this code etc for so long that I am now confusing myself - not good
I have foreach of
foreach($sort_order as $sort)
{
echo '<pre>';
var_dump($sort['sorder']);
echo '</pre>';
}
That gives me a result of:
string(2) "20"
string(2) "10"
How can I return this so I can do value="<?php echo $someValue; ?>"
Assuming that 'sorder' is a key in your array I would try the following:
foreach($sort_order as $key => $sort) {
echo '<pre>';
if($key == "sorder") {
echo $sort[$key];
}
echo '</pre>';
}
It looks like you're only displaying they key. I'm assuming your array is of type associative.
To traverse associative arrays in PHP, follow this:
foreach($array as $key => $value)
{
echo "[" . $key . "]" . " = " . $value . "<br />;
}
I'm not sure what are you trying to do but...
<?php
$val = array();
foreach($sort_order as $sort) {
$val[] = $sort['sorder'];
}
?>
<p>value = <?php echo $val[0]; ?></p>
<p>value = <?php echo $val[1]; ?></p>
Perhaps I'm simply having trouble understanding how php handles arrays.
I'm trying to print out an array using a foreach loop. All I can seem to get out of it is the word "Array".
<?php
$someArray[]=array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value;
?>
<br />
<?php
}
?>
This prints out this:
Array
I'm having trouble understanding why this would be the case. If I define an array up front like above, it'll print "Array". It almost seems like I have to manually define everything... which means I must be doing something wrong.
This works:
<?php
$someArray[0] = '1';
$someArray[1] = '2';
$someArray[2] = '3';
$someArray[3] = '4';
$someArray[4] = '5';
$someArray[5] = '6';
$someArray[6] = '7';
for($i=0; $i<7; $i++){
echo $someArray[$i]."<br />";
}
?>
Why won't the foreach work?
here's a link to see it in action >> http://phpclass.hylianux.com/test.php
You haven't declared the array properly.
You have to remove the square brackets: [].
<?php
$someArray=array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value;
?> <br />
<?php
}
?>
Try:
<?php
$someArray = array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value . "<br />\n";
}
?>
Or:
<?php
$someArray = array(
0 => '1',
'a' => '2',
2 => '3'
);
foreach($someArray as $key => $val){
echo "Key: $key, Value: $val<br/>\n";
}
?>
actually, you're adding an array into another array.
$someArray[]=array('1','2','3','4','5','6','7');
the right way would be
$someArray=array('1','2','3','4','5','6','7');