Increment an array counter - php

In the loop below I want $value[0] to increment on each pass of the loop. So, if $count = 2 the loop will run twice and output $key . " " . $value[0] and $key . " " . $value[1].
Right now, my loop is outputting $key . " " . $value[0] twice. What did I do wrong?
$count = count($updates['positionTitle']);
for($i = 1; $i<=$count; $i++){
foreach($updates as $key => $value){
if(!is_array($value))
echo $key . " " . $value . "<br/>";
else
echo $key . " " . $value[0]++ . "<br/>";
}
}

You need to store what the current index is. I'm not actually sure what loop you were talking about though. I'm assuming it was the outer for loop. I still think this is broken but based on your comment this is what you want.
$count = count($updates['positionTitle']);
$idx = 0;
for($i = 1; $i<=$count; $i++){
foreach($updates as $key => $value){
if(!is_array($value))
echo $key . " " . $value . "<br/>";
else
echo $key . " " . $value[$idx] . "<br/>";
}
$idx++;
}

Change:
foreach($updates as $key => $value){
To:
foreach($updates as $key => &$value){

Related

Iterate through Nested array in PHP

I have a nested array on this link Array Sample
I am using code below to parse this, but second and beyond depth it's returning nothing. However tried with recursive function.
printAllValues($ArrXML);
function printAllValues($arr) {
$keys = array_keys($arr);
for($i = 0; $i < count($arr); $i++) {
echo $keys[$i] . "$i.{<br>";
foreach($arr[$keys[$i]] as $key => $value) {
if(is_array($value))
{
printAllValues($value);
}
else
{
echo $key . " : " . $value . "<br>";
}
}
echo "}<br>";
}
}
What I am doing Wrong? Please help.
Version of J. Litvak's answer that works with SimpleXMLElement objects.
function show($array) {
foreach ($array as $key => $value) {
if (!empty($value->children())) {
show($value);
} else {
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
You can use recurcive function to print all values:
function show($array) {
foreach( $array as $key => $value) {
if (is_array($value)) {
show($value);
} else{
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);

How to detect URL redirections and separate each header?

I have a problem: i want to detect all redirections of a 3xx response code and separate each header of URL. I investigated that get_headers function follows redirections and put it in an array of each component of redirection. I know that "fb.com" redirects 3 times as you can see on this page:
I want to do the same thing of that page, and am trying to do so:
But my PHP code seems to be wrong. Here it is:
<?php
$header = array();
$header[] = array();
$header_url = #get_headers('http://fb.com',1);
/*
foreach ($header_url as $key => $value){
echo $key . " = " . $value . "<br />";
}
*/
$a = 0;
foreach($header_url as $key => $value){
if(is_int($key)){
$header[$a][] = $value;
$a++;
} else if(!is_int($key) && !is_object($key)){
$header[0][$key] = $value;
} else if(!is_int($key) && is_object($key)){
foreach ($key as $key2 => $value2){
$header[$key2][$key] = $value2;
}
}
}
echo "<h1>ONE</h1>";
foreach ($header[0] as $key => $value) {
echo $key . " = " . $value . "<br />";
}
echo "<h1>TWO</h1>";
foreach ($header[1] as $key => $value) {
echo $key . " = " . $value . "<br />";
}
echo "<h1>THREE</h1>";
foreach ($header[2] as $key => $value) {
echo $key . " = " . $value . "<br />";
}
?>
I would appreciate help on what i am doing wrong.
THANKS SO MUCH AND HAVE A NICE DAY DEV!

PHP: how to make a sum inside a "for each" cycle?

I've made a foreach loop that print some variables found in an xml file:
foreach ($xml->result->rowset->row as $row)
{
echo $row["price"] . " " . $row["product"] . "</br>";
}
I would like to get the sum of all the "prices" shown. How can i do?
Just have a variable add up those values as you iterate:
$total = 0; // start with zero
foreach ($xml->result->rowset->row as $row)
{
$total += $row["price"]; // add price to total
echo $row["price"] . " " . $row["product"] . "</br>";
}
echo $total; // echo out total amount
Store the count in a variable:
$total = 0;
foreach ($xml->result->rowset->row as $row) {
echo $row["price"] . " " . $row["product"] . "</br>";
$total += $row['price']; // assumed row_price is some integer
}
echo $total;
Simply add it to a new variable:
$sum = 0;
foreach ($xml->result->rowset->row as $row) {
echo $row["price"] . " " . $row["product"] . "<br />";
$sum += $row["price"];
}
echo $sum . "<br />";
$sum = 0;
foreach ($xml->result->rowset->row as $row)
{
echo $row["price"] . " " . $row["product"] . "</br>";
$sum += $row["price"] ;
}
echo $sum ;
isnt that simple ?
$sum=0;
foreach ($xml->result->rowset->row as $row)
{
echo $row["price"] . " " . $row["product"] . "</br>";
$sum +=$row["price"];
}
echo $sum;
just try this code!!

Can i nest a foreach within foreach?

I have an array: $categories = array("item1", "item2", "item3");
I also have three arrays: $item1Array = array("hi", "items");, $item2Array = array("hi", "items");, $item3Array = array("hi", "items");
I stated a foreach like this:
foreach ($categories as &$value) {
echo "<optgroup label='" . $value . "'>';
$nextArray = $value . "Array";
foreach($nextArray as &$nextValue) {
echo "<option value='" . $nextValue . "'>" . $nextValue . "</option>";
}
}
but it get an error Warning: invalid argument supplied for foreach().
Is there a way I can achieve this?
Yes, you could, by ${$nextArray}. but notice named variables is not good practice, you could use associative array instead.
And note you don't need to use reference in this case.
$categories = array("item1", "item2", "item3");
$item1Array = array("hi", "items");
$item2Array = array("hi", "items");
$item3Array = array("hi", "items");
foreach ($categories as $value) {
echo "<optgroup label='" . $value . "'>";
$nextArray = $value . "Array";
foreach(${$nextArray} as $nextValue) {
echo "<option value='" . $nextValue . "'>" . $nextValue . "</option>";
}
}
Of course, but as you can clearly see from the syntax highlighting of your post, you used a ' instead of a " at the end of the "optgroup" line.
Also, you could just use a nested array:
$categories = Array(
"item1"=>Array("hi","items"),
"item2"=>Array("hi","items"),
"item3"=>Array("hi","items"),
);
foreach($categories as $key=>$array) {
echo "<optgroup label='".$key."'>";
foreach($array as $value) {
echo "<option>".$value."</option>";
}
echo "</optgroup>";
}

To get the multi dimention array values in php

I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}

Categories