How can I retrieve data from array - php

Am a beginner in yii2. In my program there is an array named Place. It contains the values like:
Array
(
[0] => Array
(
[0] => Aluva
[1] => Paravoor
)
[1] => Array
(
[0] => Ernakulam
[1] => Paravoor
)
[2] => Array
(
[0] => Aluva
[1] => Ernakulam
)
[3] => Array
(
[0] => Kottuvally
[1] => Paravoor
)
)
How can I retrieve each element from this array?

You can use the array_walk function
$total = [];
array_walk($sales, function($value) use(&$total) {
foreach ($value as $key => $arr) {
echo $arr. "<br>";
}
});

using foreach ?
foreach($array as $row){
foreach($row as $key => $val){
echo $val.'<br>';
}
}

Sample code -
<?php
$data = array(
0 => array(0=>'Aluva', 1=>'Paravoor'),
1 => array(0=>'Ernakulam', 1=>'Paravoor'),
2 => array(0=>'Aluva', 1=>'Ernakulam'),
3 => array(0=>'Kottuvally', 1=>'Paravoor')
);
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value1) {
echo '<br>'.$value1;
}
}

$var = $Place[0][1];
Where is $var is now "Paravoor"

Related

How to get array with same key into one in php

I would like to get the array with same value into one.
This is the array I have
Array
(
[0] => Array
(
[id] => 6
[name] => role
)
[1] => Array
(
[id] => 5
[name] => role
)
[2] => Array
(
[id] => 3
[name] => category
)
[3] => Array
(
[id] => 4
[name] => category
)
)
This is what I want to achieve.
Array
(
[0] => 5,
[1] => 6
)
Array
(
[0] => 4,
[1] => 3
)
This is my code
$result = array();
foreach ($items as $key => $value) {
$name = $value['name'];
$result[$name] = array($value['id']);
}
foreach($result as $key => $val){
print_r($val);
}
What I am getting is
Array (
[0] => 5
)
Array (
[0] => 4
)
Can anyone here to help me for solving this? Any help really
appreciated.
Thanks.
$result = array();
foreach ($items as $key => $value) {
$name = $value['name'];
if (!isset($result[$name])) {
$result[$name] = [];
}
$result[$name][] = $value['id'];
}
print_r($result);
Try like this
$result=[];
foreach ($items as $value) {
$result[$value['name']][] = $value['id'];
}
print_r($result);

Parsing an array in PHP after serializeArray() in JS

I receive this array in my PHP page after form serializeArray() done on a form in Javascript:
Array
(
[datas] => Array
(
[0] => Array
(
[name] => room_1
[value] => a
)
[1] => Array
(
[name] => room_2
[value] => b
)
[2] => Array
(
[name] => room_3
[value] => c
)
)
)
How can I parse it after during a foreach loop ?
Thanks.
I tried:
foreach ($datas as $key => $item) {
echo $item;
}
foreach ($array['datas'] as $item) {
echo $item['name'];
echo $item['value'];
}
If the name of the array is $array,
$arr = $array['datas'];
foreach($arr as $key => $val){
$name = $val['name'];
$value = $val['value'];
}
}

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

Manipulate multidimensional array with php

I have the following array:
$arr = array(
array("2014-03-13"=>array("a"=>1,"b"=>2)),
array("2014-03-12"=>array("a"=>4,"b"=>3))
);
And I would like the change the way it looks to something more like this.
$arr = array(0=>array("date"=>"2014-03-13","a"=>1,"b"=>2),
1=>array("date"=>"2014-03-12","a"=>4,"b"=>3));
Heres what I have so far.
$keys = array();
$vals = array();
foreach($arr as $row){
foreach($row as $key=>$val){
$keys[]=array("date"=>$key);
foreach($val as $keys=>$values){
$vals[]=array($keys=>$values);
}
}
}
The array which gets the dates works fine so in the below example the $keys array works however the $vals array does not work as intended and instead gives me an array similar to this.
Array ( [0] => Array ( [a] => 1 )
[1] => Array ( [b] => 2 )
[2] => Array ( [a] => 4 )
[3] => Array ( [b] => 3 )
[4] => Array ( [a] => 4 )
[5] => Array ( [b] => 3 ) )
Any help to get the array desired is appreciated.
It should be
$result = array();
foreach($arr as $row) {
foreach($row as $date=>$values) {
$values['date'] = $date;
$result[] = $values;
}
}
$arr = array(
array(
"2014-03-13" => array("a"=>1, "b"=>2)
),
array(
"2014-03-12" => array("a"=>4, "b"=>3)
),
);
$result = array();
foreach ($arr as $item) {
foreach ($item as $date => $values) {
$result[] = array_merge(array('date' => $date), $values);
}
}
var_dump($result);
Here is the code:
$arr = array(
0=>array("2014-03-13"=>array("a"=>1,"b"=>2)),
array("2014-03-12"=>array("a"=>4,"b"=>3))
);
$finalarray=array();
foreach($arr as $k=>$v){
foreach($v as $kk=>$vv){
$newarray=array();
$newarray["date"]=$kk;
foreach($vv as $kkk=>$vvv){
$newarray[$kkk]=$vvv;
}
}
$finalarray[]=$newarray;
}
echo "<pre>";
print_r($finalarray);
echo "</pre>";
**Here is the output:**
Array
(
[0] => Array
(
[date] => 2014-03-13
[a] => 1
[b] => 2
)
[1] => Array
(
[date] => 2014-03-12
[a] => 4
[b] => 3
)
)

2-dimensional Array - unset if not value

I have a 2-dimensional array and want to delete all elements, whose values are not "Name1". They should keep their index numbers (keys):
Array
(
[array001] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name1
)
[array002] => Array
(
[0] => Name2
[1] => Name1
[2] => Name4
)
[array003] => Array
....
)
will output
Array
(
[array001] => Array
(
[0] => Name1
[3] => Name1
)
[array002] => Array
(
[1] => Name1
)
[array003] => Array
....
)
Possible solutions could be achieved with a foreach loop, with preg_replace, when the array is converted into a string: $array = print_r($array,true);
none of them is working..
I found the solution by myself:
foreach($array as $key => $value) {
foreach($value as $innerkey => $innervalue){
if($innervalue != 'Name1'){
unset($array[$key][$innerkey]);
}
}
}
foreach($array as $key => $value) {
foreach ($value as $string) {
if ($string !== "Name1") {
unset($string);
}
}
}
try this:
function removeElementDifferentValue($array, $value){
foreach($array as $subKey => $val){
if($val != $value){
unset($array[$subKey]);
}
}
return $array;
}
$array = removeElementWithValue($array, 'Name1');
foreach($array as $key1 => $val1) {
foreach($val1 as $key2 => $val2) {
if(strcmp($val2,"Name1") != 0) {
unset($array[$key1][$key2]);
}
}
}

Categories