How to parse nested Array with Laravel - php

Hey guys i am new of the laravel, how can i parse this output array? This is my array is coming from repeater using jquery.
Array
(
[tour_baslik] => 1. Day
[tour_icerik] => content here....
[lunch] => Array
(
[0] => 2
)
[dinner] => Array
(
[0] => 1
[1] => 2
)
)
Array
(
[tour_baslik] => 2.Day
[tour_icerik] => content 2 here...
[lunch] => Array
(
[0] => 1
[1] => 2
)
[dinner] => Array
(
[0] => 2
)
)
I need parse like that but i'm stuck:
foreach($myarray as $key => $data){
echo $key . '-' . $data; }
Output must be:
tour_baslik - 1.day
tour_icerik - content here..
lunch - 2
dinner - 1,2

If you need to iterate through all items of your input, you could use a recursive function like the following:
function iterator($key, $value) {
if (is_array($value)) {
foreach ($value as $key => $value) {
iterator($key, $value);
}
} else {
echo !empty($key) ? "$key - " : "";
echo $value."\n";
}
}
iterator(null, $input);

Related

php - check multidimensional array for values that exists in more than 1 subarray

Following simplified multidimensional array given:
$input = Array
(
[arr1] => Array
(
[0] => JAN2016
[1] => MAI2013
[2] => JUN2014
}
[arr2] => Array
(
[0] => APR2016
[1] => DEC2013
[2] => JUN2014
}
[arr3] => Array
(
[0] => JAN2016
[1] => MAI2020
[2] => JUN2022
}
)
I want to check for elements, that exists in more than 1 subarray. An ideal output would be:
$output = Array
(
[JUN2014] => Array
(
[0] => arr1
[1] => arr2
)
[JAN2016] => Array
(
[0] => arr1
[1] => arr3
)
)
I'm currently stucked in a nested foreach because i need to look all silblings of the outer foreach and don't know how to accomplish that.
foreach($input as $k=>$values)
{
foreach($values as $value)
{
//check if value exists in array k+1....n
//if true, safe to output.
}
}
You are almost all the way there
$new = [];
foreach($input as $k=>$values) {
foreach($values as $value) {
$new[$value][] = $k;
}
}
The $new array should look just as you want it
Extended solution which filters subarrays:
$newArray = [];
foreach($input as $k=>$values)
{
foreach($values as $value)
{
$newArray[$value][] = $k;
}
}
print_r(array_filter(
$newArray,
function($v) { return 1 < count($v); }
));
Sample fiddle here.

Replace every string in multidimensional array if conditions matched

Ok so I have an array look like this,
Array
(
[0] => Array
(
[0] => order_date.Year
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date.Quarter
[1] => =
[2] => 1
)
)
What I want to do is, in any element of this multidimensional array I want to replace any string that have a . with removing everything after .
So the new array should look like this,
Array
(
[0] => Array
(
[0] => order_date
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date
[1] => =
[2] => 1
)
)
I have tried doing this,
foreach ($filter as $key => $value) {
if(is_array($value)) {
$variable = substr($value[0], 0, strpos($value[0], "."));
$value[0] = $variable;
}
}
print_r($filter);
I'm getting $value[0] as order_date but can't figure out how to assign it to $filter array without affecting other values in array;
The $value variable is not linked with the original array in the foreach loop.
You can make a reference to the original array by using ampersand "&"
foreach ($filter as $key => &$value) { ... }
Or you can use old school key nesting
$filter[$key][0] = $variable;
Please take a look here https://stackoverflow.com/a/10121508/9429832
this will take off values after . in every element of any multidimensional array.
// $in is the source multidimensional array
array_walk_recursive ($in, function(&$item){
if (!is_array($item)) {
$item = preg_replace("/\..+$/", "", $item);
}
});

Extract values only from an array to encode them in JSON using PHP

This has probably been asked many times but I can't find a solution for my case.
This is my array :
$request=Array (
[0] => Array ( [staName] => Auditorium Stravinsky 2m2c )
[1] => Array ( [staName] => Geneva Arena )
[2] => Array ( [staName] => Les Docks )
[3] => Array ( [staName] => Kheops )
)
And i need an output as follows as JSON:
"Auditorium Stravinsky 2m2c ","Geneva Arena","Les Docks","Kheops"
My current code is as follows:
foreach($request as $value)
{
$names[]=$value;
}
$jsonValue = json_encode(array_values($names));
print_r($jsonValue);
And my current output is as follows in JSON format:
[{"staName":"Auditorium Stravinsky 2m2c "},{"staName":"Geneva Arena"},{"staName":"Les Docks"},{"staName":"Kheops"}]
How can i stop "staName " from being outputed?
Many thanks in advance and please be considerate of my post as this is only the second one I make on this site.
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ) ,
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$newArray=array();
for($i=0;$i<count($request);$i++){
$newArray[$i]=$request[$i]['staName'];
}
$newArray=json_encode($newArray,true);
print_r($newArray);
And the output is a merged json:
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
You can achieve this from,
Code
$a = array();
foreach($request as $key =>$val){
foreach($val as $k => $v){
$a[] = $v;
}
}
print_r(json_encode($a));
Check this Demo link
Output
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
fist of all your array definition are not correct. and your output is simple string not a JSON
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ),
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$name = '';
foreach($request as $value)
{
foreach($value as $value2)
{
$name = $name . ' ' . $value2;
}
}
echo $name;
Output
Auditorium Stravinsky 2m2c Geneva Arena Les Docks Kheops

