php doesn't fetch end of array - php

foreach doesn't fetch end of array. I want to use end of that in another method:
$array = array(
"Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
foreach ($array as $kk=>$val1)
{
echo $kk.'<br/>';
foreach ($val1 as $key=>$val2)
{
if (! end(array_keys($array)))
echo $val2;
}
echo end(array_value);//must be show 12
}

If I understand correctly, in your if() statement, you're attempting to see if the pointer is at the end of the array. Unfortunately, in this case, end() will never be false and so the line echo $val2; won't ever execute.
Try replacing
if (! end(array_keys($array)))
with
if ($key <> end(array_keys($array))
also your last line should be:
echo end(array_values($array));

Try using below code:
$array = array("Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
foreach($array as $key1 => $val1){
if(is_array($val1)){
echo $key1.'<br/>';
foreach($val1 as $key2 => $val2){
if(is_array($val2)){
foreach($val2 as $k => $v){
echo $v.'<br/>';
}
} else {
echo $val2.'<br/>';
}
}
}
}
echo end(array_values($array));
The result will be:
Language programings
100
200
12

Related

Show All Data list of multidimensional array Using Foreach Loop

Here is The My Array Code...
$data = array(
'data_1',
'data_2',
'data_3',
'data_4',
'data_5' => array(
'data_5_1',
'data_5_2'
)
);
i Want to Ountput Like The : -
data_1
data_2
data_3
data_4
data_5
data_5_1
data_5_2
Here is My Code I try to Self But I Show Error
foreach($data as $da){
echo $da."<br>";
}
Error Found Like This
data_1
data_2
data_3
data_4
Notice: Array to string conversion in filename.php on line 3
Array
Please Fix this problem & Use echo not print_r
This is best achieved with a recursive function so that you can deal with any level of nested arrays:
function display_list($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
echo "$k\n";
display_list($v);
}
else {
echo "$v\n";
}
}
}
display_list($data);
Output:
data_1
data_2
data_3
data_4
data_5
data_5_1
data_5_2
Demo on 3v4l.org
You could use iterators:
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($data)) as $item)
echo "$item<br>", PHP_EOL;
As asked in comments, if you want either the key or value depending on type, you can use the flag SELF_FIRST and the ternary operator:
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($data), RecursiveIteratorIterator::SELF_FIRST) as $key => $item)
echo (is_scalar($item) ? $item : $key) . '<br>', PHP_EOL;
foreach ($data as $val) {
if(is_array($val)){
foreach ($val as $row) {
echo "<br> ".$row;
}
}
else{
echo "<br>".$val;
}
}

How to get position of a value in a php array

