PHP Array - obtaining vlaues - php

I have an array, the output of print_r is:
Array ( [0] => Array ( [ID] => 1 [col1] => 1 ) [1] => Array ( [ID] => 2 [col1] => 2 ) )
Could you help of kind of array it is? So that I could research more about it? What I want is to get ID and col1 values
I've tried to use foreach:
foreach($array_name as $key=>$value){
print "$key holds $value\n";
}
The output I get is 0 holds Array 1 holds Array
And I would simply like to get:
1 1
2 2

It's a multi dimensional array, or an array where each element is another array. So you'll need to loop twice. Try this to look at it:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$key = $val\n";
}
}
Or, to get your just added desired output, do this:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$val ";
}
print "\n";
}
Or this:
foreach($array_names as $arr)
{
print $arr['ID'] . " " . $arr['col1'] . "\n";
}
or a few other ways but you should be getting the picture.

Related

How can I Check the current value which is already passed or not in an array in nested foreach in php

My array
$key1=>
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 11
[4] => 12
[5] => 17
[6] => 18
)
$_POST['name']=>
Array
(
[0] => General
[1] => General
[2] => Outdoors
[3] => Dining
[4] => Kitchen
)
Here is my code,
foreach ($key1 as $key => $value) {
// echo $value;
foreach ($_POST['name'] as $key => $value1) {
//echo $value;
$subQueryCond .=' AND '.$value1.' LIKE ' .$value ;
}
}
While my Ajax calls this nested loop occurs..
Inside this I wrote a query..
If one value is passed.
The query is in the format of AND 'General' LIKE 1.
And if another value is passed in the $key1 it pass the query two times.
It's like How many arrays are given that much time that query was passed..
So,here I would like to restrict the $value if it already came..
if two values were given,it pass the query in the following manner
AND General LIKE 1
AND Outdoors LIKE 1
AND General LIKE 7
AND Outdoors LIKE 7
And my desired query must be in the form of
AND General LIKE 1
AND General LIKE 7
AND Outdoors LIKE 7
can someone help me..
Use this code to remove duplicates from an array:
$uniqueKey1 = array_unique($key1);
And then you regular code:
foreach ($uniqueKey1 as $key => $value) {
// echo $value;
foreach ($_POST['name'] as $key => $value1) {
//echo $value;
$subQueryCond .=' AND '.$value1.' LIKE ' .$value ;
}
}
This will work for you...
<?php
$subQueryCond= '';
foreach ($key1 as $key => $value)
{
foreach ($_POST['name'] as $key => $value1)
{
$subQueryCond['AND '.$value1.' LIKE ' .$value] = ' AND '.$value1.' LIKE ' .$value ;
}
}
echo "<pre>"; print_r($subQueryCond);
$query = implode('',$subQueryCond) ;
print_r($query);
?>
just make an array with unique keys to value, then use implode() function to make query string...

I want to loop 2d array in PHP

here is my code
echo '<pre>';
print_r($header);
echo '</pre>';
and here is the output
Array
(
[1] => Array
(
[A] => Name
[B] => rollnumber
[C] => class
[D] => marks
)
)
I want to read the all array like I want to display
Name rollnumber class marks in table td using loop any help? thanks
you can try this:
foreach($header as $key => $value){ // start $header array loop
if(is_array($value)){ // check if the $value is another array
foreach($value as $k => $v){ // start $value array loop
echo $v.'<br>'; // output $v 'Name rollnumber class marks'
}
}else{
echo $value.'<br>'; // in your case $value is array but you will be able to use like array(1=>array(), 2=>string) the 2 will be the output without a foreach loop
}
}

Get items in multidimensional array separately

I have an array ($myArray) like this:
print_r(array_values ($myArray));
result: Array (
[0] =>
[1] => Array (
[id] => 1
[name] => ABC
)
[2] => Array (
[id] => 2
[name] => DEF
)
)
I'm trying to get each ID and NAME.. So Im trying this:
foreach ($myArray as $value) {
foreach($value as $result) {
echo $result;
}
}
I'm facing two problems:
I get a PHP WARNING that says: " Invalid argument supplied for foreach() on line 29
This line is: foreach($value as $result) {
I would like to get keys to ID and NAME to place them in correct places. This ways echo "1ABC" and "2DEF"
Any idea? Thanks for helping.
Basically, the error triggered, since the array in your example (index zero in particular) is not an array (most likely an empty string/null ) which is being used inside foreach.
Since one of the elements is not an array, you could just check that if its an array or not using is_array():
foreach($myArray as $values) {
if(is_array($values)) {
echo $values['id'] . ' ' . $values['name'] . '<br/>';
}
}
Alternatively, you could also use array_filter() in this case which in turn removes that empty index zero, so that you could just use that loop that you have. You wouldn't have to check for that empty element.
$myArray = array_filter($myArray);
foreach ($myArray as $value) {
foreach($value as $result) {
echo $result;
}
}
try this,
foreach (array_slice($myArray ,1) as $value) {
foreach($value as $result) {
echo $result;
}
}
the 1st position is empty so omit first position

Search multidimensional array by specific key and print the value php

I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
Example: key['user_id'] this will print all user_id from array. is it possible? please help me thanks
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[user_id] => 1
)
[1] => Array
(
[user_id] => 2
)
)
[Post3] => Array
(
[0] => Array
(
[user_name] => 1
)
)
)
Here is my PHP code:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
You can print_r to achive the same results you want with your triple for each.
I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
You can easily use recursion for such a problem. You can use something along the lines of:
function printValuesByKey($array, $key) {
if (!is_array($array)) return;
if (isset($array[$key]))
echo $key .': '. $array[$key] .'<br>';
else
foreach ($array as $v)
printValuesByKey($v, $key);
}
In your example:
printValuesByKey($array, 'user_id');
will print:
user_id: 1
user_id: 2

Question related to array

I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";

Categories