How do I get the array values from two arrays by using keys

My arrays are
$name=>
Array
(
[0] => General
[1] => General
[2] => Outdoors
[3] => Dining
[4] => Dining
[5] => Kitchen
[6] => Kitchen
)
$key1=>
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 11
[4] => 12
[5] => 17
[6] => 18
)
Array function
foreach ($key1 as $key => $value1) {
foreach ($name as $key => $value) {
echo $value "=>" $value1 ;
//echo "$value1";
}
}
Here I would like to print the values by using the same keys
if $name having the index as [0] and my $key1 also take the [0] value
i.e: my result should be in the form of
General => 1
General => 2
Outdoors => 7
Dining => 11
Dining => 12
Kitchen => 17
Kitchen => 18
You only need to iterate one array, not both of them:
foreach ($name as $key => $name_value) {
echo "$name_value => " . $key1[$key];
}
You can use a simple for loop to do this
for ($i = 0; $i < count($name); $i++) {
echo $name[$i] . '=>' . $key[$i]
}
The problem with your code is you're using the same variable $key for both foreachs, so the last one overwrites the value.
foreach ($key1 as $key => $value1) {
foreach ($name as $key => $value) {
echo $value "=>" $value1 ;
//echo "$value1";
}
}
You could make things easier by combining those two arrays, making $name array be the keys and $key1 array be the values
$newArray = array_combine($name, $key1);
foreach ($newArray as $name => $key) {
echo "{$name} =>{$key}";
}
This will work for you
<?php
$a1= array('General','Outdoors','Dining ');
$a2= array('1','2','3');
$newArr=array();
foreach($a1 as $key=> $val)
{
$newArr[$a2[$key]]= $val;
}
echo "<pre>"; print_r($newArr);
?>
output
Array
(
[1] => General
[2] => Outdoors
[3] => Dining
)
I am afraid this wont be possible if you want the output as associative array as same key name in an associative array is not allowed. It would be always overwritten if you are dealing with the associative arrays.
Although you may have something like this:
array_map(function($key, $val) {return array($key=>$val);}, $name, $key1)
Output:
Array ( [0] => Array ( [General] => 1 ) [1] => Array ( [General] => 2 ) [2] => Array ( [Outdoors] => 7 ) [3] => Array ( [Dining] => 11 ) [4] => Array ( [Dining] => 12 ) [5] => Array ( [Kitchen] => 17 ) [6] => Array ( [Kitchen] => 18 ) ).
But if you want the output in string format It is possible.
for ($i = 0; $i < count($key); $i++) {
echo $name[$i] . '=>' . $key[$i].'<br>';
}
Just change the foreach as follows...
foreach ($key1 as $key => $value1) {
echo $name[$key] ."=>". $value1."<br>";
}
replace the <br> with \n if you're running through the linux terminal. Also don't miss the '.' operator to concatenate the string..
Nested foreach won't do what you need... Good luck..

Output two-dimensional array

Ok, I'm still struggling with my arrays... I have created a two-dimensional array and saved into a session to get results on another page: $_SESSION['myARRAY']
print_r($_SESSION['myARRAY']);
// will output:
Array ( [Car] => Array ( [0] => 1 [1] => 9 [2] => 0 )
[Truck] => Array ( [0] => 2 [1] => 10 [2] => 0 )
[Bus] => Array ( [0] => 1 [1] => 8 [2] => 2 ))
Now I need to output the data into the following format:
$xls->addRow(Array("Car",1,9,0));
$xls->addRow(Array("Truck",2,10,0));
$xls->addRow(Array("Bus",1,8,2));
I tried to do something like this:
foreach($_SESSION['myARRAY'] AS $key => $value) {
$arr[$key] = $key;
foreach($value AS $k => $v) {
$arr[$key] = $v;
}
$xls->addRow($arr[$key]);
}
but it did not really work. I think I'm close but not quite there...
$value is already an array. Now you only need to prepend the $key to it. You could use array_unshift:
foreach($_SESSION['myARRAY'] AS $key => $value) {
array_unshift($value, $key);
$xls->addRow($value);
}
Of course if you do this more than once, you should consider storing the consolidated array.
I'd probably use array_unshift as it seems like a more appropriate way to solve this problem, but you could also do it like:
foreach($_SESSION['myARRAY'] AS $key => $value) {
$xls->addRow(array_merge(array($key), $value));
}

Categories