Search multidimensional array by specific key and print the value php - 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

Related

I have only one array inside the index, so i want to move the index to the previous index? Please see the reference below:

Array
(
[data] => Array
(
[0] => Array
(
['degree_level'] => Bachelor's
)
[1] => Array
(
['field_of_study'] => Science
)
[2] => Array
(
['grade_point'] => 3
)
[3] => Array
(
['criteria'] => desired
)
)
)
What I want :
Array
(
[data] => Array
(
['degree_level'] => Bachelor's
['field_of_study'] => Science
['grade_point'] => 3
['criteria'] => desired
)
)
You should use array_flatten(); to achieve your goal like this,
$flattened = array_flatten(Your_Data_Array);
Please give it a try and let me know.
UPDATE
$flattened = array_map(function($item) {
return $item[0];
}, Your_Data_Array);
For more information you can visit this for PHP functions.
Let me know in case of any queries.
$output = array_map(function($item) { return $item[0]; }, $myArray);
Try this,
foreach($data as $key1=>$val1){
foreach($val1 as $key2=>$val2){
$new_array[$key2] = $val2;
}
}
You can do it by loop.
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value2) {
$data[$key1] = $value2;
}
}
You could use for example a double foreach loop to use the key and the value from the second loop and add those to the $arrays["data"] array.
Then you could use unset to remove the nested arrays.
$arrays = [
"data" => [
["degree_level" => " Bachelor's"],
["field_of_study" => "Science"],
["grade_point" => 3],
["criteria" => "desired"]
]
];
foreach($arrays["data"] as $dataKey => $data) {
foreach ($data as $key => $value) {
$arrays["data"][$key] = $value;
}
unset($arrays["data"][$dataKey]);
}
print_r($arrays);
That would give you:
Array
(
[data] => Array
(
[degree_level] => Bachelor's
[field_of_study] => Science
[grade_point] => 3
[criteria] => desired
)
)
Demo
you can achieve this using array_collapse.
Link
EDIT :
while tag has changed.
Here is the core php solution based on Laravel array_collapse:
function collapse($array)
{
$results = [];
foreach ($array as $values) {
if (! is_array($values)) {
continue;
}
$results = array_merge($results, $values);
}
return $results;
}

How can i return all the results from my array?

Array
(
[edit] => true
[id] => 1
[type] => Array
(
[0] => LC
)
[userid] => 1
[norooms] => 1
[park] => Central
[start] => 09:00
[end] => 11:00
[length] => 2
[student] => 79
[status] => Rejected
)
<?php
$posted_data = array();
if (!empty($_POST['edit'])) {
$posted_data = json_decode($_POST['editVal'], true);
}
print_r ($posted_data);
foreach ($posted_data as $key => $value) {
echo '<p>'.$key.'</p>';
echo '<p>'.$value.'</p>';
}
?>
The array at the top is the jason_decode returned. However with my foreach function it does not display the first index of the array within the array. ie. ( [0] => LC ).
Where am I going wrong?
You need to build a recursive function, something like:
function print_recursively(array $array)
{
foreach ($array as $key => $value)
{
if(is_array($value))
{
print_recursively($value);
}
else
{
echo '<p>'.$key.'</p>';
echo '<p>'.$value.'</p>';
}
}
}
Tune it according to your needs.
If you know there is array hierarchy to one level only
Keep printing the values and if the value is an array using is_array.. Iterate again.
foreach($dataArray as $key =>$value){
if(is_array($value)){
foreach($value as $array2Data){
echo $array2Data; //you can use keys as well
}
}
else
echo $value;
}

How to search an array that is an object

I am a little confused about the array_search function (or maybe I am trying to use the incorrect thing.) I have a bunch of transaction objects, (transactions about a customer) each is an array of values. After passing in an email address, I get an object that is one transaction where that email address was used. Example is below. I get it from the command print_r($results):
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 4564564654-65465464565-4654654
[Results] => Array
(
[0] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 5555555555
[email_addy] => someaddy#something.com
)
[1] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 6666666666
[email_addy] => someaddy#something.com
)
[2] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 6666666666
[email_addy] => someaddy#something.com
)
)
)
I get this output with no trouble at all. My issue is that I need to identify when someone has a specific ID. I was trying to use a foreach but I am not getting back what I need. The code and output is below.
foreach ($results as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Output is
Key: OverallStatus; Value: OK
Key: RequestID; Value: 4564564654-65465464565-4654654
Key: Results; Value: Array
All I really need to know is if the customer has an ID of 5555555555. This number will always stay the same. Am I going in the wrong direction here?
You have array of object , so you should get first the array results , and then iterate over it,try this :
foreach ($results->Results as $key => $value) {
if($value->ID == 55555555) echo 'found at position'.$key;//if id is unique , add a break;
}
You need to iterate through $results->Results.
foreach ($results->Results as $key => $value) {
if($value->ID == 5555555555)
print "Match found";
}
You should loop again for results array. Try following foreach
foreach ($results as $key => $value) {
if($key == "Results") {
foreach($value as $v) {
if($v->ID == "5555555555") {
echo "I found you";
break;
}
}
} else {
echo "Key: $key; Value: $value<br />\n";
}
}
Loop through '$results->Results' and check in loop if the ID=5555555555 like this:
foreach ($results->Results as $value) {
if($value->ID == 5555555555) {
//do something
}
}
It would be something like the following:
$transactions = $results->Results; //get results array
foreach ($transactions as $transaction) { // loop through each transaction object.
echo $transaction->ID . '<br />'; // print out the id of each transaction.
}

PHP Going through array?

I have a $search_array like this
Array
(
[1] => Array
(
[type] => book
[search] => steve
)
[2] => Array
(
[type] => book
[search] => john
)
foreach ($search_array as $s) {
$arrayid = //???????
$searchtype = $s['type'];
$search = urlencode($s['search']);
getResult($arrayid);
}
I'm trying to figure out how to get the array number. So for the first result i need $arrayid to be 1. How do I reference that in the foreach loop?
Thanks
Use a foreach with "$key => $value"
Array
(
[1] => Array
(
[type] => book
[search] => steve
)
[2] => Array
(
[type] => book
[search] => john
)
foreach ($search_array as $key => $s) {
$arrayid = $key
$searchtype = $s['type'];
$search = urlencode($s['search']);
getResult($arrayid);
}
Adding $arrayid => in your foreach loop declaration will auto-assign $arrayid with the current array index.
foreach ($search_array as $arrayid => $s) {
// ...
getResult($arrayid);
}
See foreach on PHP Manual.
foreach ($search_array as $arrayid => $s) {
Please read PHP documentation before asking such basic questions
foreach ($search_array as $arrayid => $s) {
// your code here
}
This is a way you could retrieve data from a Php array. It will retrieve all entries from array $value and their key $key.
foreach ($array as $key => $value) {
// $key is the array index
}

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