make inline result from array value - php

I have two array as bellow :
First array :
Array
(
[0] => Array
(
[0] => Array
(
[name] => one
[number] => 051
)
[1] => Array
(
[name] => two
[number] => 052
)
[2] => Array
(
[name] => three
[number] => 053
)
)
[1] => Array
(
[0] => Array
(
[name] => four
[number] => 061
)
[1] => Array
(
[name] => five
[number] => 062
)
)
)
I want to make output from first array above
[0] => 051, 052, 053.
[1] => 061, 062.
Array
(
[0] => Array
(
[0] => Array
(
[name] => book
[number] => 41
)
[1] => Array
(
[name] => pencil
[number] => 42
)
)
[1] => Array
(
[name] => eraser
[number] => 71
)
)
I want to make output from second array above
[0] => 41, 42.
[1] => 71.
Please advise. Thank you.

You can make a try like this way with two foreach() loop.
$numbers = [];
foreach ($array as $k => $v) {
$num = [];
foreach ($v as $k2 => $v2) {
$num[] = $v2['number'];
}
$numbers[$k] = implode(',',$num).'.';
}
print_r($numbers);
DEMO: https://3v4l.org/mEeO7

you can try something like this
$arr = Array (
Array (
Array (
"name" => "one",
"number" => "051"
),
Array (
"name" => "two",
"number" => "052"
),
Array (
"name" => "three",
"number" => "053"
)
),
Array (
Array (
"name" => "four",
"number" => "061"
),
Array (
"name" => "five",
"number" => "062"
)
)
);
foreach ($arr as $k => $s_arr) {
echo "[" . $k . "] => ";
foreach ($s_arr as $k2 => $v2) {
echo $v2["number"] . " ";
}
echo "\n";
}

Related

Convert Multi dimensional array to Single array in PHP

Request your help on how to convert the below array as a single array, I tried all these methods:
array_column($testarray, "Name") ,
array_merge($array, , $testarray),
array_map('current', $$testarray[0])
but nothing seem to be working. Basically I need to passes the array value as a string using implode("','", Name) so that the array value would be like below
T : 'Name1','Name2'
D : 'Name11','Name21'
P : 'Name111','Name211'
Original Array
Array
(
[T] => Array
(
[0] => Array
(
[Name] => Name1
)
[1] => Array
(
[Name] => Name2
)
)
[D] => Array
(
[0] => Array
(
[Name] => Name11
)
[1] => Array
(
[Name] => Name21
)
)
[P] => Array
(
[0] => Array
(
[Name] => Name111
)
[1] => Array
(
[Name] => Name211
)
)
)
Expected Array
T:
Name: Name1
Name2
D:
Name: Name11
Name21
P:
Name: Name111
Name211
From,
Vino
try this one
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
You can flatten your array using array_map and array_column:
$newarr = array_map(function ($v) { return array_column($v, 'Name'); }, $array);
print_r($newarr);
This gives:
Array
(
[T] => Array
(
[0] => Name1
[1] => Name2
)
[D] => Array
(
[0] => Name11
[1] => Name21
)
[P] => Array
(
[0] => Name111
[1] => Name211
)
)
You can then output the key with each array imploded for your string result:
foreach ($newarr as $key => $arr) {
echo "$key: " . implode(',', $arr) . "\n";
}
Output:
T: Name1,Name2
D: Name11,Name21
P: Name111,Name211
Demo on 3v4l.org
$arr = [
'T' => [['Name' => 'Name1'], ['Name' => 'Name2']],
'D' => [['Name' => 'Name11'], ['Name' => 'Name11']],
'P' => [['Name' => 'Name111'], ['Name' => 'Name211']]
];
array_walk($arr,function(&$v, $k){
$v = ['Name' => array_column($v, 'Name')];
});
print_r($arr);
OUTPUT:
Array
(
[T] => Array
(
[Name] => Array
(
[0] => Name1
[1] => Name2
)
)
[D] => Array
(
[Name] => Array
(
[0] => Name11
[1] => Name11
)
)
[P] => Array
(
[Name] => Array
(
[0] => Name111
[1] => Name211
)
)
)

print array values in new format in array

