how to get array like this? - php

I have one array in php like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] => People
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] => People
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] => Facility
)
)
I want to replace the duplicate type with blank. but the first one should have that type name and others should have blank. so the result array should be like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
I want array in this format only. and i am using PHP zend.
I search for this but most of them showing to remove that element from array. but i don't want to remove it. i want to replace it with blank but want to show the first one.
Tried Code
$result = array();
$result1 = array();
$result2 = array();
$y = array();
$y1 = array();
foreach ($data as $entry) {
$type= $entry["type"];
if (!isset($y[$type])) {
$y[$type] = array();
unset($entry["type"]);
$result[$type][] = $entry;
}
}
can anyone tell me how to do that ?

Code
$i = 0;
$found = 0;
foreach($data as $key=>&$val) {
if($i != 0) {
if($data[$i]['Type'] == $data[$found]['Type']) {
$data[$i]['Type'] = "";
}
else {
$found = $i;
}
}
$i++;
}
echo "<pre>";
print_r($data);
Output
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
Example

Related

Rearrange items in multidimensional array

I have a multidimensional array, I want to take the key 'data' from each array item and form another array, like first array 'data' element has 3 items, second has 1 item, 3rd and 4th are empty, and 5th has one item, I want to make a separate array like $temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]
input array is,
Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
)
the output to be like this
Array
(
[0] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
[1] => Array
(
[0] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
[2] => Array
(
[0] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
)
You need to do it like below:-
$data_array= array_column($array,'data');
$count = 0;
foreach($data_array as $data){
$real_count = count($data);
if($real_count > $count){
$count = $real_count;
}
}
echo $count;
$final_array = [];
foreach($data_array as $data_arr){
for($i=0;$i< $count; $i++){
$final_array[$i][] = (count($data_arr[$i])>0)? $data_arr[$i]: array();
}
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/925383
Reference:- PHP: array_column - Manual
You can use array_column() function
Example :-
<?php
$array = [
[
"meal_type" => "bf",
"data" => [
"name" => "test",
"email" => "test#ymail.com"
]
],
[
"meal_type" => "bf",
"data" => []
]
];
echo "<pre>";
print_r(array_column($array, "data"));
?>
You can do this using foreach and array_push.
$data_item_array = [];
foreach ($array as $item) {
$data = $item['data'];
foreach ($data as $t) {
array_push($data_item_array, $t);
}
}
print_r($data_item_array);
Output will be :
Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[3] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)

Move a child array to parent array and change parent/child name

I know probably this was asked before not sure if was in this form but I did tried some replay from what I found here about this and failed.
ok I have this array
Array
(
[0] => Array
(
[Data3] => Array
(
[id] => 5
[category] => Whiskey
[name] => Some name
[description] => description
[image] => asdf.jpg
[price] => 83.99
)
[ammount] => 1
[Data_id] => 3
)
[1] => Array
(
[Data3] => Array
(
[id] => 4
[category] => Tequila
[name] => Something Red 75cl
[description] => description
[image] => sierratequilasilver100.jpg
[price] => 92.49
)
[ammount] => 2
[Data_id] => 3
)
[2] => Array
(
[Data4] => Array
(
[id] => 3
[category] => Whiskey
[name] => Some name Gold
[description] => description
[image] => asdf.jpg
[price] => 83.99
)
[ammount] => 1
[Data_id] => 4
)
[3] => Array
(
[Data4] => Array
(
[id] => 5
[category] => Vodka
[name] => Something Blue 100 cl
[description] => description
[image] => Something.jpg
[price] => 32.44
)
[ammount] => 1
[Data_id] => 4
)
)
What I would like to be the result is something like this:
Array
(
[0] => Array
(
[id] => 5
[category] => Whiskey
[name] => Some name
[description] => description
[image] => asdf.jpg
[price] => 83.99
[ammount] => 1
[Data_id] => 3
)
[1] => Array
(
[id] => 4
[category] => Tequila
[name] => Something Red 75cl
[description] => description
[image] => sierratequilasilver100.jpg
[price] => 92.49
[ammount] => 2
[Data_id] => 3
)
[2] => Array
(
[id] => 3
[category] => Whiskey
[name] => Some name Gold
[description] => description
[image] => asdf.jpg
[price] => 83.99
[ammount] => 1
[Data_id] => 4
)
[3] => Array
(
[id] => 5
[category] => Vodka
[name] => Something Blue 100 cl
[description] => description
[image] => Something.jpg
[price] => 32.44
[ammount] => 1
[Data_id] => 4
)
)
or another way I could work with is if I can change Data1, Data2, Data3 and so on ..
can be n Data depends how many producs a user select
into a same name ex simple Data or Info.
ex:
Array
(
[0] => Array
(
[Info] => Array
(
[id] => 5
[category] => Whiskey
[name] => Some name
[description] => description
[image] => asdf.jpg
[price] => 83.99
)
[ammount] => 1
[Data_id] => 3
)
[1] => Array
(
[Info] => Array
(
[id] => 4
[category] => Tequila
[name] => Something Red 75cl
[description] => description
[image] => sierratequilasilver100.jpg
[price] => 92.49
)
[ammount] => 2
[Data_id] => 3
)
Any solution will be fine for me.
Thanks and regards
Use this code for your result:
$final_array = array();
foreach($array1 as $offset1 => $array2) {
$tmp_array = array();
foreach($array2 as $offset2 => $array3) {
if(is_array($array3)) {
$tmp_array = $array3;
} else {
$tmp_array[$offset2] = $array3
}
}
$final_array = array_merge($final_array, $tmp_array;);
//or
$final_array[] = $tmp_array;
}
I would do this this way. However I would look to fix why the data is in that structure to begin with.
$aStartArray = array(array('Data3'=>array('id'=>1, 'cat'=>2), 'amount' => 1, 'Data_id'=>3));
foreach ($aStartArray as $iPos => $aArray) {
$aKeys = array_keys($aArray); // fetches all the keys
$aFirstElement = $aArray[$aKeys[0]]; // Get the first element using first key
// assign/ overwrite data at the same position
$aStartArray[$iPos] = array($aFirstElement, 'amount' => $aArray['amount'], 'Data_id' => $aArray['Data_id']);
}
echo "<pre>";
var_dump($aStartArray);
your first option:
foreach($arr as $key => $value){
foreach($value as $k => $val){
if(is_array($val)){
$arr[$key] = $val;
unset($arr[$key][$k]);
}
}
}
echo "<pre>"; print_r($arr);
Check output here
This is the best algorithm
const moveArrayToParentArray = (input) => {
let finalOutput = []
input.forEach((e) => {
if (Array.isArray(e)) {
finalOutput = [...finalOutput, ...e];
} else {
finalOutput = [...finalOutput, e];
}
})
return finalOutput
}
const array = ['a', 'b']
const array2 = [array]
const array3 = [array, "c"]
console.log(moveArrayToParentArray(array))
console.log("----------------------------")
console.log(moveArrayToParentArray(array2))
console.log("----------------------------")
console.log(moveArrayToParentArray(array3))
console.log("----------------------------")

Iterating recursively through an array

Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}

convert this array format to single array in php

I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);

PHP array_count_values with multidimensional array

I have this array:
Array (
[0] => stdClass Object ( [id] => 123 [name] => Alex )
[1] => stdClass Object ( [id] => 124 [name] => John )
[2] => stdClass Object ( [id] => 123 [name] => Alex )
[3] => stdClass Object ( [id] => 124 [name] => John)
[4] => stdClass Object ( [id] => 126 [name] => Paul )
)
And I want to output like the following:
Array (
[0] => Array ( [id] => 123 [name] => Alex [count] = 2 )
[1] => Array ( [id] => 124 [name] => John [count] = 2 )
[2] => Array ( [id] => 126 [name] => Paul [count] = 1 )
)
I tried using array_count_values($array), but it doesn't work.
Any ideas how to solve this?
You can simply use array_map along with simple foreach like as
foreach(array_map("get_object_vars",$arr) as $val){
$hash = $val['id'];
if(isset($result[$hash])){
$result[$hash]['count'] += $result[$hash]['count'];
}else{
$result[$hash] = $val;
$result[$hash]['count'] = 1;
}
}
print_r(array_values($result));
Output:
Array
(
[0] => Array
(
[id] => 123
[name] => Alex
[count] => 2
)
[1] => Array
(
[id] => 124
[name] => John
[count] => 2
)
[2] => Array
(
[id] => 126
[name] => Paul
[count] => 1
)
)

Categories