I have this value from an XML file
<InfoExtractor:views>35490904</InfoExtractor:views>
From - http://www.infoextractor.org/upfiles/songlinks.txt.xml
it is not showing when I use this code
foreach ($viewfile->channel->item as $viewfileinfo):
$title=$viewfileinfo->title;
$views=$viewfileinfo->InfoExtractor:views;
echo "<span> ",$title,"</span> <br/> <span> ",$views,"</span> <br/>";
The title is showing fine when I echo, but how do I output the views value to echo?
==========
I found the answer, I changed the line to
$views=$viewfileinfo->children('InfoExtractor', true)->views;
This takes the colon into account.
You could use get_object_vars to capture an array, then view all the keys and values to determine precisely what you want to echo:
$views_array = get_object_vars($views);
foreach ($views_array as $key => $value) {
echo 'key = '.$key.', value = '.$value.'<br/>';
}
Related
I have a PHP session array where it can be counted as multidimensional array, basically I am trying to store data inside my session array and i am successfully obtaining that part of the task. The main issue is, I am not able to echo them specifically and I have to use var_dump. When I try to print them with echo i got an notice which says array to string conversion. Please any help I would be appreciated how to print them with their own specific keys or values. The code as follows:
if (!is_array($_SESSION['products']['names'])){
$_SESSION['products']['names'] = array();
$_SESSION['products']['names']['prices']= array();
}else {
$pros = $_SESSION['products']['names'];
if (in_array($product->getName(), $pros, true)){
echo 'The product is available in your basket';
} else {
array_push($_SESSION['products']['names'],$product->getName());
array_push($_SESSION['products']['names']['prices'], $product->getPrice(Currency::getCurrentCurrency()));
foreach ($_SESSION['products'] as $val){
echo $val['names'];
echo $val['prices'];
}
}
}
The output that I receive as follows:
Notice: Undefined index: names in
Array to string conversion in
Use join() function in your foreach, like this:
echo join('<br>', $val);
Or instead of
echo $val['prices'];
write
echo $val['names']['prices'];
This is your problem...
// Here your assigning `['names']` as a string..
array_push($_SESSION['products']['names'],$product->getName());
// Then here you're overwriting the string with an array...
array_push($_SESSION['products']['names']['prices'], $product->getPrice(Currency::getCurrentCurrency()));
Change the first one to this..
array_push($_SESSION['products']['names']['name'],$product->getName());
Assuming $product->getPrice() returns a string or a number...
foreach ($_SESSION['products'] as $val){
foreach($val['names'] as $name){
echo $name['name'];
echo $name['prices'];
}
}
There is no issue with the code you have here. I don't see you trying to echo or vardump them directly so please show the code you are echoing them out specifically or the output from above and which line is giving you an issue.
If you want to echo each one out with it's price.
for($i=0;$i<count($_SESSION['products']['names']);$i++) {
echo $_SESSION['products']['names'][$i] . " " . $_SESSION['products']['names']['price'][$i];
}
I've been strugling with this problem for some time now. I can't figure out whats wrong.
Following code is very simple, VISOR_URL is a constant defined in another file. If i echo it outside forearch loop it prints the constant value. If I print it inside the loop it's value gets duplicated.
Same problem occurs if I use a variable.
Any ideas?.
Thanks in advance.
Sebastian
<?php
require_once('conf.php');//I require the file where VISOR_URL is defined
//VISOR_URL is defined in conf.php. define('VISOR_URL', $server_ip.'/'.VISOR_NAME);
echo VISOR_URL; //echoes http://192.168.0.15/tncvisornuevo
if (!empty($occurrence_ids)) {//occurrence_ids is an array and values are printed fine
foreach ($occurrence_ids as $key => $value) {
echo VISOR_URL; //echoes http://192.168.0.15/tncvisornuevohttp://192.168.0.15/tncvisornuevo
$ocurrencia = new ca_occurrences($value);
$nombre_ocurrencia = $ocurrencia->get('ca_occurrences.preferred_labels');
$link = ''.$nombre_ocurrencia.'';
echo $link."<br>";
}
}
?>
Following simple example does not duplicate the constant value:
<?php
//Define a constant
define('CONSTANT', 'imaconstant');
echo CONSTANT."<br>"; //Echoes imaconstant
$test_array = array(0,1,2,3,4,5,6,7,8);
foreach ($test_array as $key => $value) {
echo $value.CONSTANT,"<br>"; //Echoes nimaconstant, n+1imaconstant
}
?>
It'll echo it as many times the loop will run. For example -
$array = array('aa','bb','cc');
$var = "abc";
foreach($array as $key => $value);
{
echo $var.'<br>';
}
/*
abc
abc
abc
*/
Now above since the array has size of 3, the loop will run 3 times and echoes the variable...ofcourse 3 times.
I have a weird issue with my PHP script. I have an array $keys that is defined at the beginning of the script:
$keys = array("name","date","event","location","address","description","link","linkname");
at some point later on I'm looping through the array, trying to print the keys:
foreach ($keys as $key_show) {
echo ($key_show);
}
And nothing is actually printed. I put a var_dump($keys) before the loop, and it seems the array is still populated with the entries from above at this point in the script. Interestingly, as soon as I put the var_dump there, the keys also showed up in the foreach-loop.
Full script can be see here
From your link
} elseif (isset($_POST['editconfirm'])) {
...
if ($jsonConcerts) {
echo "<form method=\"POST\" action=\"edit.php\"";
//var_dump($keys);
foreach ($keys as $key_show) {
echo ($key_show. ": ");
//echo "<input class=\"wide\" name=\"".$key.
//"\" value=\"".$jsonConcerts[$counter][$key]."\"><br>\n";
}
...
You don't close the form tag, so all $key_show values are treated as attributes of the form tag and thus never show up in your html output.
If you run this script on the command line, you will see the array values with or without the var_dump().
Besides, why you use echo "string" and echo ("string") all mixed?
Try without brackets?
foreach ($keys as $key_show) {
echo $key_show;
}
I have some arrays that have each 4 values in them, named:
$myarray_row0
$myarray_row1
$myarray_row2
So normally I can use this echo statement to get at the values in the first array:
echo 'My value is '.$myarray_row0[0]; // This works fine
But I want to use a FOR LOOP to iterate through them and I'm getting stuck because I want to use something like:
for ($i=0; $i<=10; $i++)
{
echo 'My value is '.$myarray_row[$i][[$i]];
echo 'My value is '.$myarray_row[$i][$i+1]];
echo 'My value is '.$myarray_row[$i][[$i+2]];
echo 'My value is '.$myarray_row[$i][[$i+3]];
}
I'm not using the correct syntax for the $i's and the brackets needed... I'm TRYING (but failing) to get the echo to spit out the arrays contents, such as:
$myarray_row0[0]
$myarray_row0[1]
$myarray_row0[2]
$myarray_row0[3]
etc
Note that it's not truly a multidimensional array, it's one dimensional, but it almost LOOKS like it is multi-dimensional since the array names have 'row0', 'row1', 'row2', etc in them.
Anyone know the syntax for getting a variable like $myarray_row0[1] to be echo'ed using the $i's that are available inside the for loop?
THANKS!
You'd need to use a variable variable name. (its been a while since ive used php so might be wrong)
for ($i=0;$i<10l$i++) {
echo 'My variable name is '.${'myarray_row'.$id}[$i+0];
echo 'My variable name is '.${'myarray_row'.$id}[$i+1];
echo 'My variable name is '.${'myarray_row'.$id}[$i+2];
echo 'My variable name is '.${'myarray_row'.$id}[$i+3];
}
However, its generally a good idea to not use them at all. making a multi-dimensional array instead would be much better for your case.
Question: if your $myarray_rowN has 4 elements why does your example have $i+X in the index?
surely it will go out of bounds after the first iteration :/ (1+1 OK, 1+2 OK, 1+3 OK 1+4 !OK etc)
possibly something like this might be better? (could be javascript though)
$index = 0;
$rows = array();
while (isset(${'myarray_row'.$i})) {
array_push($rows, ${'myarray_row'.$i});
}
foreach ($rows as $row) {
echo 'My variable name is '.$row[0]."\r\n";
echo 'My variable name is '.$row[1]."\r\n";
echo 'My variable name is '.$row[2]."\r\n";
echo 'My variable name is '.$row[3]."\r\n";
}
Sounds like you need to use a variable variable. See the docs here: http://php.net/manual/en/language.variables.variable.php
Something like this should work:
for ($i=0;$i<10;$i++) {
$var = 'myarray_row'.$i;
echo 'My variable name is ' . $$var[$i+0];
...
}
how about something like this:
foreach($myarray_row0 as $key => $value){
echo 'The ' . $key . ' value of this array is: ' . $value;
}
The above will iterate through all values in $myarray_row0 and echo the current value
foreach ($data['data'] as $data) {
echo $data['title'][0];
//echo '<br />';
}
this will print out:
melon
apple
...
banana
pear
Now, how to jump all, only get the last value in a foreach? only need pear. Thanks.
If you only need the last value, then you don't need to loop. You can use end():
$lastItem = end($data['data']);
echo $lastItem['title'][0];
Note that this will set the internal array pointer to the last element. It might be necessary that you call reset($data) afterwards.
End has already been suggested (which I can recommend actually), however if you want to do it with foreach, you could do:
foreach ($data['data'] as $data) {
}
echo $data['title'][0];
//echo '<br />';
but this is really superfluous. You iterate over the array then only to store the last element into $data. So if there are no elements in 'data' at all this will fail (as with end, but end will return a false if the array is empty).
So go for:
$data=end($data['data']);