I want to send the value to jquery-variable in new format, i am using nested foreach loop
here is my code:
foreach($names as $key1 => $desc1) {
$arr_names[] = $names;
$details = get_details( $key1 );
if ( is_array( $states ) ) {
foreach($details as $key2 => $desc2) {
$title[] = $key2;
$value[] = $desc2;
}
}
}
echo "<input type='hidden' id='storageElement' data-storectrystts='".json_encode($namdetailsArray)."'>";
Right now, i can get this:
Array
(
[0] => Sam
[1] => Ben
[2] => John
[0] => Age
[1] => Place
[2] => Height
[3] => Weight
[4] => Year
[5] => Salary
[0] => 30
[1] => AU
[2] => 6
[3] => 150
[4] => First
[5] => 50000
)
I need to send value in this format through data-storectrystts='".json_encode($namdetailsArray).":
'Sam' => [
['Age', '30'],
['Place', 'AU']],
'Ben' => [
['Height', '6'],
['Weight', '150']],
'John' => [
['Year', 'First'],
['Salary', '50000']]
Update
//if we use print_r($namdetailsArray); then everything is fine
Array
(
[Sam] => Array
(
[0] => Array
(
[0] => Age
[1] => 30
)
[1] => Array
(
[0] => Place
[1] => AU
)
)
[Ben] => Array
(
[0] => Array
(
[0] => Height
[1] => 6
)
[1] => Array
(
[0] => Weight
[1] => 150
)
)
[John] => Array
(
[0] => Array
(
[0] => Year
[1] => First
)
[1] => Array
(
[0] => Salary
[1] => 50000
)
)
[Derek] => Array
(
[0] => Array
(
[0] => tax's cal
[1] => 100
)
[1] => Array
(
[0] => distance
[1] => 5
)
)
)
//if we use echo json_encode($namdetailsArray) then everything is fine
{"Sam" => [
["Age", "30"],
["Place", "AU"]],
"Ben" => [
["Height", "6"],
["Weight", "150"]],
"John" => [
["Year", "First"],
["Salary", "50000"]],
"Derek" => [
["tax's cal", "100"],
["distance", "5"]]}
/*but if we use
echo "<input type='hidden' id='storageElement' data-storectrystts='".json_encode($namdetailsArray)."'>";
echo '<div id="availhai"></div>';
var cSttArry = $("#storageElement").data('storectrystts');
$("#availhai").html(cSttArry);
then it is not showing anything after word "tax" */
{"Sam" => [
["Age", "30"],
["Place", "AU"]],
"Ben" => [
["Height", "6"],
["Weight", "150"]],
"John" => [
["Year", "First"],
["Salary", "50000"]],
"Derek" => [
["tax
You shouldn't be creating lots of separate arrays. Create a single associative array where the key is the name, and the value is a 2-dimensional array like you show.
$namdetailsArray = array();
foreach ($names as $name => $desc) {
$personArray = array();
foreach ($desc as $key => $value) {
$personArray[] = array($key, $value);
}
$namdetailsArray[$name] = $personArray;
}

Change key of multidimensional array

I have following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.
This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.
Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);

How to merge array field values in php?

My array is like this:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord
)
[1] => Array
(
[id] => 2
[name] => b
[hardware_type] => mouse
)
[2] => Array
(
[id] => 1
[name] => a
[hardware_type] => mouse
)
[3] => Array
(
[id] => 1
[name] => a
[hardware_type] => moniter
)
[4] => Array
(
[id] => 2
[name] =>b
[hardware_type] => keyboad
)
)
required out put like this i want only merge hardware type
Array(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord, mouse, moniter
)
[1] => Array
(
[id] => 1
[name] => b
[hardware_type] => keyboard, mouse
)
)
Where $array is the input array you described, where $newarray is the output array you desire, and assuming every value of id has the same name as in your example input:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
if (empty($newarray[$item['id']]['hardware_type']))
$temp[$item['id']]['hardware_type'] = $item['hardware_type'];
else
$temp[$item['id']]['hardware_type'] .= ', ' . $item['hardware_type'];
}
$newarray = array_values($temp);
If you want the hardware_type to be an array instead of a comma-separated list of strings, do this instead:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
$temp[$item['id']]['hardware_type'][] = $item['hardware_type'];
}
$newarray = array_values($temp);

How to eqally split an array to 3 arrays

I have a one dimensional array as follows. (It may dynamically extend to any length)
Array
(
[0] => Array
(
[city] => Trivandrum
[citykey] => ab5416c6
)
[1] => Array
(
[city] => Kochi
[citykey] => 85cb7d9c
)
[2] => Array
(
[city] => Alappuzha
[citykey] => 4d5f200e
)
[3] => Array
(
[city] => Mumbai
[citykey] => 47d98024
)
)
Now I would like to split it to 3 arrays . I mean I would like to split an array in to 3 arrays as follows.
Array
(
[0] => Array
(
[city] => Trivandrum
[citykey] => ab5416c6
)
[1] => Array
(
[city] => Mumbai
[citykey] => 47d98024
)
)
Array
(
[0] => Array
(
[city] => Kochi
[citykey] => 85cb7d9c
)
)
Array
(
[0] => Array
(
[city] => Alappuzha
[citykey] => 4d5f200e
)
)
$a = range(1,4); // example input array of 4 items
$n = 3; // number of pieces you want in the output array $b
$b = array_fill(0, $n, array());
for($i=0; $i<count($a); $i++) {
$b[$i % $n][] = $a[$i];
}
print_r($b);
// if you need to split them out
list($a1,$a2,$a3) = $b;
Try this code:
<?
$i=0;
$array=Array(0=> Array("city" => 'Trivandrum',
"citykey" => 'ab5416c6'
),
1 => Array
(
"city" => 'Kochi',
"citykey" => '85cb7d9c'
),
2 => Array
(
"city" => 'Alappuzha',
"citykey" => '4d5f200e'
),
3 => Array
(
"city" => 'Mumbai',
"citykey" => '47d98024'
)
);
$array_0=array();
$array_1=array();
$array_2=array();
foreach ($array as $value)
{
$name='array_'.$i%3;
$temp[0]=$value;
$$name=array_merge_recursive($$name, $temp);
$i++;
}
echo '<pre>';
print_r($array_0);
print_r($array_1);
print_r($array_2);
echo '</pre>';
?>
Your original array should be in variable $array

Categories