Parsing an array in PHP after serializeArray() in JS - php

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

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

How can I retrieve data from array

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"

How can I use foreach to process a multidimensional php array?

I parsed a JSON file with json_decode, and the result is a long multidimensional array like the following:
Array
(
[Basic] => Array
(
[0] => Array
(
[text] => Taunt.
[playerClass] => Shaman
[locale] => enUS
[mechanics] => Array
(
[0] => Array
(
[name] => Taunt
)
)
)
)
[Classic] => Array
(
[0] => Array
(
[cardId] => CS2_188o
[name] => 'Inspired'
[mechanics] => Array
(
[0] => Array
(
[name] => OneTurnEffect
)
)
)
)
)
I want to use a foreach to insert the data into a datatable but I can't make it work with this multidimensional array. How would I do that?
You must use recursive array to do it like this
function build($fullArray)
{
foreach ($fullArray as $item) {
if (is_array($item)){
build($item);
}
else{
echo $item["cardId"];
echo $item["name"];
....
}
}
}
Use this:
foreach ($items as $item) {
if (is_array($item)){
foreach ($item as $it) {
echo $it["name"];
}
}
echo $item["cardId"];
echo $item["name"];
....
}

How can I loop through this array in php?

I'm trying to loop through this multidimensional array however it doesn't seem to be working correctly.
Array
(
[*backingData] => Array
(
[data] => Array
(
[0] => stdClass Object
(
[name] => bob
[id] => 1
)
[1] => stdClass Object
(
[name] => bob
[id] => 2
)
[2] => stdClass Object
(
[name] => bob
[id] => 3
)
)
)
)
This is what I have right now:
foreach ($array as $key => $value) {
for ($i=0;$i<=count($value);$i++) {
echo $value[$i]['id'];
}
}
Here a way to do that :
function recursiveArrayLoop ($array) {
foreach ($array as $a) {
if (is_array ($a)) {
recursiveArrayLoop ($a);
} else {
var_dump ($a);
}
}
}
you have to convert your object data to array. use get_object_vars().
foreach ($array as $key=>$value) {
foreach ($value as $key2=>$data) {
$data = get_object_vars($data);
echo $data['id'];
}
}

php array key unset if element is another element in a second array

I have a array named $new_items in the following format, with multiple items....
Array
(
[0] => Array
(
[id] => 66901
[weight] => 0.3000
[Price] => 14.1800
[category] => Array
(
[parent_id] => Array
(
[0] => 222
[1] => 1232
[2] => 1315
)
)
.......and (cont...)
[1] => Array
( ......
I have another array $data in following format:
Array
(
[174] => 67495
[253] => 67471
[278] => 67460
[323] => 67412
[390] => 67332
[600] => 67282
[738] => 67209
)
I wish to remove the parent in $new_items array where id is present in $data array.
My code is as follows:
foreach ($new_items as $key => $item) {
if ($item['id'] === $data) {
unset($new_items[$key]);
}
}
is not removing the parent.
But if i hardcode the id as follows:
if ($item['id'] === "67495"),
It is getting removed..
Please help me...
$data is an array, so you can't compare it with integer value! Use in_array instead..
foreach ($new_items as $key => $item) {
if (in_array($item['id'], $data)) {
unset($new_items[$key]);
}
}
foreach ($new_items as $key => $item)
{
if(array_search($item['id'], $data))
{
$key = array_search($item['id'], $data);
unset($data[$key]);
}
}

Categories