PHP Print array values without all the extra "=>" stuff - php

I have an array and I want to simply print out a list of all the values. Not sure why I can't find the answer to this. I have tried "var_dump" and "Var_export", "TRUE" and "FALSE". Here is my code:
$var = var_export($xyz,TRUE);
print "$var";
But it outputs this:
array (
0 => '700',
1 => '750', etc
I just want this:
700
750

<?php
$a = array (700, 750);
foreach($a as $key => $value)
{
echo $value. "<br>";
}
?>
You can easily print the value of an array using the foreach loop. Here I gave an example for your better help.

foreach ($xyz as $val) {
echo $val . '<br>';
}
?

Have you tried iterating into $xyz using a foreach loop ?
foreach($xyz as $value) {
print "$value \n" }
The \n being a new line.
edit : identical to #PHPNoob's answer

Looks like you are attempting to get the values from an array.
Using a Foreach will loop through the array apply the code to each value until the array is complete.
foreach($xyz as $value){
echo $value . "<br/>";
}

Related

How to print PHP array values without square brackets and symbols?

I want to print an array without printing the square brackets and the word "Array", for example if I do
print_r($Array);
I will get this:
Array ( [0] => Example0 [1] => Example1)
How can I get this?
Example0
Example1
Any of these ways should work just fine.
// First way
print_r(implode("<br>", $your_array));
// Second way
for ($i = 0; $i < count($your_array); $i++) {
print_r($your_array[$i]);
echo "<br>";
}
// Third way
foreach ($your_array as $value) {
print_r($value);
echo "<br>";
}
The first method works for one-dimensional arrays only. If you have multidimensional arrays, you need to use for loops and to check whether the current element is an array or not and recursively enter into more for loops in order to print out all the data.
You can do it in this way:
function print_array ($array) {
foreach ($array as $key => $value) {
if (is_array ($value)) {
print_array ($value);
} else {
echo ($value."<br />");
}
}
}
You could use array walk recursive
$array = ['Example0','Example1', ['Example2']];
array_walk_recursive($array,function($item,$key){echo"$item\n";});
// tip use <br> instead of \n for HTML
Outputs
Example0
Example1
Example2
See it online
array_walk_recursive — Apply a user function recursively to every member of an array
So this will seamlessly handle multi-dimensional arrays, as my example shows.
If I understood correctly, you want to print the values for each key. You can use
foreach ($Array as $value) {
print_r($value);
echo "\n";
}
This will result in
Example0
Example1
foreach($Array as $key) {
echo $key.", ";
}

PHP two dimensional array array search

Please help me with the code to search for a particular value in a two-dimensional array and print the values returned in PHP. Thanks in advance.
in the below array i want to search for value 15 and print 12
code:
$speed = array
(
array(5,4),
array(10,8),
array(15,12),
array(20,16),
array(25,20),
array(30,24),
array(35,28),
array(40,32),
array(45,36),
array(51,40),
array(56,44)
);
foreach ($speed as $key => $val)
{
}
foreach ($speed as $key => $val)
{
if($val[0] === 15)
{
echo $val[1];
}
}
$val is an array so first index is your search and second is your value.
When you browse the array, a row is an array in your input. So if you want to check for 15 and to print 12, you have to do :
// For each row of my array $speed, I have an array that I will call $arr
foreach($speed as $arr){
if(15 == $arr[0]) {
echo $arr[1];
}
}
you can use in_array
loop through the array and search for the value in the array using in array
foreach ($speed as $key => $val)
{
echo (in_array(15, $val)) ? $val[1] :NULL;
}

Foreach only showing last item in array [duplicate]

This question already has answers here:
Array Foreach Loop Prints Last Item Only [closed]
(4 answers)
Closed 9 months ago.
I'm attempting to make a foreach loop to iterate over each item in an array, but it only captures the last item and does not iterate over the first one. I've stripped down the code to only show the relevant parts and added some commands to identify the problem as described above.
$message == "kk,ll";
$myArray = explode(',', $message);
print_r ($myArray);
foreach ($myArray as $value);
{
echo "$value <br>";
$array[] = $value;
}
print_r ($array);
The output is:
Array ( [0] => kk [1] => ll ) ll
Array ( [0] => ll )
You can see that when I use print_r() the array contains two items. But the foreach loop only loops over the last item. Adding the array elements into a new array inside the loop also ends up with an array containing only the last element. What am I doing wrong?
You have two mistakes in you code:
In your first line you have two equal signs which should only be one.
In your foreach loop, you have by mistake put an semicolon at the end:
foreach ($myArray as $value);
Doing this, the foreach loop will run, but the code inside the {} is actually placed outside the foreach loop, and thereby causing $value only to store the last element of the array.
The code should look like this:
$message = "kk,ll";
$myArray = explode(',', $message);
print_r ($myArray);
foreach ($myArray as $value) {
echo "$value <br>";
$array[] = $value;
}
print_r ($array);
your foreach just assigned the $value, but output nothing. This is caused by the ; after foreach, same as
foreach ($myArray as $value)
{}
And after this, the $value have the last element of $myArray, then
{
echo "$value <br>";
$array[] = $value;
}
only output the last element.
remove the ; after the Foreach like in the follow code
foreach ($myArray as $value)
{
echo "$value <br>";
$array[] = $value;
}
In Laravel Framework Use Code into Controller:
$dd = $categories->pluck( 'title' )->toArray();
foreach ( $dd as $key => $value ) {<br />
$array[$key] = '.' . $value;<br />
} <br />
$cat = implode( ',' , $array );
<br />
// Result Display : James,Mark,Helmet.....
Only remove the semicolon after foreach ($myArray as $value) or used it

Duplicate array values not working with Simple HTML DOM

I'm working with Simple HTML DOM like this:
foreach($html->find('img', 18) as $d) {
echo $d->outertext;
}
Now I want to implement an array of variables, in this case images, so I did:
$img=array(
"img"=>"18",
"img"=>"21"
);
foreach($img as $x=>$x_value)
{
$d = $html->find($x, $x_value);
echo $d->outertext;
}
The problem is that Simple HTML DOM is only returning the last image in array, which is number 21. What do I have to do to make it return everything in the array?
It's because both items in your $img array has the same key. foreach doesn't recognize them as two seperate items because both keys are img.
Example code to demonstrate:
$test = array(
"key" => 1,
"key" => 2
);
echo "Length of array: " . count($test) . "\n\n";
echo "Items in array:\n";
foreach($test as $key => $value) {
echo "$key => $value\n";
}
Outputs:
Length of array: 1
Items in array:
key => 2

displaying the content of an array which is composed of two other arrays

i have an array like this :
$finalArray= array($array1, $array2);
later, how should i do to display the content of $finalArray in a loop without knowing the name of the columns of $array1 and $array2 ? thx in advance :)
EDIT
i.e : to display the content of a simple array we do something like that :
foreach($array1 as $value){
echo $value['the_name_of_the_columns']."<br />";
}
how about an array of arrays :), i'm supposed to having to know the name of columns, i need to it independently on the columns names .
use foreach....
echo "first: <br/>";
foreach ($finalarray[0] as $value) {
echo "Value: $value<br />\n";
}
echo "second: <br/>";
foreach ($finalarray[1] as $value) {
echo "Value: $value<br />\n";
}
EDIT :
documentation on foreach:
http://php.net/manual/en/control-structures.foreach.php

Categories