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

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>";
}

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;
}
}

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>";
}

php doesn't fetch end of array

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

how do I echo out an array array using foreach?

i am trying to automate my navigation links. How do I automatically echo out foreach. I am getting undefined offset right now... And can I ignore the first item in the array (i.e. title)?
'control' => array( 0=>'Controls',
1=> array('Add school','add.school.php'),
2=> array('Add doctor','add.doctor.php'),
3=> array('Add playgroup','add.play.php'),
4=> array('Suggestions','suggestion.php'),
5=> array('List tutor service','list.tutor.php'),
6=> array('Create playgroup','create.play.php'),
7=> array('Dashboard', 'dashboard.php')
),
<?php
foreach ($nav['control'] as $value=>$key){
echo''.$key[1].'';
}
?>
Numeric arrays are indexed from 0, not 1. You want [1] and [0] respectively.
// for key => value is more nature.
foreach ($nav['control'] as $key => $value){
// should skip the first.
if ($key === 0) {
continue;
}
// array is 0 base indexed.
echo''.$value[0].'';
}
foreach ($nav['control'] as $value=>$key) {
echo''.$key[0].'';
}
a nested array requires nested loop.
foreach($array as $key => $value){
if($key != 0){
foreach($value as $k => $v){
if($k == 0){ $title = $v;}
if($k == 1){ $link = $v;}
}
//put code to run for each entry here (i.e. <div> tags, echo $title and $link)
}
my personal practice when i only use two fields is to push the first into the array['id'] and the second as the value i.e.
while($row_links = sth->fetch (PDO::FETCH_ASSOC)){
$array[$row_links['title']] = $row_links['link'];
}
then you can use
<?php foreach($array as $key => $value){ ?>
<?php echo $key; ?>
<?php } ?>

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