Foreach with array in php - php

How to make a foreach with this type of array please ?
Thanks.
Array (
[0] => Array (
[0] => TPK12\"
[1] => MRP59\"
[2] => MSM105\"
[3] => RGS70\"
[4] => GDN36\"
)
)
I tried that but it echoes just Array():
foreach($match as $value) {
echo $value;
}

For a two-dimensional array you can use this:
foreach($match as $subArray) {
foreach($subArray as $value) {
echo $value;
}
}

Since you have a multidimensional array, you need to either have a foreach inside another, or specify which array you want to iterate.
foreach ($match[0] as $value) {
echo $value;
}

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

Print out JSON with multiple arrays

I have this JSON string that has been converted into a PHP array:
Array (
[textfield] => Array (
[elements] => Array (
[0] => Array (
[type] => textField
)
)
[title] => textfield
)
[textarea] => Array (
[elements] => Array (
[0] => Array (
[type] => textArea
)
)
[title] => textarea
)
) textfield
And I'm trying to loop through it and print out the type and title of each array. This is what I have so far:
foreach($inputs as $key => $jsons) {
foreach($jsons as $key => $value) {
echo $value;
}
}
But that only prints out the title. Note, I do actually need to loop through the array and get all the values because I need to use them, I know I could use print_r to just dump the array but that's not what I need!
Here's an easy way... but without seeing more of what you're trying to do, who knows if this will even work.
foreach($json as $key => $value) {
$elements = $value['elements'];
foreach($elements as $key => $element) {
echo "$key = {$element['type']}\n";
}
$title = $value['title'];
echo "$key = $title\n";
}
foreach($inputs as $key => $jsons) {
foreach($jsons as $key1 => $value) {
if( $key1 == "title" ) {
echo "TITLE :-".$value;
} else if( is_array($value) {
foreach($value as $key2 => $value2) {
echo "Type :".$value;
}
}
}
}

Foreach loop through multidimentional array like this

I have a multidimensional array in variable $comments, containing:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => appr
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => appr
)
// There is more...
)
How do I loop through this array and echo each value using for each?
foreach($arrayOfArrays as $array){
foreach($array as $index => $value){
echo $array[$index];
}
}
You should use two foreach loops as your array has to levels :
foreach ($comments as $comment)
foreach ($comment as $comment_data)
echo $comment_data;
If your array structre stay like the one you show, you can do this like follow :
foreach($comments as $comment) {
echo $comment[0];
echo $comment[1];
echo $comment[2];
echo $comment[3];
}
Just use two foreach loops. One inside the other
foreach($comments as $commentArray){
foreach($commentArray as $comment){
echo $comment;
}
}
Hope this helps you
$i=0;
$c=count($array);
while ($i<$c) {
foreach ($array[$i] as $comment_property) {
echo $comment_property;
}
$i++;
}

Array value and index migration

My array is like:
Array
(
[0] => Array
(
[0] => "name"
[1] => "zxczxc5"
)
[1] => Array
(
[0] => "about"
[1] => "zxczxc"
)
[2] => Array
(
[0] => "contact"
[1] => "zxczxc"
)
)
I want to generate another array like this :
Array
{
['name']="zxczxc5";
}
Array
{
['contact']="zxczxc";
}
Array
{
['about']="zxczxc";
}
I want the first array index zero value goes as the index of second value in my new array.
Thanks.
There are many ways to solve what you want to achieve, this is just one of those:
foreach ($array as &$pair) {
$pair = call_user_func_array('array_combine', $pair);
}
unset($pair);
print_r($array);
It makes use of array_combine.
Assuming you name your first Array $aTest:
foreach($aTest as $aElement)
{
$aNewArray[$aElement[0]] = $aElement[1];
}
print_r($aNewArray);
foreach ($array as $value) {
$newArray[$value['0']] = $value['1'];
}
Assuming the first array is called $array
$new_array = array();
foreach($array as $element)
{
$new_array[] = array($element[0] => $element[1]);
}
$newArr = array();
foreach($arr as $val) {
$newArr[$val[0]] = $val[1];
}

Categories