<?php
$arr1 = preg_split("/[;]+/", $data);
foreach ($arr1 as $arr1 => $value) {
echo '<td>';
echo $value;
echo '</td>';
if (key($value)==6)
{
echo '</tr>';
echo '<tr>';
}
}
?>
This is code I am using but the key function is not returning anything.
I want to get return the position of $value and want to excecute some code if it is divisible by 6.
How can I get the position of $value in $arr1?
You must use a different variable than $arr1 to store the key.
Use this instead, where $key will be the key:
<?php
$arr1 = preg_split("/[;]+/", $data);
foreach ($arr1 as $key => $value) {
echo '<td>';
echo $value;
echo '</td>';
if ($key==6)
{
echo '</tr>';
echo '<tr>';
}
}
?>
You can following example to get position of particular $value in array.
<?php
$value = 'green';
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key_of_particular_value = array_search($value, array);
//After perform above operation $key_of_particular_value = 2
//Below statement will check whether key is divided by 6 or not
if(($key_of_particular_value % 6) == 0)
{
//perform your operation
}
?>
To know more please refer following link-
http://php.net/manual/en/function.array-search.php
You have used same variable $arr1 for key and original array, try using another variable to get key(index)
foreach ($arr1 as $key => $value) {

PHP Print R Foreach Array, getting a string to post as array

Driving me crazy but here goes:
$cat=2,3,4
$test = print_r(explode(',', $cat), true);
echo ''.$test.'';
foreach ($test as $key => $val) {
echo "$key => $val <br>";
}
What I'm hoping to get:
2
3
4
I'm trying to use a string of numbers obtained from another code to build an array, and then show each value on separate lines. What am I doing wrong?
You shouldn't assign print_r() to $test variable... It's a debugging function for printing purposes..
You should write your code like this..
$cat='2,3,4';
foreach(explode(',',$cat) as $v)
{
echo $v."<br>";
}
This should be either:
$test = explode(',', $cat);
or:
print_r(explode(',', $cat), true);
<?php
$cat='2,3,4';
$array = explode(',', $cat);
$test = print_r($array, true);
echo ''.$test.'';
foreach ($array as $key => $val) {
echo "$key => $val <br>";
}
$test is string. You need array for foreach statement.
Try with this:
<?php
$cat = "2,3,4"; // This is a string separated with commas.
$test = explode(',', $cat); // This assign every number to an array item. The print_r() function is not necesary here.
foreach ($test as $value) {
echo $value. "<br />\n";
}
?>
THIS WILL PRODUCE:
2
3
4
Explanation about your errors:
$cat= '2,3,4'; <- ** Missing quotes **
$test = print_r(explode(',', $cat), true); <- **print_r is not needed here **
echo ''.$test.'';
foreach ($test as $key => $val) { <- Not using it propperly
echo "$key => $val ";
}
You miss ; in $cat variable declaration. Use the following code.
$cat=2,3,4;
$test = explode(',', $cat);
foreach ($test as $key => $val) {
echo $val."<br>";
}

Find last character in multi-dimensional associative array and delete it

I have an associative array in a foreach like this:
foreach ($mArray as $aValue) {
foreach ($aValue as $key => $value) {
echo $html->find($key,$value)
}
}
It gives me this output:
bobby
johnny
Now I would like to get the last character which is y so I did:
echo substr($TheString, -1);
But this gives me: yy because its a multi-dimensional array so it gives me the last characters in each array. What can I do to get the last character on the page y (..and delete it)?
$last_char = '';
foreach ($mArray as $aValue) {
foreach ($aValue as $key => $value) {
if(substr($html->find($key,$value), -1) == 'y'){
$last_char = $html->find($key,$value);
}
}
}
echo $last_char;
This seems to work for me
echo substr_replace($TheString,"",-3);
Try This :
echo substr($TheString, -1, 1);
OR replace a string Removing y
$s="abcdey";
$m=substr($s,0,-1);
echo substr_replace($s,$m,0)
Try This :
$array = array(
"foo" => "jonny",
"bar" => "monny",
);
$i=0;
$con=count($array);
foreach($array as $key => $value)
{
$i++;
if($i==$con)
{
$s=$value;
$m=substr($value,0,-1);
$value=substr_replace($s,$m,0);
echo "Removed Y from array of last item =".$m."</br>";
}
echo $value."</br>";
}

How to echo out the values of this array?

How to echo out the values individually of this array?
Array ( [0] => 20120514 [1] => My Event 3 )
so
echo $value[0]; etc
I have this so far:
foreach (json_decode($json_data_string, true) as $item) {
$eventDate = trim($item['date']);
// positive limit
$myarray = (explode(',', $eventDate, 2));
foreach ($myarray as $value) {
echo $value;
}
This echo's out the whole string no as an array. and if i do this?
echo $value[0};
Then I only get 2 characters of it??
The print_r :
Array ( [0] => 20120430 [1] => My Event 1 )
foreach ($array as $key => $val) {
echo $val;
}
Here is a simple routine for an array of primitive elements:
for ($i = 0; $i < count($mySimpleArray); $i++)
{
echo $mySimpleArray[$i] . "\n";
}
you need the set key and value in foreach loop for that:
foreach($item AS $key -> $value) {
echo $value;
}
this should do the trick :)
The problem here is in your explode statement
//$item['date'] presumably = 20120514. Do a print of this
$eventDate = trim($item['date']);
//This explodes on , but there is no , in $eventDate
//You also have a limit of 2 set in the below explode statement
$myarray = (explode(',', $eventDate, 2));
//$myarray is currently = to '20'
foreach ($myarray as $value) {
//Now you are iterating through a string
echo $value;
}
Try changing your initial $item['date'] to be 2012,04,30 if that's what you're trying to do. Otherwise I'm not entirely sure what you're trying to print.
var_dump($value)
it solved my problem, hope yours too.

Categories